views:

1981

answers:

4

Is it possible to have XML-embedded JavaScript executed to assist in client-side (browser-based) XSL transformations? How is it done and how official is it?

Microsoft's XML DOM objects allow this on the server-side (i.e. in ASP/ASP.NET).

Clarification: I do not mean HTML DOM scripting performed after the document is transformed, nor do I mean XSL transformations initiated by JavaScript in the browser (e.g. what the W3Schools page shows). I am referring to actual script blocks located within the XSL during the transformation.

+3  A: 

Yes. It's browser dependant but you can use Javascript. There is a small but practical tutorial on w3schools.com. It's part of the XSLT tutorial.

The page:

http://www.w3schools.com/xsl/xsl_client.asp

The XSLT tutorial:

http://www.w3schools.com/xsl/default.asp

That site will be more helpful than myself. Good luck!

helios
A: 

I don't think you can execute JavaScript code embedded inside XML documents. Like, helios mentioned, you can preform the transformation using JavaScript.

JavaScript is embedded as CDATA in most cases, which is usually used after the XSL transformation has taken place. If I understand correctly, you want to have an executable <script> tag in your XML.

You could use XSL parameters and templates if you need a more control on your transformations. You can set these values in your XSLT and then pass them to exec(). Mozilla supports setting parameters in XSL, but I'm not sure about other browsers.

Also, cross-browser JavaScript/XSLT is a pain in the neck. Mozilla's JavaScript/XSLT interface is very different than IE's, so you might want to rely on a browser-independent library like jQuery's XSLT.

cubex
A: 

I doubt you'll find what you're looking for if "official" means "standards-based." What you describe is a user-agent scripting language to be parsed and executed during a style-sheet parsing. If your aim is to simplify XSLT by doing the dirty work in Javascript, you may be better off trying to generate the XSLT in javascript and then using a class wrapper to parse the result in via the browser's own XSLT parser.

This is of course a lot more work than you probably signed on for, but if you're convinced you want to do so, I'd take look at John Resig's Javascript Micro-Templates to dynamically store template-friendly XSLT in your javascript.

enricopulatzo
+1  A: 

To embed JavaScript for the aid of transformation you can use <xsl:script>, but it is limited to Microsoft's XML objects implementation. Here's an example:

scripted.xml:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="scripted.xsl"?>
<data a="v">
    ding dong
</data>

scripted.xsl:

<?xml version="1.0" encoding="ISO-8859-1"?>
<html xmlns:xsl="http://www.w3.org/TR/WD-xsl"&gt;
<xsl:script implements-prefix="local" language="JScript"><![CDATA[

    function Title()
    {
        return "Scripted";
    }

    function Body(text)
    {
        return "/" + text + "/";
    }

]]></xsl:script>
<head>
    <title><xsl:eval>Title()</xsl:eval></title>
</head>
<body>
    <xsl:for-each select="/data"><xsl:eval>Body(nodeTypedValue)</xsl:eval></xsl:for-each>
</body>
</html>

The result in Internet Explorer (or if you just use MSXML from COM/.NET) is:

<html>
<head>
    <title>Scripted</titlte>
</head>
<body>
    /ding dong/
</body>
</html>

It doesn't appear to support the usual XSL template constructs and adding the root node causes MSXML to go into some sort of standards mode where it won't work.

I'm not sure if there's any equivalent functionality in standard XSL, but I can dream.

Neil C. Obremski