XML technologies / XSLT / XSLT Cookbook / From XML to XML / Deepening an XML hierarchy
(Excerpt from “XSLT Kochbuch” by Sal Mangano)
Problem
Your poorly structured document needs more structure. It may suit other goals, but not yours.
Solution
This is the opposite of recipe Reducing an XML hierarchy. Add structure, perhaps organizing elements by further criteria.
Adding structure based on existing data
This reverses the previous recipe’s flattening transformation:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:import href="copy.xslt"/>
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="people">
<union>
<xsl:apply-templates select="person[@class = 'union']" />
</union>
<salaried>
<xsl:apply-templates select="person[@class = 'salaried']" />
</salaried>
</xsl:template>
</xsl:stylesheet>Adding structure to repair poor design
Some misguided attempts to simplify XML encode information with siblings instead of parents. To be fair, this is not always wrong: design balances competing drawbacks.
Suppose union and nonunion members are distinguished like this:
<people>
<class name="union"/>
<person>
<firstname>Warren</firstname>
<lastname>Rosenbaum</lastname>
<age>37</age>
<height>5.75</height>
</person>
...
<person>
<firstname>Theresa</firstname>
<lastname>Archul</lastname>
<age>37</age>
<height>5.5</height>
</person>
<class name="salaried"/>
<person>
<firstname>Sal</firstname>
<lastname>Mangano</lastname>
<age>37</age>
<height>5.75</height>
</person>
...
<person>
<firstname>James</firstname>
<lastname>O'Riely</lastname>
<age>33</age>
<height>5.5</height>
</person>
</people>Elements marking union and salaried-class elements are empty. All following siblings of an element class belong to its class until another element class or the end of siblings. This is easy to understand but harder for XSLT. Compute the set difference between all elements person following the first element class and the elements person following the next element class. XSLT 1.0 has no explicit set difference, but the same effect is achieved more efficiently by selecting elements after an element class whose positions precede those after the next element class:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:import href="copy.xslt"/>
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- Gesamtanzahl an Leuten -->
<xsl:variable name="num-people" select="count(/*/person)"/>
<xsl:template match="class">
<!--Die letzte Position, die wir betrachten wollen. -->
<xsl:variable name="pos" select="$num-people - count(following-sibling::class/following-sibling::person)"/>
<xsl:element name="{@name}">
<!-- Kopieren der Leute, die dieser Klasse folgen, deren Position aber kleiner oder gleich $pos ist.-->
<xsl:copy-of select="following-sibling::person[position( ) <= $pos]"/>
</xsl:element>
</xsl:template>
<!-- Ignorieren der person-Elemente. Sie wurden oben kopiert. -->
<xsl:template match="person"/>
</xsl:stylesheet>A key is another option:
<xsl:key name="people" match="person" use="preceding-sibling::class[1]/@name" />
<xsl:template match="people">
<people>
<xsl:apply-templates select="class" />
</people>
</xsl:template>
<xsl:template match="class">
<xsl:element name="{@name}">
<xsl:copy-of select="key('people', @name)" />
</xsl:element>
</xsl:template>A stepwise approach is also possible:
<xsl:template match="people">
<people>
<xsl:apply-templates select="class[1]" />
</people>
</xsl:template>
<xsl:template match="class">
<xsl:element name="{@name}">
<xsl:apply-templates select="following-sibling::*[1][self::person]" />
</xsl:element>
<xsl:apply-templates select="following-sibling::class[1]" />
</xsl:template>
<xsl:template match="person">
<xsl:copy-of select="." />
<xsl:apply-templates select="following-sibling::*[1][self::person]" />
</xsl:template>XSLT 2.0
Adding structure based on existing data
Using XSLT 2.0 function xsl:for-each-group gives a more generic solution than XSLT 1.0. Generic 1.0 solutions exist, as discussed below, but none is as simple:
<xsl:template match="people">
<xsl:for-each-group select="person" group-by="preceding-sibling::class[1]/@name">
<xsl:element name="{curent-grouping-key( )}">
<xsl:apply-templates select="current-group( )" />
</xsl:element>
</xsl:for-each>
</xsl:template>Adding structure to repair poor design
Use xsl:for-each-group with option group-starting-with to solve this:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:import href="copy.xslt"/>
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="people">
<xsl:copy>
<xsl:for-each-group select="*" group-starting-with="class">
<xsl:element name="{@name}">
<xsl:apply-templates select="current-group( )[not(self::class)]"/>
</xsl:element>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>Discussion
Adding structure based on existing data
The earlier solution explicitly named category criteria, such as union and salaried. Ideally the stylesheet discovers them itself, becoming more complex but generic:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:import href="copy.xslt"/>
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- Aufbauen einer eindeutigen Liste aller Klassen -->
<xsl:variable name="classes" select="/*/*/@class[not(. = ../preceding-sibling::*/@class)]"/>
<xsl:template match="/*">
<!-- Für jede Klasse wird ein Element erzeugt, das nach der Klasse benannt wird, die Elemente dieser Klasse enthält -->
<xsl:for-each select="$classes">
<xsl:variable name="class-name" select="."/>
<xsl:element name="{$class-name}">
<xsl:for-each select="/*/*[@class=$class-name]">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:for-each>
</xsl:element>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>This is not fully generic, but avoids assumptions about category types. Its only application-specific information is that categories are encoded in attribute @class on elements two levels below the root.
Adding structure to repair poor design
An explicit set-difference solution is elegant but impractical for large documents with many categories. Its set-difference technique is described in recipe Performing set operations on node sets:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:import href="copy.xslt"/>
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="class">
<!--Alle Leute, die diesem class-Element folgen -->
<xsl:variable name="nodes1" select="following-sibling::person"/>
<!--Alle Leute, die dem nächsten class-Element folgen -->
<xsl:variable name="nodes2" select="following-sibling::class/following-sibling::person"/>
<xsl:element name="{@name}">
<xsl:copy-of select="$nodes1[count(. | $nodes2) != count($nodes2)]"/>
</xsl:element>
</xsl:template>
<xsl:template match="person"/>
</xsl:stylesheet>
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