24

How can I include/import javaScript file/libary in xslt file.

1
  • 1
    Good question, +1. Do you want to use the javascript code as a set of extension functions that are called during the transformation, or do you want to generate HTML code that references a javascript file, or both? All these are valid and meaningful scenarios. Commented Sep 16, 2011 at 12:52

4 Answers 4

25

If you need to use the javascript in the transformation (for example, it contains a set of extension functions that are called within the transformation), you need to put the javascript contents (at least that of one javascript file) in a separate XSLT stylesheet file, using the proper extension element (such as <msxml:script>) as the parent of the text-node that contains the javascript code.

Here is a very simple example, using any Microsoft XSLT processor (MSXML3/4/6, XslCompiledTransform or XslTransform):

file XSL-JS.xsl:

<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:msxsl="urn:schemas-microsoft-com:xslt"
      xmlns:user="http://mycompany.com/mynamespace">

 <msxsl:script language="JScript" implements-prefix="user">
   function xml(nodelist) {
      return "A B C";
   }
 </msxsl:script>
</xsl:stylesheet>

File XSL-Main.xsl that is importing the javascript:

<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:msxsl="urn:schemas-microsoft-com:xslt"
      xmlns:user="http://mycompany.com/mynamespace">
 <xsl:import href="XSL-JS.xsl"/>

 <xsl:template match="/">
   <xsl:value-of select="user:xml(.)"/>
 </xsl:template>

</xsl:stylesheet>

When the transformation, contained in the file XSL-Main.xsl is applied on any XML document (not used/ignored), the wanted, correct result is produced:

A B C

A completely different case is if you just want to generate with your XSLT application an HTML file that references a given Javascript file.

Then you include this in your XSLT code and generate this literally as part of the output:

<script type="text/javascript" src="SomePath/SomeFileName.js"></script> 
Sign up to request clarification or add additional context in comments.

5 Comments

Does it work when JavaScript contains // comments?
@U.Windl, Are you still using <msxsl:script> ? Yes, it should work
You are right: I had tried it last week, and the line breaks in the script were not preserved for some reason, so a comment would ignore the rest of the script. But today I tired it again ,and the line breaks are preserved, so it works using the method described in stackoverflow.com/a/30332909/6607497!
@DimitreNovatchev what should be the actual value of "http://mycompany.com/mynamespace". How to determine what should I put there?
@RbG, It can be any string. Just give it a try :)
13

If you are trying to include a Javascript source in the generated HTML file, the script tag is the way. But many XSLT processors will choke if the tag is empty. If everything looks fine but you are getting empty output when you add the script tags, try inserting a non-breaking space within the tag as in:

<script type="text/javascript" src="SomePath/SomeFileName.js">&#160;</script> 

4 Comments

wow awesome ! Thanks , I was having issues using .NET XsltTransform libraries working .
This is very important information. I am beyond lucky to have noticed this. For the life of me I couldn't figure out why my stylesheet doesn't transform right.
To finish up - is there a way to tell the transformer to expect empty tags? I'd REALLY hate to have to find all empty tags and fill them with empty spaces. I'm doing this in C# and using XslCompiledTransform from System.Xml
Found an answer to my question too stackoverflow.com/questions/70658913
13

This is what I did and it worked for me.

<script>
<![CDATA[
   //ADD SCRIPT HERE.
]]>
</script>

Reference: https://www.w3schools.com/xml/dom_nodes_traverse.asp

1 Comment

But this does not seem to consider the "in xslt" requirement: When I tried it, everything was put in a single line, and as the first line was a comment, nothing was available in HTML.
0

In order to use vanilla Javascript, or any framework in JS, you need to use an outer page of PHP. After your PHP processing, you can write the JS below it in a <script></script> format. You can directly cause effects on the page from there with the DOM.

<?php

// Do XLST Loading here

?>

<script>
//Do javascript here
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.