Using XSLT to Transform Data

No matter what you choose as a final data format, you need to translate your XML into something else. You may choose to do this in code by parsing the XML into a tree, traversing the tree, and building the new data format.

Often, a better choice is Extensible Stylesheet Language Transformations (XSLT). XSLT is a language for XML transformation. XSLT stylesheets are themselves XML files. Basic XSLT translations start with an XML file and an XSLT stylesheet. These are fed into an XSLT processor, which applies the rules in the XSLT file to the data in the XML file to output a third file. That resulting file may or may not be XML.

Figure 8. Basic XSLT processing

XSLT is easiest to understand with an example. Many web sites provide RSS news feeds. RSS feeds are just XML files in a standard format. They usually contain a collection of news items that are frequently updated. Users can subscribe to a news feed using a news feed reader. The reader software periodically grabs the RSS file from the web site. The reader parses the RSS file and displays the information to the user. An RSS file contains headlines, links, and brief descriptions of news stories or articles.

O’Reilly publishes several news feeds on www.oreilly.com. The listing below shows an abridged feed file that shows the basic RSS structure.

<?xml version='1.0' encoding='utf-8'?> ...

Get Ajax and Web Services now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.