Xslt distinct select/Group by

1 xslt

<statisticItems>
    <statisticItem id="1" frontendGroupId="2336" caseId="50264"  />
    <statisticItem id="2" frontendGroupId="2336" caseId="50264"  />
    <statisticItem id="3" frontendGroupId="2337" caseId="50265"  />
    <statisticItem id="4" frontendGroupId="2337" caseId="50266"  />
    <statisticItem id="5" frontendGroupId="2337" caseId="50266"  />
</statisticItems>

<?xml version="1.0" encoding="utf-8"?>
  <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" omit-xml-declaration="yes"/>

  <xsl:key name="statistic-by-frontendGroupId" match="statisticItem" use="@frontendGroupId" />

  <xsl:for-each select="statisticItems/statisticItem[count(.|key('statistic-by-frontendGroupId', @frontendGroupId)[1]) = 1]">
       <xsl:value-of select="@frontendGroupId"/>
  </xsl:for-each>
Run Code Online (Sandbox Code Playgroud)

我所做的这样的票价是通过所有distict frontendGroupIds.我现在要做的是对每个distist frontendGroupId进行所有distict caseId的计数,但我似乎无法做到这一点.有人可以帮我这里吗?

Tom*_*lak 6

你很亲密:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="text" />

  <xsl:key 
    name="statistic-by-frontendGroupId" 
    match="statisticItem" 
    use="@frontendGroupId" 
  />

  <xsl:template match="statisticItems">
    <xsl:for-each select="
      statisticItem[
        count(
          . | key('statistic-by-frontendGroupId', @frontendGroupId)[1]
        ) = 1
      ]
    ">
      <xsl:value-of select="@frontendGroupId"/>
      <xsl:value-of select="' - '"/>
      <!-- simple: the item count is the node count of the key -->
      <xsl:value-of select="
        count(
          key('statistic-by-frontendGroupId', @frontendGroupId)
        )
      "/>
      <xsl:value-of select="'&#10;'"/>
    </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

这导致:

2336 - 2
2337 - 3

编辑 - 哦,我看到你想要团队中的独特计数.这将是:

<!-- the other key from the above solution is still defined -->

<xsl:key 
  name="kStatisticItemByGroupAndCase" 
  match="statisticItem" 
  use="concat(@frontendGroupId, ',', @caseId)"
/>

<xsl:template match="statisticItems">
  <xsl:for-each select="
    statisticItem[
      count(
        . | key('kStatisticItemByGroup', @frontendGroupId)[1]
      ) = 1
    ]
  ">
    <xsl:value-of select="@frontendGroupId"/>
    <xsl:value-of select="' - '"/>
    <xsl:value-of select="
      count(
        key('kStatisticItemByGroup', @frontendGroupId)[
          count(
            . | key('kStatisticItemByGroupAndCase', concat(@frontendGroupId, ',', @caseId))[1]
          ) = 1
        ]
      )
    "/>
    <xsl:value-of select="'&#10;'"/>
  </xsl:for-each>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)

这看起来(不可否认)有点可怕.它输出:

2336 - 1
2337 - 2

核心表达:

count(
  key('kStatisticItemByGroup', @frontendGroupId)[
    count(
      . | key('kStatisticItemByGroupAndCase', concat(@frontendGroupId, ',', @caseId))[1]
    ) = 1
  ]
)
Run Code Online (Sandbox Code Playgroud)

归结为:

key('kStatisticItemByGroup', @frontendGroupId)满足以下条件的" " 计算节点:它们是各自" kStatisticItemByGroupAndCase"组中的第一个.

仔细观察,您会发现这并不比您已经做过的复杂.:-)


编辑:最后一个提示.就个人而言,我发现它比上述表达更具表现力,因为它强调节点平等比" count(.|something) = 1"方法更多:

count(
  key('kStatisticItemByGroup', @frontendGroupId)[
    generate-id()
    =
    generate-id(
      key('kStatisticItemByGroupAndCase', concat(@frontendGroupId, ',', @caseId))[1]
    )
  ]
)
Run Code Online (Sandbox Code Playgroud)

结果是一样的.