The <xsl:attribute> Element: Creating New Attributes

Just as you can create new elements with <xsl:element> and set the element name and content at run time, you can use the <xsl:attribute> element to do the same for attributes.

This element has two attributes:

  • name(mandatory). The name of the new attribute. Set to an attribute value template returning a QName.

  • namespace(optional). The namespace of the new attribute. Set to a URI.

This element encloses a template body that sets the value of the attribute.

In the following example, 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 6.7. Using <xsl:attribute>
 <?xml version="1.0"?> ...

Get Inside XSLT 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.