How to include Javascript in xml-document?

The task seems to be pretty easy: how to include a Javascript file in xml-document so that at least Opera and Firefox could actually parse it and execute the code?



Add a script stating XHTML namespace, it will run just fine.

<xhtml:script xmlns:xhtml=""

See also


If I get you, you want an XML document to run javascript when viewed in a browser?

This is not part of the XML standard, and as such will not be suppoted until it is (I assume this will never be supported because XML is not intended for display, but data). If you are talking about XHTML then this is a different matter.


Edit: just to clarify my answer.

XML was never intended to be a display markup like HTML, thats why XHTML was developed (HTML that conforms to XML standards). Browsers have been made to interpret XHTML in a certain way, but XML is simply raw data.

If you want your XML to run additions such as JavaScript you will want to consider using XSLT to transform your XML into XHTML and therefore take advantage of a browsers capabilities.

<script xmlns=""><![CDATA[

Or for external javascript:

<script xmlns="" src="external.js"></script>

Works in Firefox and Opera.


I did this:


<xsl:value-of select="/label[@id='MyScript']/text()" disable-output-escaping="yes"/>


<label id="MyScript"><![CDATA[
<script type="text/javascript">
alert("Hello world");

Embed the XML and the Javascript in an XHTML document and then use the vast and well-documented capabilities of dynamic HTML.

You'll get something up and running much faster than you will by reasoning that since some browsers implement weak and poorly-documented CSS styling of XML documents, therefore they must support the execution of Javascript embedded in XML, as though there were any connection whatsoever between those two ideas.


Similar to the above, but that could error because the <![CDATA[ and ]]> portions are not valid code. If you're putting it into an XSL script, you can just as well put a JS comment mark before these beginning and ending elements.

I also have used the xsl:text element to output the <![CDATA[ portion. This portion may be a bit of cheat, but it results in well-formed XML. An example from within an xsl:choose block might be...

<xsl:when test='name()="script"'>
    <xsl:for-each select='@*'><xsl:copy-of select='.' /></xsl:for-each>
    <xsl:text disable-output-escaping='yes'>
// &lt;![CDATA[
  <xsl:copy-of select='./text()' />
  <xsl:text disable-output-escaping='yes'>

Walking through the pieces...

  1. Detect a <script> element.
  2. Replicate <script> tag for the output.
  3. Be sure to preserve the tag attributes in the output with a quick xsl:for-each line.
  4. Output the non-escaped text: // <![CDATA[. The // renders the rest of the line as a comment and thus prevents a JS error.
  5. Copy the text contents of the <script> tag. NOTE: You must preseve the new-line (either as above or some other way) so that the commented out line before it does not end up on the same line as this one. Obviously, if it does, it will comment out this line as well. Preserving the one after is not essential, but is keeps the aesthetics of the two matching CDATA tags.
  6. Output the non-escaped text: // ]]>. This ends the CDATA block, and again, the CDATA marking is ignored by the browser when reading the JS.
  7. Close the block with a </script> tag, of course.
  8. And, if you're using it in a xsl:choose block, close then xsl:when.

Only steps 2, 3, 5, & 7 actually copy the script block. The rest is busywork to make it work.

Transforming a block such as...

<script type='javascript'>alert('Hello World!');</script>

Then becomes,

<script type='javascript'>
// <![CDATA[
alert('Hello World!');
// ]]>

Effectively preserved, and readable both by XML as well as a browser.


A function that should help you is the eval() function. This function will evaluate any string you pass to it as Javascript.

Also, it's possible to parse XML in Javascript. Just google "javascript xml parser".

Combine these two ideas, and you'll be on your way.

If you simply want to put javascript in the XML file:

<xml> <js script="1"> here is some javascript; here is more javascript; </js> <js script="2"> here is even more javascript; jere is even more javascript; </js> </xml>


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.