xsl:attribute
xsl:copy
xsl:copy-of
ELEMENT: xsl:element
<xsl:element
name="element-name"
namespace="URI"
use-attribute-sets="qname"
>
</xsl:element>
The
xsl:element
element is used to create and name an element (node) that can appear in the output. This ability to create both custom elements and attributes, and to display the results, is a major reason why stylesheets generated by XSL are a very sophisticated approach to displaying XML data.
There are two ways to add attributes to a created element: by using the
xsl:attribute, xsl:copy,
and the
xsl:copy-of
elements or, by using the optional
use-attribute-sets
attribute of the
xsl:element
element. Any attributes created using the
use-attribute-sets
attribute that have the same name as attributes created using the
xsl:attribute, xsl:copy,
and the
xsl:copy-of
elements, will be overwritten.
There is a firm restriction regarding the addition of attributes. After a child node has been added to an element, no additional attributes can be added to the parent node. This rule is required so that the XSL processor does not have to store the result tree in memory. Otherwise, if attributes could be added at a later time, the result tree would have to be stored while the XSL processor searched the entire tree for additional attributes.
Note, you can use the
xsl:copy
element to copy a node from the source document.
This element is not self-closing. The separate closing element is mandatory.
name="element-name"
The mandatory
name
attribute is the qname of the element to be created. 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 in XSLT 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:element name="{$el_name}" />
namespace="URI"
The optional
namespace
attribute is the namespace URI (Universal Resource Identifier) of the generated element. 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.)
use-attribute-sets="qname"
The optional
use-attribute-sets
attributes allows the addition of attributes to the created element. It is composed of a white space delimited list (set) of the attributes to be added. This is an alternative to using the
xsl:attribute
element.
We use the
DevGuru Staff List XML file
for our example with the following header:
<?xml-stylesheet type="text/xsl" href="xslt_example_element.xsl"?>
and we name it: xslt_example_element.xml
In this example we create a staff_member element that contains the name of each staff member.
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: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