6.6. Adding Attributes to LREs

Attributes can be added to LREs in three ways: by directly typing the attribute and its value in the LRE, by using the <xsl:attribute> element, or by using the <xsl:attribute-set> element in conjunction with either the use-attribute-sets on an element or xsl:use-attribute-sets attribute on an LRE. Because LREs are passed through to the output result tree, using attributes directly in LRE elements can be difficult if the value of the attribute is to be generated or extracted from some node-set in the input document. For example, if we wanted to output a new element called <newblock> instead of our <block>s in Markup City, and then add an attribute with the text content of the <block>, the template in Example 6-14 would not accomplish our goal.

Example 6-14. An ineffective way to add attributes to LREs.
<xsl:template match="block">
      <newblock name="text()"/>
</xsl:template>

The result of this template rule would replace each <block> element in the input with a literal element <newblock name="text()"/> in the output. The content of the new attribute would be the literal string text(). It is not possible to use a function directly in an LRE attribute. For this reason, the value of an attribute of a literal result element can be an Attribute Value Template, or AVT.

6.6.1. Attribute Value Templates

Normally, LRE attributes are passed through verbatim to the output result tree. However, if the attribute of an LRE contains curly braces ({}), the content of the curly braces is considered an Attribute Value Template, or AVT. AVTs can contain expressions and patterns, allowing the value of the resulting attribute to be extracted or generated from the input source document, or from some other result of an expression.

Note

The word template in Attribute Value Template should not be misconstrued to mean that it can contain children or anything of the sort, and should not be mistaken with the template contained in the <xsl:template> element.


If we add curly braces to the attribute value of the LRE in the previous example, the result will actually be the correct value of each text() node of each <block> element, as shown in Example 6-15. Note that in this example we are removing any extra text or stylesheet elements that do not apply to the result.

Example 6-15. Using an AVT to create a new attribute in an LRE.
						INPUT:


<?xml version="1.0"?>
<main>
      <parkway>
             <thoroughfare>Governor Drive</thoroughfare>
             <thoroughfare name="Whitesburg Drive">
                   <sidestreet>Bob Wallace Avenue</sidestreet>
                   <block>1st Street</block>
                   <block>2nd Street</block>
                   <block>3rd Street</block>
                   <sidestreet>Woodridge Street</sidestreet>
             </thoroughfare>
             <thoroughfare name="Bankhead">
                   <sidestreet>Tollgate Road</sidestreet>
                   <block>First Street</block>
                   <block>Second Street</block>
                   <block>Third Street</block>
                   <sidestreet>Oak Drive</sidestreet>
             </thoroughfare>
      </parkway>
      <boulevard>
                   <block>Panorama Street</block>
                   <block>Highland Plaza</block>
                   <block>Hutchens Avenue</block>
                   <block>Wildwood Drive</block>
                   <block>Old Chimney Road</block>
                   <block>Carrol Circle</block>
      </boulevard>
                   </main>
TEMPLATE RULE:

<xsl:template match="block">
      <newblock name="{text()}"/>
</xsl:template>
OUTPUT:

<newblock name="1st Street"/>
<newblock name="2nd Street"/>
<newblock name="3rd Street"/>
<newblock name="First Street"/>
<newblock name="Second Street"/>
<newblock name="Third Street"/>
<newblock name="Panorama Street"/>
<newblock name="Highland Plaza"/>
<newblock name="Hutchens Avenue"/>
<newblock name="Wildwood Drive"/>
<newblock name="Old Chimney Road"/>
<newblock name="Carrol Circle"/>

6.6.2. Using <xsl:attribute> with LREs

Adding attributes to LREs can also be accomplished by using the <xsl:attribute> element directly following the open tag for the LRE, as shown in Example 6-16.

Of course, the value of the resulting attribute, which is determined by the content of the <xsl:attribute> element, will be once again hard-coded, or literally output, for each <newblock> as MyAtt, which is not really useful. But because we've moved the resolution of the value of the attribute to an instruction element instead of an AVT, we can now add instruction elements, such as <xsl:value-of>, as the content of the <xsl:attribute> element, as shown in Example 6-17.

Example 6-16. Adding attributes to LREs using <xsl:attribute>.
<xsl:template match="block">
      <newblock>
            <xsl:attribute name="name">MyAtt<xsl:attribute>
      </newblock>
</xsl:template>

The template will result in the same output as was shown in Example 6-15, but this stucture allows additional elements to be included to expand the functionality of the <xsl:attribute> element. In addition, <xsl:attributes> can be nested in <xsl:attribute-set> elements and used on LRE elements as discussed in the next section.

Example 6-17. Using <xsl:value-of> to add attributes to LREs.
<xsl:template match="block">
      <newblock>
            <xsl:attribute name="name">
                  <xsl:value-of select="."/>
            </xsl:attribute>
      </newblock>
</xsl:template>

6.6.3. Using <xsl:attribute-set> and the xsl:use-attribute-sets Attribute with LREs

LREs can take advantage of attributes defined in attribute sets by calling them directly in the LRE, using the xsl prefix on the use-attribute-sets attribute. For example, the element <newblock> created in Example 6-17 can use the xsl:use-attribute-sets to call a predefined set of attributes. The attributes in the set are added to the <newblock> element in the output result tree, as shown in Example 6-18.

The two attributes, name and context, are added to each <newblock> element, with their value calculated from each node as it is processed.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.238.20