2

I have input XML:

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <FT>Paket</FT>
   <FT>Parti</FT>
   <FT>Paket</FT>
   <FT>Styche</FT>
   <FT>Styche</FT>
</root>

And I want my output to display such as -

Paket   2
Parti   1
Styche  2

Its is grouping the value of elements and the no. is showing the total count of the value being repeated. Like Paket is indicating the value and it is being repeated 2 times in the XML.

How the logic will work?

Kirill Polishchuk
  • 54,804
  • 11
  • 122
  • 125
Kundan
  • 153
  • 1
  • 5
  • 15
  • I cannot write the code here as it is an image output. I tried with group and count functions, but not receiving the result which I want. – Kundan Nov 07 '13 at 05:14

1 Answers1

2

In XSLT 1.0, using Muenchian grouping:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" indent="yes"/>
  <xsl:key name="k" match="FT" use="."/>

  <xsl:template match="/*">
    <xsl:apply-templates select="FT[generate-id() = generate-id(key('k', .))]"/>
  </xsl:template>

  <xsl:template match="FT">
    <xsl:value-of select="concat(., ' ', count(key('k', .)))"/>
    <xsl:text>&#xa;</xsl:text>
  </xsl:template>

</xsl:stylesheet>

Output:

Paket 2
Parti 1
Styche 2
Kirill Polishchuk
  • 54,804
  • 11
  • 122
  • 125
  • Thanks for the answer. I want to apply the above logic without templates. If that can be done. Though I applied , it genearted the Paket, Parti... but when I added , it didn't went to the next line. Also, didn't worked. Because I didn't applied template to it. – Kundan Nov 07 '13 at 05:49