如何在 xslt 中包含 javaScript 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7444317/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 02:18:29  来源:igfitidea点击:

How to include javaScript file in xslt

javascriptxsltweb

提问by jasin_89

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

如何在 xslt 文件中包含/导入 javaScript 文件/库。

采纳答案by Dimitre Novatchev

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.

如果您需要在转换中使用 javascript(例如,它包含一组在转换中调用的扩展函数),您需要将 javascript 内容(至少是一个 javascript 文件的内容)放在单独的 XSLT 样式表中文件,使用适当的扩展元素(例如<msxml:script>)作为包含 javascript 代码的文本节点的父节点。

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

这是一个非常简单的示例,使用任何 Microsoft XSLT 处理器(MSXML3/4/6、XslCompiledTransform 或 XslTransform)

file XSL-JS.xsl:

文件 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:

正在导入 javascript 的文件 XSL-Main.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">
 <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:

当包含在文件 XSL-Main.xsl 中的转换应用于任何 XML 文档(未使用/忽略)时,会产生所需的正确结果

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.

一个完全不同的情况是,如果您只想用 XSLT 应用程序生成一个引用给定 Javascript 文件的 HTML 文件

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

然后您将其包含在您的 XSLT 代码中,并将其作为输出的一部分逐字生成:

<script type="text/javascript" src="SomePath/SomeFileName.js"></script> 

回答by bitfiddler

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:

如果您尝试在生成的 HTML 文件中包含 Javascript 源代码,那么脚本标记就是方法。但是,如果标签为空,许多 XSLT 处理器会卡住。如果一切正常,但在添加脚本标签时输出为空,请尝试在标签中插入一个不间断空格,如下所示:

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

回答by Binary01

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

参考:https: //www.w3schools.com/xml/dom_nodes_traverse.asp