XML technologies / XSLT / XSLT Kochbuch / From XML to XML / Converting attributes into elements
(Excerpt from "XSLT Kochbuch" by Sal Mangano)
Problem
You have a document that encodes information in attributes, but would prefer to use child elements instead.
Solution
XSLT 1.0
This problem is ideally suited to the technique mentioned in this chapter’s introduction: copying with overrides. This example transforms all attributes into elements:
<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:template match="@*">
<xsl:element name="{local-name(.)}" namespace="{namespace-uri(..)}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>The stylesheet works by overriding the copying behaviour for attributes. It replaces that behaviour with a template that converts an attribute into an element of the same name whose content is the attribute value. It also assumes that this new element should belong to the same namespace as the attribute’s parent element. If you prefer to make no assumptions, use the following code:
<xsl:template match="@*">
<xsl:variable name="namespace">
<xsl:choose>
<!--Verwendet den Namensraum des Attributs, falls es einen gibt -->
<xsl:when test="namespace-uri( )">
<xsl:value-of select="namespace-uri( )" />
</xsl:when>
<!--Ansonsten wird der Namensraum des Elternelements verwendet -->
<xsl:otherwise>
<xsl:value-of select="namespace-uri(..)" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:element name="{name( )}" namespace="{$namespace}">
<xsl:value-of select="." />
</xsl:element>
</xsl:template>You will often want to be selective about which attributes to convert into elements (see the following three examples).
Example: input.
<people which="MeAndMyFriends">
<person firstname="Sal" lastname="Mangano" age="38" height="5.75"/>
<person firstname="Mike" lastname="Palmieri" age="28" height="5.10"/>
<person firstname="Vito" lastname="Palmieri" age="38" height="6.0"/>
<person firstname="Vinny" lastname="Mari" age="37" height="5.8"/>
</people>Example: a stylesheet that transforms only person attributes.
<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:template match="person/@*">
<xsl:element name="{local-name(.)}" namespace="{namespace-uri(..)}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>Example: output.
<people which="MeAndMyFriends">
<person>
<firstname>Sal</firstname>
<lastname>Mangano</lastname>
<age>38</age>
<height>5.75</height>
</person>
<person>
<firstname>Mike</firstname>
<lastname>Palmieri</lastname>
<age>28</age>
<height>5.10</height>
</person>
<person>
<firstname>Vito</firstname>
<lastname>Palmieri</lastname>
<age>38</age>
<height>6.0</height>
</person>
<person>
<firstname>Vinny</firstname>
<lastname>Mari</lastname>
<age>37</age>
<height>5.8</height>
</person>
</people>XSLT 2.0
In XSLT 2.0 the solution can be made more concise, while the recipe remains essentially the same. Here you can replace the cumbersome xsl:choose with an if expression from XPath 2.0.
<xsl:template match="@*">
<xsl:variable name="namespace" select="if (namespace-uri( )) then namespace-uri( ) else namespace-uri(..)"/>
<xsl:element name="{name( )}" namespace="{$namespace}">
<xsl:value-of select="." />
</xsl:element>
</xsl:template>Discussion
This recipe and the next address problems that arise when a document’s designer has made an unfortunate choice about encoding information in attributes or elements. The ‘attribute or element’ decision is one of the most controversial aspects of document design. (Note: in my experience, the only other stylistic issue that gets software developers even more worked up is where to put braces in C-like programming languages, such as C++ and Java.) These examples are useful because they let you correct your own mistakes or those of others.
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 terms as the printed edition: the work and all its parts are protected by copyright. All rights are 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