Writing Attribute Values

Writing Attribute Values

There are several ways to write attribute values to output documents in XSLT, and the most powerful one is to create attributes from scratch with the <xsl:attribute> element youll see in Chapter 6. However, you can also use attribute value templates for many purposes, and Ill take a look at them in this chapter.

For example, suppose that you want to convert the text in the elements such as <MASS> , <DAY> , and < NAME > to attributes of <PLANET> elements, converting planets.xml to this form:

 <?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> 

To create the transformation, you cant just use expressions such as the following, where I take the values of the <NAME> , <MASS> , and <DAY> elements and try to make them into attribute values:

 <xsl:template match="PLANET">      <PLANET NAME="<xsl:value-of select="NAME"/>"          MASS="<xsl:value-of select="MASS"/>"          DAY="<xsl:value-of select="DAY"/>"      /> 

This doesnt work, because you cant use < inside attribute values as I have in the preceding example. XLST provides multiple ways to do that, however. One way is to use attribute value templates.

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

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