XSLT for Dummies

 
Chapter 6 - We Want Results!
XSLT For Dummies
by Richard Wagner
Hungry Minds 2002
  

So far in this chapter, Ive been manipulating elements to generate the result documents, but you can similarly work with attributes as well. (If you recall, attributes are name /value pairs that describe the element it is associated with.)

Just as I showed you in the preceding section how to remove an element, an attribute can also be omitted from the result document in a variety of ways. One common technique is to reconstruct the element in a template rule, leaving behind any attributes you dont want to include. In the following stylesheet, I create a new derivative of coffee-light.xml that omits the origin attribute from the coffee element:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <!-- Copy coffee but don't include the origin attribute --> <xsl:template match="coffee"> <coffee name="{@name}"> <xsl:apply-templates/> </coffee> </xsl:template> <!-- Copy everything else over --> <xsl:template match="@*node()"> <xsl:copy> <xsl:apply-templates select="@*node()"/> </xsl:copy> </xsl:template> </xsl:stylesheet>

The result is shown here:

<?xml version="1.0" encoding="utf-8"?> <coffee name="Guatemalan Express"> <taste>Mild and Bland</taste> <price>11.99</price> <availability>Year-round</availability> <bestwith>Breakfast</bestwith> </coffee>

A second example that follows highlights another technique to remove attributes. Suppose I want to remove the attributes from the region element. I can do this with the following XSLT code:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <!-- Copy region, but not its attributes --> <xsl:template match="region"> <xsl:copy> <xsl:apply-templates select="*node()"/> </xsl:copy> </xsl:template> <!-- Copy everything else over --> <xsl:template match="@*node()"> <xsl:copy> <xsl:apply-templates select="@*node()"/> </xsl:copy> </xsl:template> </xsl:stylesheet>

Looking closer at the stylesheet, the first template copies the region tags to the result document with xsl:copy . It also uses xsl:apply-templates to add its content to the output as well. However, the select attribute value of xsl:apply-templates selects elements and other nodes, but it specifically does not include attributes. The attribute-free result for region is shown here:

<?xml version="1.0" encoding="utf-8"?><coffees> <region> <coffee name="Guatemalan Express" origin="Guatemala"> <taste>Mild and Bland</taste> <price>11.99</price> <availability>Year-round</availability> <bestwith>Breakfast</bestwith> </coffee> <coffee name="Costa Rican Deacon" origin="Costa Rica"> <taste>Solid Yet Understated</taste> <price>12.99</price> <availability>Year-round</availability> <bestwith>Dessert</bestwith> </coffee> </region> <region> <coffee name="Ethiopian Sunset Supremo" origin="Ethiopia"> <taste>Exotic and Untamed</taste> <price>14.99</price> <availability>Limited</availability> <bestwith>Chocolate</bestwith> </coffee> <coffee name="Kenyan Elephantismo" origin="Kenya"> <taste>Thick And Chewy</taste> <price>9.99</price> <availability>Year-round</availability> <bestwith>ElephantEars</bestwith> </coffee> </region> </coffees>

  
 
 
2000-2002    Feedback

Категории