简体   繁体   English

xslt 1.0 节点集错误,转换无效

[英]xslt 1.0 node-set error with Invalid conversion

I have a problem with xslt in 1.0 version.我在 1.0 版本中遇到了 xslt 的问题。 My node looks like this我的节点看起来像这样

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

Then I save child node into variable然后我将子节点保存到变量中

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

and when I'am using this variable in template当我在模板中使用这个变量时

    <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>

I get error like this我收到这样的错误

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

Is there any problem with node iteration in xslt? xslt中的节点迭代有问题吗?

In XSLT 1.0, the xsl:value-of instruction creates a text node whose value is the string-value of the first node in the selected node-set.在 XSLT 1.0 中, xsl:value-of指令创建一个文本节点,其值为所选节点集中第一个节点的 字符串值

In your example, the $entries variable is a result-tree-fragment that contains the text "red yellow left" (with or without some white-space characters).在您的示例中, $entries变量是一个结果树片段,其中包含文本"red yellow left" (带有或不带有一些空白字符)。 In order to process a result-tree-fragment using the xsl:for-each instruction, you must first convert it to a node-set, using a processor-specific extension function.为了使用xsl:for-each指令处理结果树片段,您必须首先使用特定于处理器的扩展 function 将其转换为节点集。 In any case, it wouldn't do you any good in this case, since - as explained above - it contains a single text node.无论如何,在这种情况下它对您没有任何好处,因为 - 如上所述 - 它包含一个文本节点。

It might suffice to use <xsl:variable name="entries" select="$element/Garage/car"/> , that way you have a node-set in XPath/XSLT 1, without the need to use an extension function.使用<xsl:variable name="entries" select="$element/Garage/car"/>可能就足够了,这样您就可以在 XPath/XSLT 1 中拥有一个节点集,而无需使用扩展 function。 The node-set is empty if the path $element/Garage/car doesn't select anything, it contains car element nodes from $element if they exist, ie the path $element/Garage/car selects something.如果路径$element/Garage/car没有 select 任何内容,则节点集为空,它包含来自$elementcar元素节点(如果存在),即路径$element/Garage/car选择了一些东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM