XSLT count values in array

2 min read 20-09-2024
XSLT count values in array


In XML transformations, XSLT (Extensible Stylesheet Language Transformations) is a powerful tool that allows developers to convert XML data into various formats. One common requirement in XML processing is counting values in an array or a list. In this article, we'll demonstrate how to count values using XSLT, along with a practical example to illustrate the concept.

Problem Scenario

Let's consider the following XML input, which represents a list of items with their respective categories:

<items>
    <item>
        <name>Apple</name>
        <category>Fruit</category>
    </item>
    <item>
        <name>Carrot</name>
        <category>Vegetable</category>
    </item>
    <item>
        <name>Banana</name>
        <category>Fruit</category>
    </item>
    <item>
        <name>Broccoli</name>
        <category>Vegetable</category>
    </item>
</items>

The goal is to count how many items fall into each category.

Original Code

To achieve this counting functionality, the following XSLT code can be used:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" indent="yes"/>
    
    <xsl:key name="categoryKey" match="item" use="category"/>

    <xsl:template match="/">
        <categoryCount>
            <xsl:for-each select="items/item">
                <xsl:if test="generate-id() = generate-id(key('categoryKey', category)[1])">
                    <category>
                        <name><xsl:value-of select="category"/></name>
                        <count><xsl:value-of select="count(key('categoryKey', category))"/></count>
                    </category>
                </xsl:if>
            </xsl:for-each>
        </categoryCount>
    </xsl:template>
</xsl:stylesheet>

Explanation of the Code

  1. Key Declaration: The <xsl:key> element creates a key named categoryKey, which allows us to group items based on their categories. The match attribute specifies which element this key will match (in this case, item), and the use attribute defines that we will use the category element for grouping.

  2. Template Matching: The <xsl:template match="/"> defines the entry point of the transformation. Within this template, we use <xsl:for-each> to iterate over each item.

  3. Conditional Counting: Within the loop, an <xsl:if> condition checks whether the current item is the first item of its category by comparing their IDs. If true, it proceeds to create a <category> element that contains the category name and the count of items in that category.

  4. Counting Items: The <count> element utilizes the count function along with the key function to determine how many items belong to the current category.

Output

When you apply this XSLT to the provided XML, the output will look like this:

<categoryCount>
    <category>
        <name>Fruit</name>
        <count>2</count>
    </category>
    <category>
        <name>Vegetable</name>
        <count>2</count>
    </category>
</categoryCount>

Conclusion

Counting values in an array or list using XSLT is a straightforward process if you utilize the <xsl:key> and <xsl:for-each> constructs effectively. This approach is not only useful for counting but can also be adapted for other aggregations or data transformations.

Additional Resources

By mastering the techniques demonstrated in this article, you can leverage XSLT to efficiently analyze and manipulate XML data. Happy coding!