XML technologies / XSLT / XSLT Kochbuch / From XML to XML / Flattening an XML hierarchy

(Excerpt from "XSLT Kochbuch" by Sal Mangano)

Problem

Your document contains elements nested more deeply than you would like, so you want to flatten the tree.

Solution

If you simply want to flatten the structure without retaining the information encoded by the deeper levels, apply an overriding copy. The overriding template must match the elements to discard and apply templates without copying them.

Consider the following input, which divides people into two categories: unionized employees (»union«) and non-unionized employees (»salaried«):

<people>
  <union>
    <person>
      <firstname>Warren</firstname>
      <lastname>Rosenbaum</lastname>
      <age>37</age>
      <height>5.75</height>
    </person>
    <person>
      <firstname>Dror</firstname>
      <lastname>Seagull</lastname>
      <age>28</age>
      <height>5.10</height>
    </person>
    <person>
      <firstname>Mike</firstname>
      <lastname>Heavyman</lastname>
      <age>45</age>
      <height>6.0</height>
    </person>
    <person>
      <firstname>Theresa</firstname>
      <lastname>Archul</lastname>
      <age>37</age>
      <height>5.5</height>
    </person>
  </union>
  <salaried>
    <person>
      <firstname>Sal</firstname>
      <lastname>Mangano</lastname>
      <age>37</age>
      <height>5.75</height>
    </person>
    <person>
      <firstname>Jane</firstname>
      <lastname>Smith</lastname>
      <age>28</age>
      <height>5.10</height>
    </person>
    <person>
      <firstname>Rick</firstname>
      <lastname>Winters</lastname>
      <age>45</age>
      <height>6.0</height>
    </person>
    <person>
      <firstname>James</firstname>
      <lastname>O'Riely</lastname>
      <age>33</age>
      <height>5.5</height>
    </person>
  </salaried>
</people>

This stylesheet simply discards the extra structure:

<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"/>
  <xsl:template match="people">
    <xsl:copy>
      <!--Elternelemente der person-Elemente verwerfen -->
      <xsl:apply-templates select="*/person" />
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

Discussion

Additional structure in a document is generally useful because it usually makes XSLT processing easier. Too much structure, however, bloats the document and makes it harder to understand. People generally prefer to discern relationships through text organization rather than extra syntactic baggage.

The following example shows that the extra structure is not redundant but encodes additional information. If you want to preserve that information while flattening the structure, consider creating an attribute or child element to hold it.

This stylesheet creates an attribute:

<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" omit-xml-declaration="yes"/>
  <!--Verwerfen der Elternelemente der person-Elemente -->
  <xsl:template match="*[person]">
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="person">
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:attribute name="class">
        <xsl:value-of select="local-name(..)"/>
      </xsl:attribute>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

This variant creates an element:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:import href="copy.xslt"/>
  <xsl:strip-space elements="*"/>
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
  <!--Verwerfen der Elternelemente der person-Elemente -->
  <xsl:template match="*[person]">
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="person">
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:element name="class">
        <xsl:value-of select="local-name(..)"/>
      </xsl:element>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

You can use xsl:strip-space and indent="yes" on the xsl:output element to prevent a whitespace gap in the output, as shown here:

<people>
  ...
  <person>
    <class>union</class>
    <firstname>Warren</firstname>
    <lastname>Rosenbaum</lastname>
    <age>37</age>
    <height>5.75</height>
   </person>
                                      <!-- Hier ist die Whitespace-Lücke! -->

  <person>
    <class>salaried</class>
    <firstname>Sal</firstname>
    <lastname>Mangano</lastname>
    <age>37</age>
    <height>5.75</height>
  </person>
  ...
</people>

  

 

 

 

back to top