让 javascript 为 .xml 文件执行
我在将 Javascript 作为 CDATA 添加到 .xml 文件时遇到问题。这可能吗?我从:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>XHTML 5 Example</title>
<script type="application/javascript">
<![CDATA[
function loadpdf() {
document.getElementById("pdf-object").src="http://www.w3.org/TR/xhtml1/xhtml1.pdf";
}
]]>
</script>
</head>
<body onload="loadpdf()">
<p>This is an example of an
<abbr title="Extensible HyperText Markup Language">XHTML</abbr> 1.0 Strict document.<br/>
<img id="validation-icon"
src="http://www.w3.org/Icons/valid-xhtml10"
alt="Valid XHTML 1.0 Strict"/><br/>
<object id="pdf-object"
type="application/pdf"
data="http://www.w3.org/TR/xhtml1/xhtml1.pdf"
width="100"
height="500"/>
</p>
</body>
</html>
example#2 (我更改了宽度值以通过 w3c 验证)
这个文件可以保存为格式良好的 .xml 文件吗?如果可以,javascript 是否应该执行?
I'm having trouble adding Javascript as CDATA to .xml files. Is that possible? I'm starting from:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>XHTML 5 Example</title>
<script type="application/javascript">
<![CDATA[
function loadpdf() {
document.getElementById("pdf-object").src="http://www.w3.org/TR/xhtml1/xhtml1.pdf";
}
]]>
</script>
</head>
<body onload="loadpdf()">
<p>This is an example of an
<abbr title="Extensible HyperText Markup Language">XHTML</abbr> 1.0 Strict document.<br/>
<img id="validation-icon"
src="http://www.w3.org/Icons/valid-xhtml10"
alt="Valid XHTML 1.0 Strict"/><br/>
<object id="pdf-object"
type="application/pdf"
data="http://www.w3.org/TR/xhtml1/xhtml1.pdf"
width="100"
height="500"/>
</p>
</body>
</html>
example#2 (I changed the width value to pass w3c validation)
can this file be saved as a well-formed .xml file, and should the javascript execute if so?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 XHTML 中放置 JavaScript
script
标记或 CSSstyle
标记时,最好将其放置在< 中/代码> 标签。但是,浏览器会将其视为脚本的一部分。要防止执行这部分脚本,只需注释掉您的
标记:
或者
这将允许您在 XML 中使用特殊字符,而无需对其进行编码,并将允许您的脚本正确执行而不会出现错误。此外,如果要解析空白,多行注释是一个不错的选择。
编辑添加:
我相信
object
元素应该有一个结束标记(尽管是空的),类似于当外部源时应如何向script
标记添加结束标记已指定。 到目前为止我还无法验证这一点。对于 XHTML,建议的文件扩展名仍然是
.html
,但是您可以使用任何文件扩展名(甚至 .pdf)保存文件。仅仅因为您将其标记为不同的文件扩展名并不意味着内容对于该扩展名一定有效,也不意味着应用程序将能够读取该文件。将 XHTML 保存为
.xml
文件是完全合法的,并且如果它是正确形成的 XHTML,则任何可以解析 XML 的应用程序都可以完全解析它。如果 Web 浏览器打开 XML 文件,它可能会将内容显示为 XML 树。如果您希望 Web 浏览器将其作为网页读取,则应将其另存为 (.html)注意事项:XML 文本节点中会保留空格和换行符。如果您使用 XML 解析器解析页面,并且为了可读性而进行了缩进,则解析器将在输出中包含换行符和换行符。
跨度的内容本质上是:
“\n 一些文本\n 在多个\n 行\n”
解决这个问题的方法是延长结束角度括号到文本节点的开头:
这是有效的,因为元素内的额外空白被忽略。
When putting a JavaScript
script
tag or CSSstyle
tag in XHTML, it is good form to place it in a<![CDATA[ ]]>
tag. However, it will be viewed by the browser as part of your script. To prevent this part of the script from executing, simply comment out your<![CDATA[]]>
tag:OR
This will allow you to use special characters in XML without needing to encode them, and will allow your script to execute properly without errors. Additionally, the multi-line comment is a good choice should white-space ever be parsed out.
EDIT to add:
I believe the
object
element should have a closing tag (despite being empty) similar to how you should add a closing tag to thescript
tag when an external source is specified. I haven't been able to verify this as of yet.As for XHTML, the recommended file extension is still
.html
, however you may save the file with any file extension (even .pdf). Just because you have labeled it as a different file extension doesn't mean the content is necessarily valid for that extension, and doesn't mean that applications will be able to read the file.Saving XHTML as a
.xml
file is perfectly legitimate, and if it is properly formed XHTML it will be fully parsable by any application that can parse XML. If a web browser opens an XML file it will likely display the contents as an XML tree. If you would like the web browser to read it as a webpage, you should save it as such (.html)A word of caution: White space and line breaks are preserved in XML text nodes. If you parse a page using an XML parser and you've indented for readability purposes, the parser will include the line breaks and newline characters in the output.
The contents of the span will essentially be:
"\n Some text\n on multiple\n lines\n"
A way around this is to extend the ending angle bracket to the beginning of a text node:
This works because extra white-space within an element is ignored.