开发者

getting javascript to execute for .xml file

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.&开发者_JAVA百科lt;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?


When putting a JavaScript script tag or CSS style 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:

<script type="text/javascript">
/* <![CDATA[ */

/* ]]> */
</script>

OR

<style type="text/css">
/* <![CDATA[ */

/* ]]> */
</style>

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 the script 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.

<div>
  <span>
    Some text
    on multiple
    lines
  </span>
  <span>
    Some text
    on multiple
    lines
  </span>
</div>

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:

<div
  ><span>Some text on one line</span
  ><span>Some text on one line</span
></div>

This works because extra white-space within an element is ignored.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜