如何在xslt中包含javaScript文件 [英] How to include javaScript file in xslt
问题描述
如何在xslt文件中包含/导入javaScript文件/ libary。
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屋!