如何在 xslt 中包含 javaScript 文件 [英] How to include javaScript file in xslt
问题描述
如何在 xslt 文件中包含/导入 javaScript 文件/库.
How can I include/import javaScript file/libary in xslt file.
推荐答案
如果需要在转换中使用 javascript(例如,它包含一组在转换),您需要将 javascript 内容(至少是一个 javascript 文件的内容)放在单独的 XSLT 样式表文件中,使用适当的扩展元素(例如 <msxml:script>
)作为包含 javascript 代码的文本节点的父节点.
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.
这是一个非常简单的示例,使用任何 Microsoft XSLT 处理器(MSXML3/4/6、XslCompiledTransform 或 XslTransform):
文件 XSL-JS.xsl:
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>
导入 javascript 的 XSL-Main.xsl 文件:
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>
当包含在文件 XSL-Main.xsl 中的转换应用于任何 XML 文档(未使用/忽略)时,就会产生所需的正确结果:
A B C
完全不同的情况是,如果您只想使用 XSLT 应用程序生成引用给定 Javascript 文件的 HTML 文件.
然后您将其包含在您的 XSLT 代码中,并将其作为输出的一部分逐字生成:
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>
这篇关于如何在 xslt 中包含 javaScript 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!