XSLT » Elements » xsl:attribute


The xsl:attribute element allows you to create an attribute node, define a value, and add it to the output. In simple terms, you are creating a custom attribute whose value can be displayed.

After a child node has been added to an element, no additional attributes can be added to the parent node. This restriction is required so that the XSL processor does not have to store the entire result tree in memory while searching for additional attributes.
The related xsl:attribute-set can serve as a container for a collection of xsl:attribute elements.
This is not a self-closing element. The separate closing element is mandatory.


<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<xsl:for-each select="devguru_staff/programmer">
<xsl:element name="staff_member">
<xsl:attribute name="dob">
<xsl:value-of select="dob" />
<xsl:value-of select="name" />
<br />
<?xml version="1.0" encoding="UTF-16"?>
<staff_member dob="03/21/1970">Bugs Bunny</staff_member>
<br />
<staff_member dob="08/09/1949">Daisy Duck</staff_member>
<br />
<staff_member dob="04/13/1977">Minnie Mouse</staff_member>
<br />
<staff_member dob="07/04/1979">Pluto</staff_member>
<br />
<staff_member dob="11/30/1956">Porky Pig</staff_member>
<br />
<staff_member dob="01/19/1953">Road Runner</staff_member>
<br />

This is the code for xslt_example_attribute.xsl.

In this example we create a staff_member element that contains the name of each staff member and a dob attribute with the date of birth. This is the code for xslt_example_stylesheet.xsl.

We use the DevGuru Staff List XML file for our example with the following header:
<?xml-stylesheet type="text/xsl" href="xslt_example_attribute.xsl"?>

Language(s): XSLT

See Also: