Attribute Value Templates

Attribute Value Templates

The name attribute value template has nothing to do with templates as weve been using themthat is, to create stylesheet rules. Instead, using an attribute value template just means that the value of an attribute can be set at execution time.

In this case, you can set an attribute to the value of an XPath expression (this topic is covered more thoroughly in Chapter 4) if you enclose that expression in curly braces, { and }. For example, to set the NAME attribute to the string value of a <DESCRIPTION> element that is a child of the context node, you could assign that value like this: NAME={DESCRIPTION}.

Heres the correct XSLT to use to assign the values from the <NAME> , <MASS> , <RADIUS> , and <DAY> elements to attributes with the same names in the <PLANET> element:

Listing 3.5 Using Attribute Value Templates
 <?xml version="1.0"?>  <xsl:stylesheet version="1.0"  xmlns:xsl="">  <xsl:output method="xml"/>  <xsl:template match="PLANETS">      <xsl:copy>         <xsl:apply-templates select="PLANET"/>      </xsl:copy>  </xsl:template>  <xsl:template match="PLANET">      <PLANET NAME="{NAME}"          MASS="{MASS}"          RADIUS="{RADIUS}"          DAY="{DAY}"      />  </xsl:template>  </xsl:stylesheet> 

Thats all it takes; now look at the resulting document where the values in various elements have been converted to attributes:

 <?xml version="1.0" encoding="UTF-8"?>  <PLANETS>      <PLANET DAY="58.65 " RADIUS="1516 " MASS=".0553 " NAME="Mercury"/>      <PLANET DAY="116.75 " RADIUS="3716 " MASS=".815 " NAME="Venus"/>      <PLANET DAY="1 " RADIUS="2107 " MASS="1 " NAME="Earth"/>  </PLANETS> 

Suppose now that you also want to include the units for each measurement. Each <MASS> , <NAME> , and <RADIUS> element includes a UNITS attribute that gives the units of the measurement,its possible to recover those values. The context node is a <PLANET> element, because thats what the template is set up to match, so you can refer to the child <MASS> , <NAME> , and <RADIUS> elements as "MASS" , "NAME" , and "RADIUS" . To address the UNITS attribute of these elements, you can use the syntax "MASS/@UNITS" , "NAME/@UNITS" , and "RADIUS/@UNITS" , as follows :

 <?xml version="1.0"?>  <xsl:stylesheet version="1.0"  xmlns:xsl="">  <xsl:output method="xml"/>  <xsl:template match="PLANETS">      <xsl:copy>          <xsl:apply-templates select="PLANET"/>      </xsl:copy>  </xsl:template>  <xsl:template match="PLANET">      <PLANET NAME="{NAME}"          MASS="{MASS} {MASS/@UNITS}"          RADIUS="{RADIUS} {RADIUS/@UNITS}"          DAY="{DAY} {DAY/@UNITS}"      />  </xsl:template>  </xsl:stylesheet> 

And heres the result, complete with the units for each measurement:

 <?xml version="1.0" encoding="UTF-8"?>  <PLANETS>      <PLANET DAY="58.65 days" RADIUS="1516 miles"          MASS=".0553 (Earth = 1)" NAME="Mercury"/>      <PLANET DAY="116.75 days" RADIUS="3716 miles"          MASS=".815 (Earth = 1)" NAME="Venus"/>      <PLANET DAY="1 days" RADIUS="2107 miles"          MASS="1 (Earth = 1)" NAME="Earth"/>  </PLANETS> 

Note that you cannot nest { and } in attribute value templates, and if you have an expression that uses { and }, such as function printHello {cout << Hello;}, you must double the curly braces so that the XSLT processor knows to ignore them: function printHello {{cout << Hello;}}.

Attribute value templates always work with the context node. You cannot, however, use attribute value templates anywhere you want in a stylesheet, which causes much confusion for XSLT developers. You can use attribute value templates in only the following places:

  • Literal result elements.

  • Extension elements (see Chapter 5).

  • <xsl:attribute> . You can use the name and namespace attributes here (see Chapter 6).

  • <xsl:element> . You can use the name and namespace attributes here (see Chapter 6).

  • <xsl:number> . You can use the format , lang , letter-value , grouping-separator , and grouping- size attributes here (see Chapter 4).

  • <xsl:processing-instruction> . You can use the name attribute here (see Chapter 6).

  • <xsl: sort > . You can use the lang , data-type , order , and case-order attributes here (see Chapter 5).

Chapter 6 has more on this topic, where you learn how to create attributes (and new elements) from scratch. And youll see more on the XPath expressions you can use in attribute value templates in Chapter 7.

Inside XSLT
Inside Xslt
ISBN: B0031W8M4K
Year: 2005
Pages: 196

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: