xpath-functions 不识别外部 Java 类 [英] xpath-functions does not identify an external Java class

查看:23
本文介绍了xpath-functions 不识别外部 Java 类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我尝试使用函数 node-name() 时,以下 XSLT 转换都会显示错误.

The following XSLT transformation displays an error whenever I try to use the function node-name().

错误:E[Saxon6.5.5]URI http://www.w3.org/2005/xpath-functions 未识别外部 Java 类

Error: E[Saxon6.5.5]The URI http://www.w3.org/2005/xpath-functions does not identify an external Java class

<xsl:stylesheet version="1.1" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:fn="http://www.w3.org/2005/xpath-functions">
<!--
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
-->        

    <xsl:output method="text" />
    <xsl:variable name="in" select="/"/>
    <xsl:variable name="filter" select="document('elementsToBeLeftIn.xml')"/>

    <xsl:template match="/">
        <xsl:apply-templates select="*">
            <xsl:with-param name="f" select="$filter/*"/>
        </xsl:apply-templates>
    </xsl:template>

    <xsl:template match="*">
        <xsl:param name="f"/>
        <xsl:choose>
            <xsl:when test="$f/*">
                <xsl:copy-of select="fn:node-name()"/>

                <!--
                <xsl:for-each select="*[fn:node-name(.) = $f/*/fn:node-name(.)]">
                    <xsl:apply-templates select=".">
                        <xsl:with-param name="f" select="f/*[fn:node-name() = current()/fn:node-name()]"/>
                    </xsl:apply-templates>
                </xsl:for-each>
                -->
            </xsl:when>
            <xsl:otherwise>
                <xsl:copy-of select="."/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

</xsl:stylesheet>    

谢谢大卫.这就是我真正想做的事情(它是递归的).使用 name() 我仍然得到错误 *Unexpected token [] in path expression*.

Thanks David. This is what I really want to make work (it is recursive). Using name() I still get error *Unexpected tocken [<function>] in path expression*.

在你之后

<xsl:stylesheet version="1.1" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:fn="http://www.w3.org/2005/xpath-functions">
<!--
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
-->        

    <xsl:output method="text" />
    <xsl:variable name="in" select="/"/>
    <xsl:variable name="filter" select="document('elementsToBeLeftIn.xml')"/>

    <xsl:template match="/">
        <xsl:apply-templates select="*">
            <xsl:with-param name="f" select="$filter/*"/>
        </xsl:apply-templates>
    </xsl:template>

    <xsl:template match="*">
        <xsl:param name="f"/>
        <xsl:choose>
            <xsl:when test="$f/*">
                <xsl:for-each select="*[name() = $f/*/name()]">
                    <xsl:apply-templates select=".">
                        <xsl:with-param name="f" select="f/*[name() = current()/name()]"/>
                    </xsl:apply-templates>
                </xsl:for-each>
            </xsl:when>
            <xsl:otherwise>
                <xsl:copy-of select="."/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

</xsl:stylesheet>    

推荐答案

即使在 XSLT2 中,您也不需要为像 node-name() 这样的标准函数添加前缀.但是您使用的是 XSLT1 的 saxon 6,因此您不能为函数添加前缀,否则它们将永远不会被识别.(XPath 1 标准函数不在命名空间中)

Even in XSLT2 you never need to prefix standard functions like node-name(). But you are using saxon 6 which is XSLT1 so you must not prefix the functions or they will never be recognised. (XPath 1 standard functions are not in a namespace)

只需使用 select="name()"

但是我认为您的代码不会按您的预期工作(但您没有说明您想要它做什么)但它只会将模板应用于一个元素(顶级文档元素),因为模板是从不递归应用.

However I don't think your code will work as you expect (but you didn't say what you wanted it to do) but it will only ever apply templates to one element (the top level document element) as templates are never recursively applied.

如果过滤器测试为真 <xsl:copy-of select="name()" 会输出该元素的名称,不带任何标记(因此结果将不是格式良好的 xml).

In the case that the filter test is true <xsl:copy-of select="name()" would put out the name of that element, with no markup (so the result will not be well formed xml).

如果过滤器测试为假,则整个文档元素包括其所有子元素都会被复制到输出中,并且不会进行进一步的处理.

In the case that the filter test is false the entire document element including all its children is copied to the output and no further processing takes place.

$f/*/name()

在 XPath2 中合法,但在 XPath 1 中不合法,其中使用 / 的路径表达式只能使用不以返回字符串的函数结尾的节点.不确定您想要做什么,因此无法立即更换.

is legal in XPath2 but not in XPath 1, where path expressions using / may only use nodes not end with a function that returns a string. Not sure exactly what you want to do so can't offer an immediate replacement.

 current()/name()

可以写成

 name(current())

在 XPath 1 中.

in XPath 1.

但是既然您使用的是 saxon java 实现,为什么不简单地使用 saxon 9 而不是 saxon 6 并从 xslt 引擎中十多年的进一步开发中受益?

But since you are using saxon java implementation why not simply use saxon 9 instead of saxon 6 and benefit from over a decade's worth of further development in the xslt engine?

这篇关于xpath-functions 不识别外部 Java 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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