XML technologies / XSLT / XSLT Cookbook / From XML to XML / Renaming elements or attributes

(Excerpt from “XSLT Kochbuch” by Sal Mangano)

Problem

You need to change the name or namespace of elements or attributes in an XML document.

Solution

To rename just a few attributes or elements, use a simple version of the overriding copy operation as shown here.

Example: Renaming person to individual.

<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">
    <individual>
      <xsl:apply-templates select="@* | node()"/>
    </individual>
  </xsl:template>
</xsl:stylesheet>

Alternatively, use xsl:element:

...
  <xsl:template match="person">
    <xsl:element name="individual">
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>
...

Renaming attributes is just as easy:

<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="@lastname">
    <xsl:attribute name="surname">
      <xsl:value-of select="."/>
    </xsl:attribute>
  </xsl:template>
</xsl:stylesheet>

Sometimes the namespace, rather than the name, must change:

Example: A document in namespace foo.

<foo:someElement xmlns:foo="http://www.ora.com/XMLCookbook/namespaces/foo">
  <foo:aChild> 
    <foo:aGrandChild/>
    <foo:aGrandChild/>
  </foo:aChild>
</foo:someElement>

For every element in namespace foo, create a new one in namespace bar, as these examples show.

Example: A stylesheet mapping foo to bar.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:foo="http://www.ora.com/XMLCookbook/namespaces/foo" xmlns:bar="http://www.ora.com/XMLCookbook/namespaces/bar">
  <xsl:import href="copy.xslt"/>
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
  <xsl:strip-space elements="*"/>
  <xsl:template match="foo:*">
    <xsl:element name="bar:{local-name()}">
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>

Example: Output.

<bar:someElement xmlns:bar="http://www.ora.com/XMLCookbook/namespaces/bar">
  <bar:aChild>
    <bar:aGrandChild/>
    <bar:aGrandChild/>
  </bar:aChild>
</bar:someElement>

Discussion

Naming is an important skill few software professionals master. (My inability is illustrated by my son spending two full days without a name: my wife and I could not find one we both liked. We understood its importance, and believe Leonardo agrees.) You should therefore know how to rename things when the first name was wrong.

For many renamings, use a generic table-driven approach, as in these examples.

Example: A generic table-driven renaming stylesheet.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ren="http://www.ora.com/namespaces/rename">
  <xsl:import href="copy.xslt"/>
  <!--Im importierenden Stylesheet überschreiben -->
  <xsl:variable name="lookup"  select="/.."/>
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
  <xsl:template match="*">
    <xsl:choose>
      <xsl:when test="$lookup/ren:element[@from=name(current())]">
        <xsl:element name="{$lookup/ren:element[@from=local-name(current())]/@to}">
          <xsl:apply-templates select="@*"/>
          <xsl:apply-templates/>
        </xsl:element>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-imports/>
     </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template match="@*">
    <xsl:choose>
      <xsl:when test="$lookup/ren:attribute[@from=name(current())]">
        <xsl:attribute name="{$lookup/ren:attribute[@from=name(current())]/@to}">
          <xsl:value-of select="."/>
        </xsl:attribute>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-imports/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>

Example: Using the table-driven stylesheet.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ren="http://www.ora.com/namespaces/rename">
  <xsl:import href="TableDrivenRename.xslt"/>
  <!-- Die Suchtabelle laden. Wir definieren sie lokal, sie kann aber auch aus einer externen Datei kommen -->
  <xsl:variable name="lookup"  select="document('')/*[ren:*]"/>
  <!-- Definieren der Umbenennungsregeln -->
  <ren:element from="person" to="individual"/>
  <ren:attribute from="firstname" to="givenname"/>
  <ren:attribute from="lastname" to="surname"/>
  <ren:attribute from="age" to="yearsOld"/>
</xsl:stylesheet>

Example: Output.

<?xml version="1.0" encoding="UTF-8"?>
<people which="MeAndMyFriends">
  <individual givenname="Sal" surname="Mangano" yearsOld="38" height="5.75"/>
  <individual givenname="Mike" surname="Palmieri" yearsOld="28" height="5.10"/>
  <individual givenname="Vito" surname="Palmieri" yearsOld="38" height="6.0"/>
  <individual givenname="Vinny" surname="Mari" yearsOld="37" height="5.8"/>
</people>

This approach also handles elements or attributes requiring context-sensitive processing. Consider:

<clubs>
  <club name="The 500 Club">
    <members>
      <member name="Joe Smith">
        <position name="president"/>
      </member>
      <member name="Jill McFonald">
        <position name="treasurer"/>
      </member>
      <!-- ... -->
    <members>
  </club>
  <!-- ... -->
<clubs>

Suppose you want to change attribute @name to @title, but only on elements position. The table-driven approach changes every element with attribute name. Create a template overriding the default for every element except position:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ren="http://www.ora.com/namespaces/rename">
  <xsl:import href="TableDrivenRename.xslt"/>
  <!-- Laden der Suchtabelle. Wir definieren sie lokal, sie kann aber auch aus einer externen Datei kommen -->
  <xsl:variable name="lookup"  select="document('')/*[ren:*]"/>
  <!-- Definieren der Umbenennungsregeln -->
  <ren:attribute from="name" to="title"/>
  <!-- ÜBERSCHREIBEN: Einfach alle Namen kopieren, die keine Attribute des position-Elements sind -->
  <xsl:template match="@name[not(parent::position)]">
    <xsl:copy/>
  </xsl:template>
</xsl:stylesheet>

When copying to rename a namespace, the old namespace may persist even if unnecessary. Reconsider document foo, now with an extra element from namespace doc:

<foo:someElement xmlns:foo="http://www.ora.com/XMLCookbook/namespaces/foo" xmlns:doc="http://www.ora.com/XMLCookbook/namespaces/doc">
  <foo:aChild>
    <foo:aGrandChild/>
    <foo:aGrandChild>
      <doc:doc>Diese Dokumentation darf auf keine Weise entfernt oder verändert werden.</doc:doc>
    </foo:aGrandChild>
  </foo:aChild>
</foo:someElement>

Applying the namespace-renaming stylesheet retains namespace foo with element doc:

<bar:someElement xmlns:bar="http://www.ora.com/XMLCookbook/namespaces/bar">
  <bar:aChild>
    <bar:aGrandChild/>
    <bar:aGrandChild>
      <doc:doc xmlns:doc="http://www.ora.com/XMLCookbook/namespaces/doc" xmlns:foo="http://www.ora.com/XMLCookbook/namespaces/foo">Diese Dokumentation darf auf keine Weise entfernt oder verändert werden.</doc:doc>
    </bar:aGrandChild>
  </bar:aChild>
</bar:someElement>

This is because element doc is processed by xsl:copy. Both xsl:copy and xsl:copy-of always copy all namespaces associated with an element. In XSLT 2.0, both xsl:copy and xsl:copy-of have an optional attribute named copy-namespaces, which may be set to yes or no. Since element doc is enclosed by elements in namespace foo, it has a namespace node foo even if not visible in the output. To avoid copying it, use xsl:element to create fresh elements rather than copying them:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:foo="http://www.ora.com/XMLCookbook/namespaces/foo" xmlns:bar="http://www.ora.com/XMLCookbook/namespaces/bar">
  <xsl:import href="copy.xslt"/>
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
  <xsl:strip-space elements="*"/>
  <!-- Erzeugt für alle Elemente ein neues Element mit demselben Namen und Namensraum -->
  <xsl:template match="*">
    <xsl:element name="{name( )}" namespace="{namespace-uri( )}">
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>
  <xsl:template match="foo:*">
    <xsl:element name="bar:{local-name( )}">
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>

You can even remove all namespaces this way:

<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="*">
    <xsl:element name="{local-name( )}">
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>

  

Back to top

 

 

 

back to top