When outputting XML or HTML, you will need to output element and attributes. A XSLT style sheet can instruct an XSLT processing program to output element and attributes using literal result elements. However, it's not always possible to know the names of the new elements you want to create. Whether you're converting an element to a new attribute, an attribute to a new element, or supplying either with a function's return value or a hardcoded string, you nedd a way to create both elements and attributes dynamically to your output.
<xsl:element>: Creating New Elements at Runtime
The <xsl:element> element is very useful if you need to determine a new element's name at runtime. Here are the three attributes of this element:
-
name (mandatory): Name of the element you want to create. Set to an attribute value template that returns a QName.
-
namespace (optional): The namespace uniform resource identifier (URI) of the new element. Set to an attribute value template returning a URI.
-
use-attribute-sets (optional): Specifies the attribute sets containing the attributes for this element. Set to a whitespace-separated list of QNames.
<xsl:attribute>: Adding New Attributes at Runtime
The xsl:attribute element is used to add an attribute value to an xsl:element element or general formatting element, or to an element created using xsl:copy. The attribute must be output immediately after the element, with no intervening character data. The name of the attribute is indicated by the name attribute and the value by the content of the xsl:attribute element.
There are two main uses for the xsl:attribute element:
- It is the only way to set attributes on an element generated dynamically using xsl:element
- It allows attributes of a literal result element to be calculated using xsl:value-of.
The xsl:attribute must be output immediately after the relevant element is generated: there must be no intervening character data (other than white space which is ignored). SAXON outputs the closing ">" of the element start tag as soon as something other than an attribute is written to the output stream, and rejects an attempt to output an attribute if there is no currently-open start tag. Any special characters within the attribute value will automatically be escaped (for example, "<" will be output as "<")
If two attributes are output with the same name, the second one takes precedence.
Example
File: Data.xml
<?xml version="1.0"?>
<?xml-stylesheet href="Transform.xslt" type="text/xsl"?>
<message>Message.</message>
File: Transform.xslt
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes" />
<xsl:template match="/">
<xsl:element name="paragraph">
<xsl:attribute name="priority">medium</xsl:attribute>
<xsl:attribute name="date">2003-09-23</xsl:attribute>
<xsl:attribute name="doc:style"
namespace="http://www.java2s.com/documents">classic</xsl:attribute> <xsl:apply-templates />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<paragraph xmlns:doc="http://www.java2s.com/documents" priority="medium" date="2003-09-23" doc:style="classic">Message.</paragraph>