XML technologies / XSLT / XSLT Cookbook / From XML to XML / Converting elements into attributes

(Excerpt from “XSLT Kochbuch” by Sal Mangano)

Problem

Your document encodes information using child elements, but you would prefer attributes.

Solution

As in recipe Converting attributes into elements, you can use the overriding copy operation. However, converting elements to attributes requires choosing where to apply the transformation: converting every element to an attribute would be meaningless. This stylesheet reverses the previous recipe’s attribute-to-element 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"/>
  <xsl:template match="person">
    <xsl:copy>
      <xsl:for-each select="*">
        <xsl:attribute name="{local-name(.)}">
          <xsl:value-of select="."/>
        </xsl:attribute>
      </xsl:for-each>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

Discussion

XSLT 1.0

This direction is less straightforward. If an element being converted has attributes, decide what happens to them. The previous solution loses them. An alternative passes them to the new parent:

<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="person">
    <xsl:copy>
      <xsl:for-each select="*">
        <xsl:attribute name="{local-name(.)}">
          <xsl:value-of select="."/>
        </xsl:attribute>
        <xsl:copy-of select="@*"/>
      </xsl:for-each>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

This works only if all relevant attribute names are unique. Otherwise, rename them, for example:

<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="person">
    <xsl:copy>
      <xsl:for-each select="*">
        <xsl:attribute name="{local-name(.)}">
          <xsl:value-of select="."/>
        </xsl:attribute>
        <xsl:variable name="elem-name" select="local-name(.)"/>
        <xsl:for-each select="@*">
          <xsl:attribute name="{concat($elem-name,'-',local-name(.))}">
            <xsl:value-of select="."/>
          </xsl:attribute>
        </xsl:for-each>
      </xsl:for-each>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

Sibling elements with repeated names create another conflict when converted to attributes. Another strategy converts an element only if it has no attributes or element children, does not repeat within its parent, and its parent has no attributes:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:import href="copy.xslt"/>
  <xsl:output method="xml" indent="yes" version="1.0" encoding="UTF-8"/>
  <!-- Elemente erfassen, die Eltern sind -->
  <xsl:template match="*[*]">
    <xsl:choose>
      <!-- Kinder nur dann konvertieren, wenn dieses Element selbst keine Attribute besitzt -->
      <xsl:when test="not(@*)">
        <xsl:copy>
          <!-- Kinder in Attribute konvertieren, wenn das Kind keine Kinder oder Attribute besitzt und unter seinen Geschwistern einen eindeutigen Namen trägt -->
          <xsl:for-each select="*">
            <xsl:choose>
              <xsl:when test="not(*) and not(@*) and not(preceding-sibling::*[name( ) = name(current( ))]) and not(following-sibling::*[name( ) = name(current( ))])">
                <xsl:attribute name="{local-name(.)}">
                  <xsl:value-of select="."/>
                </xsl:attribute>
              </xsl:when>
              <xsl:otherwise>
                <xsl:apply-templates select="."/>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:for-each>
        </xsl:copy>
      </xsl:when>
      <xsl:otherwise>
        <xsl:copy>
          <xsl:apply-templates/>
        </xsl:copy>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>

XSLT 2.0

Using xsl:for-each-group partly simplifies and speeds up the 1.0 solution. The trick is to use group-by="name( )" to detect same-named siblings. This also passes the converted element’s attributes to its parent, which is possible in 1.0 too:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:import href="copy.xslt"/>
  <xsl:output method="xml" indent="yes" version="1.0" encoding="UTF-8"/>
  <!-- Elemente erfassen, die Eltern sind -->
  <xsl:template match="*[*]">
    <xsl:choose>
      <!-- Nur Kinder konvertieren, wenn dieses Element keine eigenen Attribute besitzt -->
      <xsl:when test="not(@*)">
        <xsl:copy>
          <!-- Kinder in Attribute umwandeln, wenn das Kind keine Kinder oder Attribute besitzt und unter seinen Geschwistern einen eindeutigen Namen trägt -->
          <xsl:for-each-group select="*" group-by="name( )">
            <xsl:choose>
              <xsl:when test="not(*) and count(current-group( )) eq 1">
                <xsl:attribute name="{local-name(.)}">
                  <xsl:value-of select="."/>
                </xsl:attribute>
                <!-- Attribute des Kindes auf das Elternelement kopieren -->
                <xsl:copy-of select="@*"/>
              </xsl:when>
              <xsl:otherwise>
                <xsl:apply-templates select="current-group( )"/>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:for-each-group>
        </xsl:copy>
      </xsl:when>
      <xsl:otherwise>
        <xsl:copy>
          <xsl:apply-templates select="@*| node( )"/>
        </xsl:copy>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>

Caution!
Both stylesheets assume a canonical structure: at each level, all elements eligible for attribute conversion precede those that are not. This document violates that assumption:

<E1>
  <E2>
    <e31>a</e31>
    <e32>b</e32>
    <e33>c</e33>
  </E2>
  <test>a</test>
  <E2>
    <e31>u</e31>
    <e32>v</e32>
    <e33>w</e33>
  </E2>
  <E2>
    <e31>x</e31>
    <e32>y</e32>
    <e33>z</e33>
  </E2>
</E1>

Element test could theoretically become an attribute of E1. The stylesheet attempts this but fails: XSLT permits copying attributes onto a node only before all other nodes. Such awkward documents need two passes. First, retain eligible elements as elements marked with a special attribute. Second, convert marked elements to parent attributes before copying all other children unchanged to their parents.

  

 

 

 

back to top