Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
230 views
in Technique[技术] by (71.8m points)

How to include javaScript file in xslt

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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> 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...