Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your profile (name, country/region, and company) is displayed to the public and will accompany any content you post. You may update your IBM account at any time.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

developerWorks Community:

  • Close [x]

Automate XML file updates, Part 1: XML process introduction and conversion stylesheet creation

A methodology using XSLT, Apache Ant, and Java SE

Tom Coppedge (tcoppedg@us.ibm.com), developerWorks Software Engineer, IBM
Tom Coppedge has been a member of the developerWorks design team since the site was launched in 1999. Tom's focus includes XML & XSLT strategy, information architecture, and site design. He joined IBM in 1988 after receiving a degree in Information Systems & Operations Management from the University of North Carolina at Greensboro.

Summary:  This is the first part of a tutorial series that describes a method for automating updates to a library of XML files so that they all conform to an updated XML schema. In Part 1, you learn the steps in the entire process and then create an XSLT stylesheet to update the XML files. In Part 2, you learn how to install, configure, and run Apache Ant and Java SE to iteratively transform each of your XML files based on the updates specified in your XSLT stylesheet.

View more content in this series

Date:  17 Aug 2006
Level:  Intermediate PDF:  A4 and Letter (303 KB | 15 pages)Get Adobe® Reader®

Activity:  13991 views
Comments:  

Create and test the XSLT conversion stylesheet

You should now have a set of tested XSLT templates that will implement the necessary XML instance document updates. You might have one template per stylesheet; or, by this time, you might have combined the templates into one or more common stylesheets. If you did the latter, you already have a head start on this step. Here's the process, which you've probably guessed by now:

  1. Combine all of your templates into one stylesheet. Technically, the order of the templates doesn't matter; alphabetizing them makes it easier to find them within the stylesheet.
  2. Test the stylesheet using XML instance documents that will cause your templates to fire and alter the output, as well as documents that will leave the instance documents unchanged. By this stage, you should already have a good list of sample instance documents due to the testing of individual templates.
  3. Change your stylesheet to reflect any problems that testing exposes. The only problems you might expect to encounter at this stage are those caused by overlapping template code (templates that process the same situations differently). This is usually the result of different people developing similar templates or from requirement updates since the original templates were coded and tested.
  4. If you have the luxury of time and willing people, ask some of your XML instance document authors and editors to transform some of their representative content with the conversion stylesheet to confirm that the result is an expected one.

It's worth emphasizing that I've added no additional XSLT code to this stylesheet other than the stylesheet declaration information at the top, some comments, and a stylesheet closing tag at the bottom.


Listing 3. Example conversion stylesheet: All conversion templates combined
<?xml version="1.0" encoding="UTF-8"?>
<!-- Conversion stylesheet for updating V1-based xml documents to V2 -->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
exclude-result-prefixes="fo xsi xsl">
  <xsl:output method="xml" indent="no" omit-xml-declaration="yes" 
                     encoding="UTF-8"/>
  <!-- Change employee element as follows:
       1. Replace dept element with new dept attribute.
       2. Replace old dept numbers with new ones
       3. Delete location element.
       4. Add manager element.
  -->
  <xsl:template match="employee">
    <xsl:element name="employee">
      <!-- Add dept attribute; source from dept element. -->
      <xsl:attribute name="dept">
         <xsl:choose>
         <!-- Replace old dept numbers with new values -->
            <xsl:when test="dept='012'">
               <xsl:text>01</xsl:text>
            </xsl:when>
            <xsl:when test="dept='123'">
               <xsl:text>02</xsl:text>
            </xsl:when>
            <xsl:when test="dept='456'">
               <xsl:text>03</xsl:text>
            </xsl:when>
            <xsl:when test="dept='789'">
               <xsl:text>04</xsl:text>
            </xsl:when>
            <xsl:otherwise>
               <xsl:text>05</xsl:text>
            </xsl:otherwise>
         </xsl:choose>
      </xsl:attribute>
      <xsl:for-each select="*">
        <xsl:choose>
          <!-- Delete dept and location elements. -->
          <xsl:when test="name()='dept' or name()='location'"/>
          <xsl:otherwise>
            <xsl:choose>
              <xsl:when test="name()='phone'">
                <!-- The match="phone" template will be called -->
                <xsl:apply-templates select="."/>
              </xsl:when>
              <xsl:otherwise>
                <xsl:element name="{name()}">
                  <xsl:value-of select="."/>
                </xsl:element>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:for-each>
      <!-- Add manager element -->
      <xsl:element name="manager">
        <xsl:choose>
          <xsl:when test="dept='012'">
            <xsl:text>Ms. Alpha</xsl:text>
          </xsl:when>
          <xsl:when test="dept='123'">
            <xsl:text>Mr. Bravo</xsl:text>
          </xsl:when>
          <xsl:when test="dept='456'">
            <xsl:text>Mr. Charlie</xsl:text>
          </xsl:when>
          <xsl:when test="dept='789'">
            <xsl:text>Ms. Delta</xsl:text>
          </xsl:when>
          <xsl:otherwise>
             <xsl:text>Ms. Parker</xsl:text>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:element>
    </xsl:element>
  </xsl:template>
  
   <!-- Change phone element as follows:
        Replace '123' area code with '333'.
   -->
  <xsl:template match="phone">
    <!-- Change area code 123 to 333, but keep the exchange+number -->
    <xsl:variable name="exchange-number">
      <xsl:value-of select="substring-after(.,'123')"/>
    </xsl:variable>
    <xsl:element name="phone">
       <xsl:value-of select="concat('333',$exchange-number)"/>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>

6 of 10 | Previous | Next

Comments



static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Web development, XML
ArticleID=153748
TutorialTitle=Automate XML file updates, Part 1: XML process introduction and conversion stylesheet creation
publish-date=08172006
author1-email=tcoppedg@us.ibm.com
author1-email-cc=