【问题标题】:Convert Node Set Values to String将节点集值转换为字符串
【发布时间】:2015-02-06 13:08:57
【问题描述】:

我正在尝试将一组节点值转换为字符串,以便我可以使用 XSLT 包含函数。下面的示例无法返回“true”并出现错误,因为 contains() 不处理 $allColors 的节点集,该节点集应包含“red white blue”。有谁知道我可以如何转换 $allColors 以便它可以在 contains 方法中使用?

<colors>
    <color type="rgb">red</color>
    <color type="rgb">white</color>
    <color type="rgb">blue</color>
</colors>

<xsl:for-each-group select="colors/color" group-by="@type">
     <xsl:variable name="allColors" select="current-group()/color"/>
     <allColors><xsl:value-of select="contains($allColors, 'red')"/></allColors>
</xsl:for-each-group>

【问题讨论】:

  • 为什么要使用 contains() 来测试节点集?如果其中一个节点包含“带红色”或“有色”,您想要一个肯定的答案吗?
  • 作为for-each-group 组color 元素,您的变量current-group()/color 选择一个空序列。

标签: xslt xslt-1.0 xslt-2.0


【解决方案1】:

我倾向于同意 michaels 的评论,但您可以使用以下内容,它应该可以为您提供所需的内容,并正确分组:

<xsl:for-each-group select="colors/color" group-by="@type">
    <group>
        <type><xsl:value-of select="@type" /></type>
        <hasRed><xsl:value-of select="current-group() = 'red'" /></hasRed>
    </group>
</xsl:for-each-group>

【讨论】:

    【解决方案2】:

    感谢大家的回复。我最终如何搜索是否存在特定颜色是 字符串长度(current-group()[color='red']/color) > 0.

    【讨论】:

    • 对。当它可能很复杂时,为什么要让它变得简单?
    猜你喜欢
    • 2013-01-14
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 2011-08-28
    • 1970-01-01
    • 1970-01-01
    • 2019-10-10
    相关资源
    最近更新 更多