23

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?

orion3
  • 9,797
  • 14
  • 67
  • 93
  • The scope of what you are trying to achieve needs broadening, edit question to add more detail – AnthonyWJones Dec 21 '08 at 16:50
  • 1
    Voting down, because I believe you're confusing cause and effect: the fact that browsers can display XML doesn't mean they're going to interpret it in the same way they interpret HTML. And personally, I consider the xml-stylesheet directive to have been a bad idea. – kdgregory Dec 21 '08 at 17:26
  • 1
    Voted it up because the question seems quite clear to me and @Ady gave a good answer to it. – Greg Dec 21 '08 at 18:22

7 Answers7

24

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

<xhtml:script xmlns:xhtml="http://www.w3.org/1999/xhtml"
                src="file.js"
                type="application/javascript"/>

See also http://www.ibm.com/developerworks/xml/library/x-ffox3/

Tim Babych
  • 3,356
  • 1
  • 20
  • 13
  • @user4815100 This says "in this tag and its descendants, the namespace `xhtml` means the `"http://www.w3.org/1999/xhtml"` namespace". It basically lets you include XHTML elements in any XML document. – wizzwizz4 Oct 27 '18 at 14:49
10

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.

Ady
  • 4,736
  • 2
  • 22
  • 20
  • Yes, you got me right, I'm talking about xml, not xhtml. And I don't care about standards, just actual browser capabilities. They do support , I thought something similar should be for Javascript. – orion3 Dec 21 '08 at 17:01
  • I think you need to consider what XML was intended for. You could use XSLT to transform the XML into XHTML and thefore open the page as an HTML document, with all the javascript you want to run. – Ady Dec 21 '08 at 17:07
  • xhtml is just a special case of xml. Now what makes xml bad for running javascript? Theoretically, js has nothing to do with what xml was designed for, I don't see any connection and I don't see any reasons to restrict js running for xml document. – orion3 Dec 21 '08 at 17:13
  • @snitko - well the main would be that no browser would comprehend it as display markup, and to get something basic like events working you'd have to define so much extension that you may as well be using xhtml which handily already defines these things – annakata Dec 21 '08 at 22:06
8
<script xmlns="http://www.w3.org/1999/xhtml"><![CDATA[
alert('Hello');
]]></script>

Or for external javascript:

<script xmlns="http://www.w3.org/1999/xhtml" src="external.js"></script>

Works in Firefox and Opera.

IS4
  • 11,945
  • 2
  • 47
  • 86
  • can confirm xmlns is the solution :) great answer! Works in Chrome! Although I find it strange that it can run getElementById/querySelectorAll since it isn't in the correct namespace, and my schema might use different attributes to signify them. – Dmytro Jul 24 '16 at 17:13
4

I did this:

XSLT:

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

XML:

<label id="MyScript"><![CDATA[
<script type="text/javascript">
alert("Hello world");
</script>
]]></label>
Rick
  • 629
  • 6
  • 6
0

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"'>
  <script>
    <xsl:for-each select='@*'><xsl:copy-of select='.' /></xsl:for-each>
    <xsl:text disable-output-escaping='yes'>
// &lt;![CDATA[
  </xsl:text>
  <xsl:copy-of select='./text()' />
  <xsl:text disable-output-escaping='yes'>
//]]&gt;
  </xsl:text>
  </script>
</xsl:when>
...

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!');
// ]]>
</script>

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

User51
  • 887
  • 8
  • 17
0

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.

Robert Rossney
  • 94,622
  • 24
  • 146
  • 218
-2

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>

stalepretzel
  • 15,543
  • 22
  • 76
  • 91