首页 > 解决方案 > xslt 1.0 节点集错误,转换无效

问题描述

我在 1.0 版本中遇到了 xslt 的问题。我的节点看起来像这样

...
<Garage>
    <car>
       <color>red</color>
       <color>yellow</color>
       <wheel>left</wheel>
    <car/>
</Garage>
...

然后我将子节点保存到变量中

<xsl:variable name="entries">
    <xsl:if test="$element/Garage/car">
        <xsl:value-of select="$element/Garage/car"/>
    </xsl:if>
</xsl:variable>

当我在模板中使用这个变量时

    <xsl:template name="entriesToString">
        <xsl:param name="table"/>
        <xsl:for-each select="$table/color">
            <xsl:if test="position() = last()">
                <xsl:value-of select="concat(current(),'/')"/>
            </xsl:if>
            <xsl:value-of select="concat(current(),',')"/>
        </xsl:for-each>
    </xsl:template>

我收到这样的错误

ERROR:  'Invalid conversion from 'com.sun.org.apache.xalan.internal.xsltc.dom.SimpleResultTreeImpl' to 'node-set'.'

xslt 中的节点迭代有什么问题吗?

标签: xmlxsltxsdxslt-1.0xslt-2.0

解决方案


在 XSLT 1.0 中,该xsl:value-of指令创建一个文本节点,其值为所选节点集中第一个节点的字符串值。

在您的示例中,该$entries变量是一个包含文本的结果树片段"red yellow left"(带有或不带有一些空白字符)。为了使用指令处理结果树片段xsl:for-each,您必须首先使用特定于处理器的扩展函数将其转换为节点集。无论如何,在这种情况下它对您没有任何好处,因为 - 如上所述 - 它包含一个文本节点。


推荐阅读