December 2002
Intermediate to advanced
672 pages
16h 53m
English
You need to rename or re-namespace elements or attributes in an XML document.
If you need to rename a small number of attributes or elements, use a straightforward version of the overriding copy idiom, as shown in Example 6-5.
Example 6-5. Rename 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/>
</individual>
</xsl:template>
</xsl:stylesheet>Or, alternatively, use
xsl:element:
...
<xsl:template match="person">
<xsl:element name="individual">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
...Renaming attributes is just as straightforward:
<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 you need to re-namespace rather than rename, as shown in Example 6-6.
Example 6-6. A document using the namespace foo
<foo:someElement xmlns:foo="http://www.ora.com/XMLCookbook/namespaces/foo">
<foo:aChild>
<foo:aGrandChild/>
<foo:aGrandChild>
</foo:aGrandChild>
</foo:aChild>
</foo:someElement>For each element in the foo namespace, create a
new element in the bar namespace, ...
Read now
Unlock full access