Chapter 6 - We Want Results! | |
XSLT For Dummies | |
by Richard Wagner | |
Hungry Minds 2002 |
You can combine elements in various fashions to form new elements. For instance, suppose I want to create a new tagline element for the coffee element in coffee-light.xml . I want to use this new element as a one-line advertisement to market the coffee. With the taste and bestwith elements, I already have bits of marketing information that can be used in the tagline. By combining these elements and adding additional text, I can produce a dynamically generated tagline for each coffee in a flashand do so without any help whatsoever from an ad agency. The stylesheet to do this task is as follows : <!-- coffee-merge.xsl --> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <!-- Merge elements to create tagline element --> <xsl:template match="coffee"> <coffee> <tagline>This Coffee Is <xsl:value-of select="taste"/> And Best Enjoyed With <xsl:value-of select="bestwith"/></tagline> <taste><xsl:value-of select="taste"/></taste> <price><xsl:value-of select="price"/></price> <availability><xsl:value-of select="availability"/></availability> <bestwith><xsl:value-of select="taste"/></bestwith> </coffee> </xsl:template> </xsl:stylesheet> As you can see, the tagline element is created using literal text and xsl:value-of instructions for both the taste and bestwith elements. The result is: <?xml version="1.0" encoding="utf-8"?> <coffee> <tagline>This Coffee Is Curiously Mild And Bland And Best Enjoyed With Breakfast</tagline> <taste>Curiously Mild And Bland</taste> <price>11.99</price> <availability>Year-round</availability> <bestwith>Curiously Mild And Bland</bestwith> </coffee>
|