Microsoft HomeproductssearchsupportshopWrite Us   Microsoft Home
Magazine
 |  Community
 |  Workshop
 |  Tools & Samples
 |  Training
 |  Site Info

Workshop  |  XML (Extensible Markup Language)

XSL Tutorial
Lesson 13: Using XSL to Generate XML


November 4, 1998

What is XSL that generates XML?

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.

How can I generate XML?

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.

Try it!

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!

Back to topBack to top

© 1998 Microsoft Corporation. All rights reserved. Terms of use.

 

Magazine Home
Ask Jane
DHTML Dude
Extreme XML
For Starters
More or Hess
Servin' It Up
Site Lights
Web Men Talking
Member Community Home
Benefits: Freebies & Discounts
Benefits: Promote Your Site
Benefits: Connect with Your Peers
Benefits at a Glance
Online Special-Interest Groups
Your Membership
SBN Stores
Join Now
Workshop Home
Essentials
Content & Component Delivery
Component Development
Data Access & Databases
Design
DHTML, HTML & CSS
Extensible Markup Language (XML)
Languages & Development Tools
Messaging & Collaboration
Networking, Protocols & Data Formats
Reusing Browser Technology
Security & Cryptography
Server Technologies
Streaming & Interactive Media
Web Content Management
Workshop Index
Tools & Samples Home
Tools
Samples, Headers, Libs
Images
Sounds
Style Sheets
Web Fonts
Training Home
SBN Live Seminars
SBN Live Chats
Courses
Peer Support
CD-ROM Training
Books & Training Kits
Certification
SBN Home
New to SBN?
What's New on SBN
Site Map
Site Search
Glossary
Write Us
About This Site