XML-to-XML Transformations

XML-to-XML Transformations

XML-to-XML transformations are sometimes thought of as SQL for the Internet, because they enable you to use what amount to database queries on XML documents. Heres an example of what I mean. The planets.xml file weve been using has a lot of data about each planet, as you see here:

 <?xml version="1.0"?>  <PLANETS>      <PLANET>          <NAME>Mercury</NAME>      <MASS UNITS="(Earth = 1)">.0553</MASS>      <DAY UNITS="days">58.65</DAY>      <RADIUS UNITS="miles">1516</RADIUS>      <DENSITY UNITS="(Earth = 1)">.983</DENSITY>      <DISTANCE UNITS="million miles">43.4</DISTANCE><!--At perihelion-->  </PLANET>  <PLANET>      <NAME>Venus</NAME>      <MASS UNITS="(Earth = 1)">.815</MASS>      <DAY UNITS="days">116.75</DAY>      <RADIUS UNITS="miles">3716</RADIUS>      <DENSITY UNITS="(Earth = 1)">.943</DENSITY>      <DISTANCE UNITS="million miles">66.8</DISTANCE><!--At perihelion-->  </PLANET>      .      .      . 

What if you just want a subset of that data, such as the name and mass of each planet? In database terms, planets.xml represents a table of data, and you want to create a new table holding just a subset of that data. Thats what SQL can do in databases, and thats what XSLT can do with XML documents.

Heres a new version of planets.xsl that will perform the transformation we want, selecting only the name and mass of each planet, and sending that data to the output document. Note in particular that were performing an XML-to-XML transformation, so Im using the <xsl:output> element with the method attribute set to xml (in fact, the default output type is usually XML, but if an XSLT processor sees a <html> tag, it usually defaults to HTML):

Listing 1.6 Selecting Name and Mass Only
 <?xml version="1.0"?>  <xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">  <xsl:strip-space elements="*"/>  <xsl:output method="xml" indent="yes"/>  <xsl:template match="/">      <xsl:apply-templates/>  </xsl:template>      <xsl:template match="PLANETS">          <xsl:apply-templates/>      </xsl:template>      <xsl:template match="PLANET">         <xsl:copy>            <xsl:apply-templates/>         </xsl:copy>     </xsl:template>      <xsl:template match="MASS">          <xsl:copy>          <xsl:value-of select="."/>          <xsl:value-of select="@UNITS"/>          </xsl:copy>      </xsl:template>      <xsl:template match="RADIUS">      </xsl:template>      <xsl:template match="DAY">      </xsl:template>      <xsl:template match="DENSITY">      </xsl:template>      <xsl:template match="DISTANCE">      </xsl:template>  </xsl:stylesheet> 

Ill apply this new version of planets.xsl to planets.xml using Xalan to create a new XML document, new.xml:

 C:\planets>java org.apache.xalan.xslt.Process -IN planets.xml -XSL planets.xsl -OUT graphics/ccc.gif new.xml 

Heres what the resulting XML document, new.xml, looks like:

 <?xml version="1.0" encoding="UTF-8"?>  <PLANET>      <NAME>Mercury</NAME>      <MASS>.0553(Earth = 1)</MASS>  </PLANET>  <PLANET>      <NAME>Venus</NAME>      <MASS>.815(Earth = 1)</MASS>  </PLANET>  <PLANET>      <NAME>Earth</NAME>      <MASS>1(Earth = 1)</MASS>  </PLANET> 

Note that this looks much like the original planets.xml, except that each <PLANET> element contains only <NAME> and <MASS> elements. In this way, weve been able to get a subset of the data in the original XML document.

You can make any number of other types of XML-to-XML transformations, of course. You can process the data in an XML document to create entirely new XML documents. For example, you can take an XML document full of student names and scores and create a new document that shows average scores. XSLT supports many built-in functions that enable you to work with data in this way, and youll see those functions in Chapter 8.

In addition, many programs use XML to exchange data online, and they usually format their XML documents differently, so another popular use of XML-to-XML transformations on the Internet is to transform XML from the format used by one program to that used by another.



Inside XSLT
Inside Xslt
ISBN: B0031W8M4K
EAN: N/A
Year: 2005
Pages: 196

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net