ELEMENT:  xsl:attribute

<xsl:attribute
  name="qname"
  namespace="URI"
>
</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.
 
name="qname"
 
The mandatory name attribute is the qname of the attribute to be displayed in output. A qname is a qualified name that is composed of an optional namespace prefix, a colon, which is only present if there is a prefix, and a mandatory XML name (for example, xsl:zipcode or zipcode). The name is one of a very few attributes that can be set to an expression that is computed at run-time. (Such attributes are interpreted as Attribute Value Templates.) The syntax for doing this is demonstrated with the following code fragment:
 
<xsl:attribute name="{$att_name}" />
 
namespace="URI"
 
The optional namespace attribute is the namespace URI (Universal Resource Identifier) of the attribute. The namespace is one of a very few attributes that can be set to an expression that is computed at run-time. (Such attributes are interpreted as Attribute Value Templates.)
 
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"?>
and we name it: xslt_example_attribute.xml
 
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.
 
Code for xslt_example_stylesheet.xsl:
 
<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:attribute>
<xsl:value-of select="name" />
</xsl:element>
<br />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

 
Output:
 
Click to view output in separate window - requires Internet Explorer

 
Copyright 1999-2001 by Infinite Software Solutions, Inc. All rights reserved.
Trademark Information