1

I am trying to extract unique values from an XML and how many times they occur.

I have been following the answer given in Xslt distinct select / Group by but my schema is a little different.

My XML looks something similar to:

<A>
    <B>
        <C>
            <D>APPLE</D>
        </C>
    </B>
    <B>
        <C>
            <D>BANANA</D>
        </C>
    </B>
    <B>
        <C>
            <D>APPLE</D>
        </C>
    </B>
</A>

Based on the code in the previous answer I have:

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

  <xsl:key 
    name="C-by-DValue" 
    match="B/C/D" 
    use="text()" 
  />

  <xsl:template match="A">
    <xsl:for-each select="
      B/C/D[
        count(
          . | key('C-by-DValue', B/C/D/text())[1]
        ) = 1
      ]
    ">
      <xsl:value-of select="text()"/>
      <xsl:value-of select="' - '"/>
      <!-- simple: the item count is the node count of the key -->
      <xsl:value-of select="
        count(
          key('C-by-DValue', text())
        )
      "/>
      <xsl:value-of select="'&#10;'"/>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

But this returns:

APPLE - 2
BANANA - 1
APPLE - 2

So the for-each-select isn't only matching the first instance of each text() value. Could someone point me in the right direction please.

Community
  • 1
  • 1
George
  • 1,036
  • 2
  • 11
  • 23

2 Answers2

4

You want to change

<xsl:template match="A">
    <xsl:for-each select="
      B/C/D[
        count(
          . | key('C-by-DValue', B/C/D/text())[1]
        ) = 1
      ]
    ">
      <xsl:value-of select="text()"/>
      <xsl:value-of select="' - '"/>
      <!-- simple: the item count is the node count of the key -->
      <xsl:value-of select="
        count(
          key('C-by-DValue', text())
        )
      "/>
      <xsl:value-of select="'&#10;'"/>
    </xsl:for-each>
  </xsl:template>

to

  <xsl:template match="A">
    <xsl:for-each select="
      B/C/D[
        count(
          . | key('C-by-DValue',.)[1]
        ) = 1
      ]
    ">
      <xsl:value-of select="text()"/>
      <xsl:value-of select="' - '"/>
      <!-- simple: the item count is the node count of the key -->
      <xsl:value-of select="
        count(
          key('C-by-DValue', text())
        )
      "/>
      <xsl:value-of select="'&#10;'"/>
    </xsl:for-each>
Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
2

It is possible to accomplish this grouping task in a much shorter way, never using xsl:for-each .

This transformation:

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

 <xsl:key name="kDByVal" match="D" use="."/>

 <xsl:template match="D[generate-id()=generate-id(key('kDByVal', .)[1])]">
  <xsl:value-of select=
   "concat(., ' - ', count(key('kDByVal', .)), '&#xA;')"/>
 </xsl:template>
 <xsl:template match="text()"/>
</xsl:stylesheet>

when applied on the provided XML document:

<A>
    <B>
        <C>
            <D>APPLE</D>
        </C>
    </B>
    <B>
        <C>
            <D>BANANA</D>
        </C>
    </B>
    <B>
        <C>
            <D>APPLE</D>
        </C>
    </B>
</A>

produces the wanted, correct result:

APPLE - 2
BANANA - 1
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431