2.4 Adding Attributes

To add a single, nonliteral attribute to paragraph in a result tree, all you have to do is add an XSLT attribute element as a child of element. The stylesheet attribute.xsl does just that:

<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:apply-templates/>    </xsl:element>  </xsl:template>    </xsl:stylesheet>

Like element, attribute can have name and namespace attributes. Again, the name attribute, which specifies the name of an attribute for the result tree, is required, while namespace is not. The namespace attribute works pretty much like it does in element. The values of both name and namespace can be computed by using an attribute value template, just as in element.

Apply attribute.xml (which contains no attributes) to attribute.xsl with:

xalan attribute.xml attribute.xsl

to produce a result with a priority attribute:

<?xml version="1.0" encoding="UTF-8"?> <paragraph priority="medium">You can use the attribute element to create attributes on the result tree.</paragraph>

The next stylesheet, attributes.xsl, adds two more attributes to paragraph for a total of three attributes. One of the additional attributes will have a namespace, and one will not:

<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.example.com/documents">classic</xsl:attribute>    <xsl:apply-templates/>   </xsl:element> </xsl:template>    </xsl:stylesheet>

When transforming attribute.xml with attributes.xsl:

xalan attribute.xml attributes.xsl

it produces this result:

<?xml version="1.0" encoding="UTF-8"?> <paragraph priority="medium" date="2003-09-23" xmlns:doc="http://www.example.com/ documents" doc:style="classic">You can use the attribute element to create attributes  on the result tree.</paragraph>

There is another way to specify multiple attributes besides listing them one after another: you can use an attribute set.

2.4.1 Reusing a Set of Attributes

The top-level attribute-set element in XSLT allows you to label a group of attributes with a name. Then you can reference and reuse that group of attributes by supplying the name in the use-attribute-sets attribute of element. The attribute element has a required name attribute, and it also has an optional use-attribute-sets attribute (such as element) so that you can chain attribute sets together. The next section, Section 2.4.1.1, shows you how.

The stylesheet attribute-set.xsl implements this feature:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes"/>    <xsl:attribute-set 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.example.com/documents">classic</xsl:attribute> </xsl:attribute-set> <xsl:template match="/">  <xsl:element name="paragraph" use-attribute-sets="paragraph">    <xsl:apply-templates/>  </xsl:element> </xsl:template>    </xsl:stylesheet>

The attribute-set element is a top-level element in XSLT, meaning that it is only allowed as a child of the stylesheet's document element. Also, the attribute-set element allows only attribute elements as children. This named group of attributes is linked to the element paragraph by the use-attribute-sets attribute. You can also see that even though an element and an attribute set have the same name (paragraph), it poses no naming conflict within XSLT.

If you process attribute-set.xsl against attribute.xml with:

xalan attribute.xml attribute-set.xsl

you will get about the same result as processing it against attributes.xsl:

<?xml version="1.0" encoding="UTF-8"?> <paragraph priority="medium" date="2003-09-23" xmlns:doc="http://www.example.com/document" doc:style="classic">You can use the attribute element to create attributes on the result tree.</paragraph>
2.4.1.1 Chaining attribute sets

As I mentioned earlier, you can also chain attribute sets together. The stylesheet chain.xsl shows you how to do this:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes"/>    <xsl:attribute-set name="doc" use-attribute-sets="paragraph">  <xsl:attribute name="doc:style" namespace="http://www.example.com/documents">classic</xsl:attribute> </xsl:attribute-set> <xsl:attribute-set name="paragraph">  <xsl:attribute name="priority">medium</xsl:attribute>  <xsl:attribute name="date">2003-09-23</xsl:attribute> </xsl:attribute-set>    <xsl:template match="/">  <xsl:element name="paragraph" use-attribute-sets="doc">   <xsl:apply-templates/>  </xsl:element> </xsl:template>    </xsl:stylesheet>

This stylesheet has two attribute-set elements that are chained together by means of the use-attribute-sets attribute. The element definition links to the attribute set named doc, which in turn links to the attribute set named paragraph.

When you process these using:

xalan attribute.xml chain.xsl

the only difference you might see in the result is that the attributes may appear in a different order:

<?xml version="1.0" encoding="UTF-8"?> <paragraph priority="medium" date="2003-09-23" xmlns:doc="http://www.example.com/ documents" doc:style="classic">You can use the element element to create elements on  the result tree.</paragraph>

This is not a problem because attributes are unordered in XML. Although a processor may attempt to keep track of the order of attributes, it is not obligated to do so by the XML 1.0 specification.

Finally, an attribute-set element need not have any content, that is, it does not have to have attribute children. This means that you can do the following (chaining.xsl):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes"/>    <xsl:attribute-set name="para" use-attribute-sets="paragraph"/> <xsl:attribute-set 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.example.com/documents">classic</xsl:attribute> </xsl:attribute-set>    <xsl:template match="/">  <xsl:element name="paragraph" use-attribute-sets="para">   <xsl:apply-templates/>  </xsl:element> </xsl:template>    </xsl:stylesheet>

The attribute-set element named para does not have any attribute children; however, it links to the attribute-set named paragraph with its use-attribute-sets attribute. This has the effect of, in essence, renaming paragraph to para and producing the same result as chain.xsl. Here's the command:

xalan attribute.xml chaining.xsl

Another thing to keep in mind is that use-attribute-sets is not a required attribute, neither on attribute-set nor on element. So, a stylesheet like unchain.xsl is legal:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes"/>    <xsl:attribute-set name="para">  <xsl:attribute name="doc:style" namespace="http://www.example.com/documents">classic</xsl:attribute> </xsl:attribute-set> <xsl:attribute-set name="paragraph">  <xsl:attribute name="priority">medium</xsl:attribute>  <xsl:attribute name="date">2003-09-23</xsl:attribute> </xsl:attribute-set>    <xsl:template match="/">  <xsl:element name="paragraph" use-attribute-sets="para">   <xsl:apply-templates/>  </xsl:element> </xsl:template>    </xsl:stylesheet>

And when processed against attribute.xml with:

xalan attribute.xml unchain.xsl

it produces a result with only one attribute:

<?xml version="1.0" encoding="UTF-8"?> <paragraph xmlns:doc="http://www.example.com/documents" doc:style="classic">You can use the attribute element to create attributes on the result tree.</paragraph>

As you may have guessed already, you can use attribute-sets creatively to add attributes to, or omit them from, a result tree.



Learning XSLT
Learning XSLT
ISBN: 0596003277
EAN: 2147483647
Year: 2003
Pages: 164

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net