Transforming XML

printer-friendly version of this section  Print  e-mail this section  E-Mail  add a public, group or private note  Add Note  add a bookmark about this section  Add Bookmark    

Java APIs for XML Kick Start
By Aoyon Chowdhury, Parag Choudhary

Table of Contents
Chapter 6.  XSLT and XPath


So far in this chapter you have learned about the XSLT and XPath basics, and how to use the transformation classes with DOM and SAX. This section will teach you how to create an XSLT stylesheet and use the transformation classes to transform an XML document into another with a very different structure and tag names.

As before, the CarParts.xml file will be used as the source XML document. You will need to write an XSLT stylesheet that will contain the transformation information. You will also need to write a small Java application that will use CarParts.xml and the XSLT stylesheet to do the transformation and show the transformed XML on the screen.

First, let's look at what the transformed CarParts.xml should look like. The transformed XML should appear as follows:

<?xml version="1.0" encoding="UTF-8"?> <carcomponents>     <?supplierformat format="X13" version="3.2"?><!--              This XML is checking out the format with our suppliers>          -->     <componentsupplier>     Heaven Car Parts (TM)     </componentsupplier>     <car_engines>         <engine >             Engine 1         </engine>     </car_engines>     <car_bodies>         <body>             Car Body 1         </body>     </car_bodies>     <car_wheels>         <wheel_type>             Wheel Set 1         </wheel_type>     </car_wheels>     <stereos>         <stereo>             Car Stereo 1         </stereo>     </stereos>     <for_CDATA Type="FreeText"><![CDATA[Special Text:  graphics/ccc.gif<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>SAMS  graphics/ccc.gifPublishing is the &best& <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>.. graphics/ccc.gif]]><![CDATA[ ]]></for_CDATA> </carcomponents> 

As shown here, for the transformation to succeed, the XSLT stylesheet has to do the following:

  • Convert the carparts element to carcomponents.

  • Copy over the processing instruction as is from CarParts.xml and add a comment.

  • Convert the supplier element to componentsupplier.

  • Convert the engines element to car_engine.

  • Copy over the engine element with only the id attribute.

  • Convert the carbodies element to car_bodies element.

  • Convert the carbody element to body.

  • Convert the wheels element to car_wheels.

  • Convert the wheel element to wheel_type.

  • Convert the carstereos element to stereos.

  • Convert the carstereo element to stereo.

  • Convert the forCDATA element to for_CDATA.

  • Add a new attribute called Type to the for_CDATA element.

Creating the XSLT Stylesheet

Launch a text editor of your choice and create a new blank document. Name the document CarParts.xsl. Because an XSLT stylesheet is an XML document, add the standard XML prologue:

<?xml version="1.0" encoding="ISO-8859-1"?> 

Next, to make this document a stylesheet, add the following lines:

<xsl:stylesheet   xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 

Next, ensure that the output is XML by adding the following lines:

<xsl:output method="xml" indent="yes"       cdata-section-elements="for_CDATA"   /> 

The method attribute determines the type of output, which in this case will be XML. The indent attribute ensures that the output is indented. The value for the cdata-section-elements attribute specifies the element that will be treated as CDATA.

Next you will copy over the processing instruction as is from the CarParts.xml file and add a comment. To do so, a combination of <xsl:templates>, the processing-instruction() function, <xsl:copy>, and the <xsl:comment> command will be used. To use them, add the following lines:

<xsl:template match="processing-instruction()">     <xsl:copy/>       <xsl:comment>              This XML is checking out the format with our suppliers>          </xsl:comment>   </xsl:template> 

A stylesheet contains a number of templates that are defined using the <xsl:template> tag. Each template has a match attribute that selects the elements to which the template will be applied. The selection of elements is done as per the XPath addressing mechanism that was described earlier in the chapter. In this case, the processing-instruction() function ensures that the processing instruction is chosen. The <xsl:copy/> command copies the matched elements to the transformed XML. The <xsl:comment> command is used to specify the comment in the transformed XML.

Next you need to transform the carparts, supplier, and engines elements to carcomponents, componentsupplier, and car_engines, respectively. To do that, add the following lines of code:

xsl:template match="carparts">       <carcomponents>          <xsl:apply-templates/>       </carcomponents>     </xsl:template>   <xsl:template match="supplier">         <componentsupplier>            <xsl:apply-templates/>         </componentsupplier>     </xsl:template>   <xsl:template match="engines">           <car_engines>                      <xsl:apply-templates/>           </car_engines>     </xsl:template> 

Now the engine element needs to be copied over with only the id attribute. To copy the element, you will use the <xsl:copy> command. To copy the id attribute, you will create a new template, and then call that template from within the template that matches the engine element. To do so, add the following lines:

<xsl:template name="copyengineid">               <xsl:for-each select="@id">                 <xsl:copy/>               </xsl:for-each>               <xsl:apply-templates/>   </xsl:template>    <xsl:template match="engines/engine">         <xsl:copy>              <xsl:call-template name="copyengineid"/>             </xsl:copy>        </xsl:template> 

Next you will add the stylesheet information for transforming the carbodies, carbody, wheels, wheel, carstereos, and carstereo elements. To do so, add these lines:

<xsl:template match="carbodies">           <car_bodies>              <xsl:apply-templates/>           </car_bodies>     </xsl:template> <xsl:template match="carbodies/carbody">              <body>                 <xsl:apply-templates/>              </body>        </xsl:template> <xsl:template match="wheels">           <car_wheels>              <xsl:apply-templates/>           </car_wheels>     </xsl:template> <xsl:template match="wheels/wheel">              <wheel_type>                 <xsl:apply-templates/>                     </wheel_type>        </xsl:template>   <xsl:template match="carstereos">             <stereos>                <xsl:apply-templates/>             </stereos>       </xsl:template>   <xsl:template match="carstereos/carstereo">                <stereo>                   <xsl:apply-templates/>                </stereo>          </xsl:template> 

Finally you will add stylesheet information to transform the forCDATA element to for_CDATA, and add an attribute called Type. To add the attribute, you will use the <xsl:attribute> command. Add the following lines to transform the forCDATA element and add the attribute:

<xsl:template match="forCDATA">                  <for_CDATA>                      <xsl:attribute name="Type">FreeText</xsl:attribute>                     <xsl:apply-templates/>                   </for_CDATA>          </xsl:template> </xsl:stylesheet> 

You've now successfully created the XSLT stylesheet. Next, you need to write the Java application that will use the stylesheet to transform the CarParts.xml file.


printer-friendly version of this section  Print  e-mail this section  E-Mail  add a public, group or private note  Add Note  add a bookmark about this section  Add Bookmark    
Top

[0672324342/ch06lev1sec7]

 
 


JavaT APIs for XML Kick Start
JAX: Java APIs for XML Kick Start
ISBN: 0672324342
EAN: 2147483647
Year: 2002
Pages: 133

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