XSLT不同选择/组由(Xslt distinct select / Group by)

2019-06-23 14:11发布

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

我这样做的票价是要经过所有DISTICT frontendGroupIds。 我想现在要做的就是做的所有DISTICT caseIds每个DISTICT frontendGroupId的计数,但我似乎无法使这项工作。 有人可以帮助我在这里PLZ?

Answer 1:

你接近:

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

这导致:

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>

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

2336 - 1
2337 - 2

核心表达式:

count(
  key('kStatisticItemByGroup', @frontendGroupId)[
    count(
      . | key('kStatisticItemByGroupAndCase', concat(@frontendGroupId, ',', @caseId))[1]
    ) = 1
  ]
)

归结为:

从伯爵“的节点key('kStatisticItemByGroup', @frontendGroupId) ”是满足以下条件:他们在各自的“第一kStatisticItemByGroupAndCase ”组。

仔细观察,你会发现,这是没有比你已经做的更加复杂。 :-)


编辑:最后一个提示。 就个人而言,我觉得这是一个很多比上述表述的表现,因为它强调平等节点很多不是更“ count(.|something) = 1 ”的方法:

count(
  key('kStatisticItemByGroup', @frontendGroupId)[
    generate-id()
    =
    generate-id(
      key('kStatisticItemByGroupAndCase', concat(@frontendGroupId, ',', @caseId))[1]
    )
  ]
)

结果是一样的。



Answer 2:

您正尝试通过可怕的“MUENCHIAN”的方法排序 - 这是我发现如此混乱,是不是值得尝试 - 所以我制定出我自己的方法。

它采用了两个转变,而不是一个。

首先对数据进行排序到正确的顺序根据您的分组的要求 - 你的样本数据已经在正确的顺序,所以我会离开它的这种解释(问,如果你想在这里帮助)

第二个转型做分组简单地通过一个节点比较了下:

    <?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
    <xsl:output method="xml" indent="yes"/>


    <xsl:template match="statisticItems">
        <groupedItem>
            <xsl:apply-templates select="statisticItem"></xsl:apply-templates>
        </groupedItem>
    </xsl:template>

    <xsl:template match="statisticItem">
        <xsl:choose>

            <xsl:when test="position()=1">
                <xsl:apply-templates select="@frontendGroupId" />
            </xsl:when>

            <xsl:when test="@frontendGroupId!=preceding-sibling::statisticItem[1]/@frontendGroupId">
                <xsl:apply-templates select="@frontendGroupId" />
            </xsl:when>

        </xsl:choose>

        <xsl:apply-templates select="@caseId" />


    </xsl:template>

<xsl:template match="@frontendGroupId">
    <group>
        <xsl:variable name="id" select="." ></xsl:variable>
        <xsl:attribute name="count">
            <xsl:value-of select="count(//statisticItem/@frontendGroupId[.=$id])"/>
        </xsl:attribute>        
        <xsl:value-of select="." />
    </group>
</xsl:template>

    <xsl:template match="@caseId">
        <value>
            <xsl:value-of select="." />
        </value>
    </xsl:template>

</xsl:stylesheet>

使用这种方法,你可以去几组深,仍然有维护的代码。



文章来源: Xslt distinct select / Group by
标签: xslt