Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include javaScript file in xslt

Tags:

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

like image 528
jasin_89 Avatar asked Sep 16 '11 11:09

jasin_89


People also ask

How add Javascript to XSLT?

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 ...

Is XSL and XSLT the same?

XSLT is designed to be used as part of XSL. In addition to XSLT, XSL includes an XML vocabulary for specifying formatting. XSL specifies the styling of an XML document by using XSLT to describe how the document is transformed into another XML document that uses the formatting vocabulary.

Can you use CSS with XSLT?

An XSLT style sheet can emit HTML <STYLE> elements, including CSS specifications, directly into the HTML that results from the XSLT transformation. This option works best when the number of CSS rules is small and easily managed.


1 Answers

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>  
like image 109
Dimitre Novatchev Avatar answered Sep 23 '22 01:09

Dimitre Novatchev