Magazine |
| | Community |
| | Workshop |
| | Tools & Samples |
| | Training |
| | Site Info |
|
|
||||||||
|
November 4, 1998
While most uses of XSL will likely generate well-formed HTML, XSL also allows generation of arbitrary XML grammars.
For example, you can convert from one XML grammar or schema to another by writing an XSL stylesheet that reads the input schema and generates the output for the desired schema. You can transform XML into XML just as you would XML to HTML, or you can use special XSL elements designed specifically for XML-to-XML transformations.
Consider these stock quotes received in the following format:
<investments> <item type="stock" symbol="ZCXM" company="zacx corp" price="$18.875"/> <item type="stock" symbol="ZFFX" company="zaffymat inc" price="$20.313"/> <item type="stock" symbol="ZYSZ" company="zysmergy inc" price="$92.250"/> </investments>
Let's say the rest of your stock XML looks like this:
<portfolio> <stock> <name>zacx corp</name> <symbol>ZCXM</symbol> <price>$18.875</price> </stock> <stock> <name>zaffymat inc</name> <symbol>ZFFX</symbol> <price>$20.313</price> </stock> <stock> <name>zysmergy inc</name> <symbol>ZYSZ</symbol> <price>$92.250</price> </stock> </portfolio>
To transform the first grammar into the second, you can use familiar XSL techniques, except that your templates contain output elements like "stock" instead of HTML elements.
<xsl:for-each select="item[@type='stock']"> <stock> <name><xsl:value-of select="@company"/></name> <symbol><xsl:value-of select="@symbol"/></symbol> <price><xsl:value-of seect="@price"/></price> </stock> </xsl:for-each> </portfolio>
Another reason to transform XML into XML is to create an XML file that has been sorted and filtered. The output will use the same schema, but the data will be trimmed, sorted, or augmented. In order to do this, you first produce a stylesheet that will perform an identity transformation on the source tree. Then you add templates to this stylesheet, allowing the transformation to be augmented for sorting.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"> <!-- Identity transformation template --> <xsl:template><xsl:copy><xsl:apply-templates select="@*"/><xsl:apply-templates/></xsl:copy></xsl:template> <!-- Sort stocks by symbol --> <xsl:template match="portfolio"><xsl:copy><xsl:apply-templates select="@*"/><xsl:apply-templates select="stock" order-by="+symbol"/></snack></xsl:template> </xsl:stylesheet>
The identity transformation template applies recursively to the XML source document and copies, using the xsl:copy element, each node found to the output. In order to prevent extra whitespace in the output, the entire template should be on one line. The second template overrides the handling of a specific node (portfolio) in order to sort its children (stocks). The "order-by" attribute indicates how to sort the stocks.
The identity transformation uses the xsl:copy command, which copies a node from the source tree to the output tree. The element keeps its name and value, and its children (and attributes) can be copied by recursion.
When generating XML, it often is useful to generate comments, pis, etc. XSL provides a set of commands that create specific kinds of nodes in the output.
For instance, xsl:attribute is useful for converting data in my stock quote grammar back to the version that relies on attributes.
<investments> <xsl:for-each select="stock"> <item type="stock"> <xsl:attribute name="company"><xsl:value-of select="name"/></xsl:attribute> <xsl:attribute name="symbol"><xsl:value-of select="symbol"/></xsl:attribute> <xsl:attribute name="price"><xsl:value-of select="price"/></xsl:attribute> </item> </xsl:for-each> </investment>
Take the above XML concerning the stock information and transform it into another XML document that is identical except that above each stock element is a comment that tells you whether you have invested in that stock.
Did you find this article useful? Gripes? Compliments? Suggestions for other articles? Write us!
© 1998 Microsoft Corporation. All rights reserved. Terms of use.