Chapter 6 - We Want Results! | |
XSLT For Dummies | |
by Richard Wagner | |
Hungry Minds 2002 |
Reordering ElementsYou can rearrange elements in the result document by recreating the element in the image of your choosing. For example, I can shuffle the children of the coffee element in the coffee-light.xml file by coding the following XSLT: <!-- coffee-reorder.xsl --> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <!-- Reorder elements --> <xsl:template match="coffee"> <coffee> <availability><xsl:apply-templates select="availability"/></availability> <price><xsl:apply-templates select="price"/></price> <bestwith><xsl:apply-templates select="bestwith"/></bestwith> <taste><xsl:apply-templates select="taste"/></taste> </coffee> </xsl:template> </xsl:stylesheet> By using a combination of literal text and xsl:apply-templates instructions, I recreate each child within a new sequential order. The result is shown here: <coffee> <availability>Year-round</availability> <price>11.99</price> <bestwith>Breakfast</bestwith> <taste>Curiously Mild And Bland</taste> </coffee> Tip Not all reordering needs to be manually done. You can also automatically sort elements using the xsl:sort instruction. I discuss sorting in Chapter 9.
|