【问题标题】:Count unique values in comma separated value in xslt 1.0在 xslt 1.0 中计算逗号分隔值中的唯一值
【发布时间】:2015-09-08 08:48:57
【问题描述】:

我在 XML 文件中有一个节点:

<TEST_STRING>12,13,12,14</TEST_STRING>

我需要计算这个字符串有多少个唯一数字/值。例如,在这种情况下,有 2 个唯一值,即 13 和 14。

老实说,我还无法构建任何东西。在 XSLT 1.0 中似乎很难,但我的系统只支持 1.0。

有解决办法吗?

【问题讨论】:

  • 您具体使用的是哪种 XSLT 1.0 处理器?

标签: xslt xpath xslt-1.0


【解决方案1】:

如果您的处理器支持 node-set 扩展函数(exslt 或 Microsoft msxsl one),那么您可以分两步完成,首先拆分字符串并构建一个每个值一个元素的 XML 片段,然后使用普通的 XPath 技术来查找单例。

可以使用尾递归模板完成第一步:

<xsl:template name="splitString">
  <xsl:param name="str"/>
  <xsl:choose>
    <xsl:when test="contains($str, ',')">
      <item><xsl:value-of select="substring-before($str, ',')"/></item>
      <xsl:call-template name="splitString">
        <xsl:with-param name="str" select="substring-after($str, ',')"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <item><xsl:value-of select="$str"/></item>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

在适当的地方你可以这样称呼

<xsl:variable name="itemsRTF">
  <xsl:call-template name="splitString">
    <xsl:with-param name="str" select="TEST_STRING"/>
  </xsl:call-template>
</xsl:variable>
<xsl:variable name="items" select="exsl:node-set($itemsRTF)"/>

items 变量现在包含类似 XML 的片段

<item>12</item>
<item>13</item>
<item>12</item>
<item>14</item>

下一个挑战是找到单例,你可以用这样的表达式来完成

$items/item[not(. = (preceding-sibling::item | following-sibling::item))]

(使用 key 有更有效的方法,但对于少量项目可能不值得费心)。所以要计算单身人士

<xsl:value-of select="count($items/item[not(. = (preceding-sibling::item | following-sibling::item))])"/>

【讨论】:

    【解决方案2】:

    没有节点集是可能的。当然,这不是最优的(你好,画家 Shlemiel),但它非常简单 - 仅当前面或后面没有给定字符串时才增加计数器。

    模板

    <xsl:template name="calcUnique">
        <xsl:param name="str"/>
        <xsl:param name="back"/>
        <xsl:param name="count"/>
    
        <xsl:if test="$str">
            <xsl:choose>
                <xsl:when test="contains($str, ',')">
                    <xsl:variable name="part" select="substring-before($str, ',')"/>
                    <xsl:call-template name="calcUnique">
                        <xsl:with-param name="str" select="substring-after($str, ',')"/>
                        <xsl:with-param name="back" select="concat($back, ',', $part)"/>
                        <xsl:with-param name="count">
                            <xsl:choose>
                                <xsl:when test="contains(concat($str, ','), concat(',', $part, ',')) or contains(concat($back, ','), concat(',', $part, ','))">
                                    <xsl:value-of select="$count"/>
                                </xsl:when>
                                <xsl:otherwise><xsl:value-of select="$count + 1"/></xsl:otherwise>
                            </xsl:choose>
                        </xsl:with-param>
                    </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:choose>
                        <xsl:when test="contains(concat($back, ','), concat(',', $str, ','))">
                            <xsl:value-of select="$count"/>
                        </xsl:when>
                        <xsl:otherwise><xsl:value-of select="$count + 1"/></xsl:otherwise>
                    </xsl:choose>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:if>
    </xsl:template>
    

    使用示例

        <xsl:variable name="result">
            <xsl:call-template name="calc">
                <xsl:with-param name="str" select="TEST_STRING"/>
                <xsl:with-param name="back" select="''"/>
                <xsl:with-param name="count" select="0"/>
            </xsl:call-template>
        </xsl:variable>
        <xsl:value-of select="$result"/>
    

    【讨论】:

    • 太好了,我会尝试两种解决方案,有和没有节点集。我会回来找你的。
    • 是的,我的错。固定。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多