Creating New Attributes

Just as you can create new elements with <xsl:element> and set the element name and content under programmatic control, you can do the same for attributes using the <xsl:attribute> element.

Here's an example. In this case, I'm creating new <PLANET> elements with attributes corresponding to the various planet names and values taken from the COLOR attribute in the original <PLANET> elements:

Listing . ch13_19.xsl
 
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="PLANETS">
    <HTML>
        <HEAD>
            <TITLE>
                Planets
            </TITLE>
        </HEAD>
        <BODY>
            <xsl:apply-templates select="PLANET"/>
        </BODY>
    </HTML>
</xsl:template>

<xsl:template match="PLANET">
    <PLANET>
					<xsl:attribute ...

Get Real World XML now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.