This is actually a good example of a grouping problem. In XSLT1.0, the most efficient way to group is the Muenchian Grouping method, so it would be useful to know about it.
In this case, you want to group Passenger elements by their @type attribute, so you must define a key for this
<xsl:key name="Passengers" match="Passenger" use="@type"/>
Then you need to select the Passenger elements, which are the first occurrence of this element in the group for their @type attribute. This is done as follows:
<xsl:apply-templates select="Passenger[generate-id() = generate-id(key('Passengers', @type)[1])]"/>
Note the use of generate-id , which generates a unique identifier for node, allowing you to compare two nodes.
Then, to count the number of occurrences in a group, this is straightforward
<xsl:value-of select="count(key('Passengers', @type))"/>
Here is the full XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/> <xsl:key name="Passengers" match="Passenger" use="@type"/> <xsl:template match="Passengers"> <Pax_Items> <xsl:apply-templates select="Passenger[generate-id() = generate-id(key('Passengers', @type)[1])]"/> </Pax_Items> </xsl:template> <xsl:template match="Passenger"> <Item> <Type> <xsl:value-of select="@type"/> </Type> <Count> <xsl:value-of select="count(key('Passengers', @type))"/> </Count> </Item> </xsl:template> </xsl:stylesheet>
When applied to your sample XML, the following is output
<Pax_Items> <Item> <Type>A</Type> <Count>2</Count> </Item> <Item> <Type>B</Type> <Count>1</Count> </Item> <Item> <Type>C</Type> <Count>1</Count> </Item> </Pax_Items>
Also note that there is no real reason to use xsl: element to display static elements. Just write the item directly.