<p>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?</p>

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

<p><strong>Clarification:</strong> I do not mean HTML DOM scripting performed <em>after</em> the document is transformed, nor do I mean XSL transformations <em>initiated</em> 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.</p>

[{'answer_id': 66050, 'author': 'helios', 'author_id': 9686, 'author_profile': 'https://Stackoverflow.com/users/9686', 'pm_score': 1, 'selected': False, 'text': '<p>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.</p>\n\n<p>The page:</p>\n\n<p><a href="http://www.w3schools.com/xsl/xsl_client.asp" rel="nofollow noreferrer">http://www.w3schools.com/xsl/xsl_client.asp</a></p>\n\n<p>The XSLT tutorial:</p>\n\n<p><a href="http://www.w3schools.com/xsl/default.asp" rel="nofollow noreferrer">http://www.w3schools.com/xsl/default.asp</a></p>\n\n<p>That site will be more helpful than myself. Good luck!</p>\n'}, {'answer_id': 66609, 'author': 'rami', 'author_id': 9629, 'author_profile': 'https://Stackoverflow.com/users/9629', 'pm_score': 2, 'selected': False, 'text': '<p>I don\'t think you can execute JavaScript code embedded inside XML documents. Like, helios mentioned, you can preform the transformation using JavaScript.</p>\n\n<p>JavaScript is embedded as CDATA in most cases, which is usually used <strong>after</strong> the XSL transformation has taken place. If I understand correctly, you want to have an executable &lt;script&gt; tag in your XML.</p>\n\n<p>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 <a href="http://developer.mozilla.org/en/The_XSLT%2f%2fJavaScript_Interface_in_Gecko/Setting_Parameters" rel="nofollow noreferrer">setting parameters</a> in XSL, but I\'m not sure about other browsers.</p>\n\n<p>Also, cross-browser JavaScript/XSLT is a pain in the neck. <a href="http://developer.mozilla.org/en/Using_the_Mozilla_JavaScript_interface_to_XSL_Transformations" rel="nofollow noreferrer">Mozilla\'s JavaScript/XSLT interface</a> is very different than <a href="http://www.learn-ajax-tutorial.com/Xslt.php" rel="nofollow noreferrer">IE\'s</a>, so you might want to rely on a browser-independent library like <a href="http://www.jongma.org/webtools/jquery/xslt/" rel="nofollow noreferrer">jQuery\'s XSLT</a>.</p>\n'}, {'answer_id': 66621, 'author': 'enricopulatzo', 'author_id': 9883, 'author_profile': 'https://Stackoverflow.com/users/9883', 'pm_score': 1, 'selected': False, 'text': '<p>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.</p>\n\n<p>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 <a href="http://ejohn.org/blog/javascript-micro-templating/" rel="nofollow noreferrer">John Resig\'s Javascript Micro-Templates</a> to dynamically store template-friendly XSLT in your javascript.</p>\n'}, {'answer_id': 74836, 'author': 'Neil C. Obremski', 'author_id': 9642, 'author_profile': 'https://Stackoverflow.com/users/9642', 'pm_score': 3, 'selected': True, 'text': '<p>To embed JavaScript for the aid of transformation you can use &lt;xsl:script&gt;, but <a href="http://www.topxml.com/xsl/tutorials/intro/xsl10.asp" rel="nofollow noreferrer">it is limited</a> to Microsoft\'s XML objects implementation.  Here\'s an <a href="https://oxampleski.googlecode.com/svn/trunk/XSL" rel="nofollow noreferrer">example</a>:</p>\n\n<p><strong>scripted.xml</strong>:</p>\n\n<pre><code>&lt;?xml version="1.0"?&gt;\n&lt;?xml-stylesheet type="text/xsl" href="scripted.xsl"?&gt;\n&lt;data a="v"&gt;\n    ding dong\n&lt;/data&gt;\n</code></pre>\n\n<p><strong>scripted.xsl</strong>:</p>\n\n<pre><code>&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;\n&lt;html xmlns:xsl="http://www.w3.org/TR/WD-xsl"&gt;\n&lt;xsl:script implements-prefix="local" language="JScript"&gt;&lt;![CDATA[\n\n    function Title()\n    {\n        return "Scripted";\n    }\n\n    function Body(text)\n    {\n        return "/" + text + "/";\n    }\n\n]]&gt;&lt;/xsl:script&gt;\n&lt;head&gt;\n    &lt;title&gt;&lt;xsl:eval&gt;Title()&lt;/xsl:eval&gt;&lt;/title&gt;\n&lt;/head&gt;\n&lt;body&gt;\n    &lt;xsl:for-each select="/data"&gt;&lt;xsl:eval&gt;Body(nodeTypedValue)&lt;/xsl:eval&gt;&lt;/xsl:for-each&gt;\n&lt;/body&gt;\n&lt;/html&gt;\n</code></pre>\n\n<p>The result in Internet Explorer (or if you just use MSXML from COM/.NET) is:</p>\n\n<pre><code>&lt;html&gt;\n&lt;head&gt;\n    &lt;title&gt;Scripted&lt;/titlte&gt;\n&lt;/head&gt;\n&lt;body&gt;\n    /ding dong/\n&lt;/body&gt;\n&lt;/html&gt;\n</code></pre>\n\n<p>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.</p>\n\n<p>I\'m not sure if there\'s any equivalent functionality in standard XSL, but I can dream.</p>\n'}]
['https://Stackoverflow.com/questions/66006', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/9642/']