如何在xsl文档中使用javascript函数? [英] How to use javascript functions in xsl document?

查看:149
本文介绍了如何在xsl文档中使用javascript函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的xsl文档中有以下代码:我尝试应用它,但它返回以下错误:
错误:XSLTProcessor :: transformToXml()[xsltprocessor.transformtoxml]:返回XPath评估没有结果。
是否有任何想法?

 < xsl:stylesheet 
xmlns:xsl =http://www.w3.org/1999/XSL/Transform
xmlns:user =http://mycompany.com/mynamespace
xmlns:msxsl = urn:schemas-microsoft-com:xsltversion =1.0>
< xsl:output method =textencoding =UTF-8/>

< xsl:template match =/>
< xsl:value-of select =CARRIERNAME/>
< xsl:value-of select =user:FormatescapeEmbeddedCommas()/>
< / xsl:template>

< msxsl:script language =javascriptimplements-prefix =user>

函数FormatescapeEmbeddedCommas(){

返回ABC;
}

< / msxsl:script>

< / xsl:stylesheet>


解决方案


 < msxsl:script language =javascriptimplements-prefix =user> 


支持用于在XSL中编写脚本的Microsoft扩展在Chrome中不可用。



如果您希望XSL在浏览器中工作,请查看 XSLT浏览器比较图表,因为跨浏览器支持不幸是相当有限的。

最好的办法是使用XSLT 1.0-only结构,不要使用外部文档( xsl:import document()函数),因为这些在Chrome中默认被阻止,并且if你需要函数,使用命名模板,因为创建一个跨浏览器的扩展函数会很棘手(我不确定Chrome是否支持这个功能,但绝对不是MS命名空间)。



如果您确实需要使用特定于供应商的扩展程序,请确保在以下内容中添加 xsl:if 使用 system-property('xsl:vendor')测试子句的结构,它将检查您的浏览器。



我发现给定的错误比较奇怪,我会预料它会像找不到函数一样。



请注意,默认情况下,即使在Internet Explorer,默认情况下禁用XSLT脚本

如果您有任何在浏览器外运行样式表的机会,您可能会为自己节省很多麻烦和宝贵的时间,并且可能使您使用XSLT 2.0或3.0而不是仅使用XSLT 1.0。 Saxon拥有一个名为Saxon-CE的 JS动力版XSLT 2.0处理器,和并声称是轻量级的。 XSLT 2.0使用扩展函数或指令的要求非常少,因为它更加灵活,并且允许您使用 xsl:function


I have the following code in my xsl document : I tried to apply it but it is returning the following error: Error:XSLTProcessor::transformToXml() [xsltprocessor.transformtoxml]: XPath evaluation returned no result. Does any one have an idea ??

 <xsl:stylesheet
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:user="http://mycompany.com/mynamespace"
            xmlns:msxsl="urn:schemas-microsoft-com:xslt" version="1.0">
            <xsl:output method="text" encoding="UTF-8" />

            <xsl:template match="/">
            <xsl:value-of select="CARRIERNAME"/>
            <xsl:value-of select="user:FormatescapeEmbeddedCommas()"/>
             </xsl:template>

             <msxsl:script language="javascript" implements-prefix="user">

        function FormatescapeEmbeddedCommas() {

            return "ABC";
 }

</msxsl:script>

     </xsl:stylesheet>

解决方案

<msxsl:script language="javascript" implements-prefix="user">

Support for the Microsoft extension for scripting in XSL is not available in Chrome.

If you want your XSL to work in browsers, have a look at this XSLT browser comparison chart, as cross-browser support is unfortunately quite limited.

Best bet is to use XSLT 1.0-only constructs, to not use external documents (xsl:import, the document() function), because these are blocked by default in Chrome, and if you need functions, use named templates, as it will be tricky to create a cross-browser extension function (I'm not sure if Chrome supports this at all, but definitely not with the MS namespace).

If you do need to use vendor-specific extensions, make sure to add xsl:if around these instructions with a test-clause using system-property('xsl:vendor'), which will check in what browser you are.

I find the given error rather odd, I would have expected it to be something like "function not found".

Note that by default, even in Internet Explorer, XSLT scripts are disabled by default.

If you have any chance of running your stylesheet outside the browser, you will likely save yourself a lot of trouble and precious time, and it may enable you to use XSLT 2.0 or 3.0 instead of only XSLT 1.0. Saxon has a JS-powered version of their XSLT 2.0 processor called Saxon-CE, and Frameless is a cross-browser implementation of a large part of the XSLT 2.0 specification and claims to be lightweight. The requirement to use extension functions or instructions is much less with XSLT 2.0 as it is more flexible and allows you to create your own stylesheet functions using xsl:function.

这篇关于如何在xsl文档中使用javascript函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆