Those Cute Little Curly Braces

 
xslt for dummies
Chapter 18 - Ten Most Confusing Things About XSLT
XSLT For Dummies
by Richard Wagner
Hungry Minds 2002
  

Curly braces are used in attribute value templates to tell the XSLT processor to evaluate whats inside each of them as an expression, rather than as normal text. In the output tree, the curly braces and expression are replaced with a resulting string. However, keep in mind that curly braces only evaluate expressions inside attribute values, not outside them.

Consider the following XML snippet:

 <film name="Henry V"> <director>Kenneth Branagh</director> <runtime>137</runtime> </film> 

Suppose I want to transform the preceding source by using the following XSLT code:

 <xsl:template match="film"> <!-- Curly braces work inside of attributes --> <movie director="{director}" length="{runtime}"/> <movie newlength="{100+60}"/> <!-- Curly braces do not work here --> The director is {director} and length is {runtime} and newlength is {100+60} <!-- Instead, use xsl:value-of outside of attributes --> The director is <xsl:value-of select="director"/> and length is <xsl:value-of select="runtime"/> and newlength is <xsl:value-of select="100+60"/> <xsl:apply-templates/> </xsl:template> <xsl:template match="director"/> <xsl:template match="runtime"/> 

The result is:

 <movie director="Kenneth Branagh" length="137" /><movie newlength="160" /> The director is {director} and length is {runtime} The director is Kenneth Branagh and length is 137 and newlength is 160 

In looking at the transformation, notice that the first part of the template surrounds the element name with curly braces to return the value of the director and runtime elements inside attribute values. In this context, XPath then evaluates director and runtime as element names rather than as plain text. Similarly, XPath evaluates 100+60 as an expression.

The second part of the template shows what happens when you try to use curly braces outside attribute values. These are simply treated as literal text in the output document.

The final part of the template illustrates how you use xsl:value-of to evaluate the same XPath expressions outside attribute values.

See Chapter 4 for more information on attribute value templates.

  
 
 
2000-2002    Feedback


XSLT For Dummies
XSLT for Dummies
ISBN: 0764536516
EAN: 2147483647
Year: 2002
Pages: 148

Similar book on Amazon

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