XML technologies / XSLT / XSLT Cookbook / From XML to XML / Reorganizing an XML hierarchy
(Excerpt from “XSLT Kochbuch” by Sal Mangano)
Problem
You need to reorganize information in an XML document, making some implicit information explicit and some explicit information implicit.
Solution
XSLT 1.0
Again, consider document SalesBySalesPerson.xml from Selecting and traversing:
<salesBySalesperson>
<salesperson name="John Adams" seniority="1">
<product sku="10000" totalSales="10000.00"/>
<product sku="20000" totalSales="50000.00"/>
<product sku="25000" totalSales="920000.00"/>
</salesperson>
<salesperson name="Wendy Long" seniority="5">
<product sku="10000" totalSales="990000.00"/>
<product sku="20000" totalSales="150000.00"/>
<product sku="30000" totalSales="5500.00"/>
</salesperson>
<salesperson name="Willie B. Aggressive" seniority="10">
<product sku="10000" totalSales="1110000.00"/>
<product sku="20000" totalSales="150000.00"/>
<product sku="25000" totalSales="2920000.00"/>
<product sku="30000" totalSales="115500.00"/>
<product sku="70000" totalSales="10000.00"/>
</salesperson>
<salesperson name="Arty Outtolunch" seniority="10"/>
</salesBySalesperson>The document explicitly shows which salesperson sells which products and how much each earns for each product sold. Total income by product is implicit, as are the names of all salespeople who sold a particular product.
Reorganizing this document therefore means transforming it into a view showing sales by product. The following stylesheet performs that transformation:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:key name="sales_key" match="salesperson" use="product/@sku"/>
<xsl:variable name="products" select="//product"/>
<xsl:variable name="unique-products" select="$products[not(@sku = preceding::product/@sku)]"/>
<xsl:template match="/">
<salesByProduct>
<xsl:for-each select="$unique-products">
<xsl:variable name="sku" select="@sku"/>
<xsl:copy>
<xsl:copy-of select="$sku"/>
<xsl:attribute name="totalSales">
<xsl:value-of select="sum($products[@sku=$sku]/@totalSales)"/>
</xsl:attribute>
<xsl:for-each select="key('sales_key',$sku)">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:attribute name="sold">
<xsl:value-of select="product[@sku=$sku]/@totalSales"/>
</xsl:attribute>
</xsl:copy>
</xsl:for-each>
</xsl:copy>
</xsl:for-each>
</salesByProduct>
</xsl:template>
</xsl:stylesheet>The resulting output is shown here:
<salesByProduct>
<product sku="10000" totalSales="2110000">
<salesperson name="John Adams" seniority="1" sold="10000.00"/>
<salesperson name="Wendy Long" seniority="5" sold="990000.00"/>
<salesperson name="Willie B. Aggressive" seniority="10" sold="1110000.00"/>
</product>
<product sku="20000" totalSales="350000">
<salesperson name="John Adams" seniority="1" sold="50000.00"/>
<salesperson name="Wendy Long" seniority="5" sold="150000.00"/>
<salesperson name="Willie B. Aggressive" seniority="10" sold="150000.00"/>
</product>
<product sku="25000" totalSales="3840000">
<salesperson name="John Adams" seniority="1" sold="920000.00"/>
<salesperson name="Willie B. Aggressive" seniority="10" sold="2920000.00"/>
</product>
<product sku="30000" totalSales="121000">
<salesperson name="Wendy Long" seniority="5" sold="5500.00"/>
<salesperson name="Willie B. Aggressive" seniority="10" sold="115500.00"/>
</product>
<product sku="70000" totalSales="10000">
<salesperson name="Willie B. Aggressive" seniority="10" sold="10000.00"/>
</product>
</salesByProduct>An alternative solution uses the Muenchian method, named after Steve Muench. It uses an xsl:key to extract unique products. Expression $products[count(.|key('product_key', @sku)[1]) = 1] selects the first product in the group defined by sku:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:variable name="doc" select="/"/>
<xsl:key name="product_key" match="product" use="@sku"/>
<xsl:key name="sales_key" match="salesperson" use="product/@sku"/>
<xsl:variable name="products" select="//product"/>
<xsl:template match="/">
<salesByProduct>
<xsl:for-each select="$products[count(.|key('product_key',@sku)[1]) = 1]">
<xsl:variable name="sku" select="@sku"/>
<xsl:copy>
<xsl:copy-of select="$sku"/>
<xsl:attribute name="totalSales">
<xsl:value-of select="sum(key('product_key',$sku)/@totalSales)"/>
</xsl:attribute>
<xsl:for-each select="key('sales_key',$sku)">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:attribute name="sold">
<xsl:value-of select="product[@sku=$sku]/@totalSales"/>
</xsl:attribute>
</xsl:copy>
</xsl:for-each>
</xsl:copy>
</xsl:for-each>
</salesByProduct>
</xsl:template>
</xsl:stylesheet>XSLT 2.0
This problem is easy to solve in XSLT 2.0 because you can use xsl:for-each-group:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<salesByProduct>
<!-- Gruppieren der Produkte nach sku -->
<xsl:for-each-group select="//product" group-by="@sku">
<xsl:copy>
<xsl:copy-of select="@sku"/>
<!-- current-group( ) verwenden, um die Verkäufe zusammenzurechnen -->
<xsl:attribute name="totalSales" select="format-number(sum(current-group( )/@totalSales),'#')"/>
<!-- Kopieren von salesperson-Elementen, die ein Kind-Produkt mit dem sku der aktuellen Produktgruppe enthalten -->
<xsl:for-each select="/*/salesperson">
<xsl:if test="product[@sku eq current-grouping-key( )]">
<xsl:copy>
<xsl:copy-of select="@*"/>
</xsl:copy>
</xsl:if>
</xsl:for-each>
</xsl:copy>
</xsl:for-each-group>
</salesByProduct>
</xsl:template>
</xsl:stylesheet>Discussion
XSLT 1.0
The solution is a highly application-specific example. That cannot be avoided: presenting a generic reorganization stylesheet is difficult, if not impossible, because the kind of reorganization depends on the nature of the documents being transformed.
Nevertheless, some common idioms recur in these reorganizations.
First, since you reorganize the entire document tree, a solution based mainly on matching and applying templates is unlikely. These stylesheets are more likely to use an iterative style. In other words, solutions will probably rely heavily on xsl:for-each.
Second, these recipes almost always initialize global variables containing elements extracted from deep in the XML structure. You will probably also need a unique subset of those elements. Recipe Selecting nodes by context fully describes techniques for constructing unique sets of elements.
Third, reorganization often involves aggregating data again using sums, products or more complex operations. In Numbers and calculations I discuss techniques for computing these aggregates.
XSLT 2.0
The power of xsl:for-each-group certainly makes XML reorganization much easier. The key is to understand exactly what defines the group, then reorganize the other elements according to their relationship to it.
See also
Recipe Preferring for-each-group to Muenchian grouping provides further examples using the XSLT 2.0 instruction for-each-group.
Copyright © 2006 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from “XSLT Kochbuch” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O'Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de