XML technologies / XSLT / Java and XSLT / XSLT – Advanced techniques / Evolving a schema (Schema Evolution)

(Excerpt from “Java und XSLT” by Eric M. Burke)

Beyond HTML generation, XSLT’s key role is converting one XML format to another. Often this means small improvements: adding attributes, reordering elements or removing unused data. For a handful of files, direct editing is simpler than creating a stylesheet. For a large collection, one stylesheet can transform the entire library in a single pass. Schema evolution is useful in B2B applications when customers need the same data in different forms.

A sample XML file

Suppose you wrote a logging API for Java programs, producing XML logs like this:

Example: Log before transformation

<?xml version="1.0" encoding="ISO-8859-1"?>
<log>
   <meldung text="Eingabeparameter war null">
      <typ>FEHLER</typ>
      <wann>
         <jahr>2000</jahr>
         <monat>01</monat>
         <tag>15</tag>
         <stunde>03</stunde>
         <minute>12</minute>
         <sekunde>18</sekunde>
      </wann>
      <wo>
         <klasse>com.irgendwas.util.StringUtil</klasse>
         <methode>reverse(String)</methode>
      </wo>
   </meldung>
   <meldung text="config-Datei kann nicht gelesen werden">
      <typ>WARNUNG</typ>
      <wann>
         <jahr>2000</jahr>
         <monat>01</monat>
         <tag>15</tag>
         <stunde>06</stunde>
         <minute>35</minute>
         <sekunde>44</sekunde>
      </wann>
      <wo>
         <klasse>com.foobar.servlet.MainServlet</klasse>
         <methode>init( )</methode>
      </wo>
   </meldung>
   <!-- weitere Meldungen... -->
</log>

This format is verbose, particularly its date and time. Logs can grow large, so a shorter representation would help. Also, message text is an attribute of <meldung> while type is a child. It would make more sense to make type an attribute and message text an element:

<meldung typ="WARNUNG">
  <text>Dies ist der Meldungstext. Mehrzeilige Meldungen lassen sich leichter realisieren, wenn statt eines Attributs ein Element verwendet wird.</text>
  ... Rest ausgelassen

The identity transform

Start a schema-evolution stylesheet with an identity transform: a simple template loading the original and transforming it into a new document with identical elements and attributes. The next example contains such a template.

 

Example: Identitaetstransformation.xslt

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" version="1.0" encoding="ISO-8859-1" indent="yes"/>
  <xsl:template match="@*|node( )">
    <xsl:copy>
      <xsl:apply-templates select="@*|node( )"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

Surprisingly, one template suffices regardless of XML complexity. Our stylesheet uses ISO-8859-1 and indentation regardless of the original format. In XPath, node( ) matches all children of the current context, but not its attributes. Therefore combine @* with node( ):

<xsl:template match="@*|node( )">

In plain language, the template matches every attribute and child of the current context. Since node( ) covers elements, comments, processing instructions and text, it matches everything in the XML document.

Inside the template we use <xsl:copy>, telling the XSLT processor to copy the current node to the result tree. To continue, <xsl:apply-templates> selects all current attributes and children with:

<xsl:apply-templates select="@*|node( )"/>

Transforming elements and attributes

After testing the identity transform, add templates performing the evolution. Multiple templates can match XML nodes, in which case the more specific wins. Without the technical details, an explicit match such as <xsl:template match="when"> overrides the identity template’s wildcard matching all attributes and nodes. Add specific templates to modify chosen elements and attributes.

A main logging problem is the XML volume per <wann> element. Instead of child elements for date and time, use this shorter syntax:

<timestamp zeit="06:35:44" tag="15" monat="01" jahr="2002"/>

This template performs the conversion:

<xsl:template match="wann">
  <!-- 'wann' wird in 'timestamp' geändert und seine Kindelemente werden in Attribute umgewandelt -->
  <timestamp zeit="{stunde}:{minute}:{sekunde}" tag="{tag}" monat="{monat}" jahr="{jahr}"/>
</xsl:template>

Add it to the identity stylesheet. It takes precedence for <wann> elements. Instead of <xsl:copy>, it creates a new <timestamp> element, using AVTs for its attributes and effectively converting elements to attributes. AVT {stunde} is equivalent to selecting child <stunde> of <wann>. Processors need not preserve attribute order, which is insignificant in XML and cannot be enforced.

Next, handle the <meldung> element. As noted, convert attribute text into an element and element <typ> into an attribute. Add another template matching <meldung> elements to override identity. Code comments explain each step.

<!-- <meldung>-Elemente lokalisieren -->
<xsl:template match="meldung">
   <!-- der aktuelle Knoten wird kopiert, nicht jedoch seine Attribute -->
   <xsl:copy>
      <!-- das <typ>-Element wird in ein Attribut umgewandelt -->
      <xsl:attribute name="typ">
         <xsl:value-of select="typ"/>
      </xsl:attribute>
      <!-- das text-Attribut wird in ein Kindelement umgewandelt -->
      <xsl:element name="text">
         <xsl:value-of select="@text"/>
      </xsl:element>
      <!-- da das select-Attribut fehlt, verarbeitet xsl:apply-templates alle Kindelemente des aktuellen Knotens (nicht jedoch Attribute oder Verarbeitungsanweisungen!) -->
      <xsl:apply-templates/>
   </xsl:copy>
</xsl:template>

The stylesheet is almost complete. <xsl:copy> simply copies element <meldung> without attributes or children. Add attributes explicitly with <xsl:attribute> and children with <xsl:element>. Then <xsl:apply-templates> continues transformation of the children of <meldung>. However, element <typ> has become an attribute but still remains in the document: identity still copies <type> elements unchanged. Fix this with an empty template:

<xsl:template match="typ"/>

The complete stylesheet combines these templates. Without repeating all the code, its outline is:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="xml" version="1.0" encoding="ISO-8859-1" indent="yes"/>
   <!-- die Identitätstransformation -->
   <xsl:template match="@*|node( )">
      ...
   </xsl:template>
   <!-- <meldung>-Elemente lokalisieren -->
   <xsl:template match="meldung">
      ...
   </xsl:template>
   <!-- <wann>-Elemente lokalisieren -->
   <xsl:template match="wann">
      ...
   </xsl:template>
   <!-- das <typ>-Element unterdrücken -->
   <xsl:template match="type"/>
</xsl:stylesheet>

The result file

With the stylesheet complete, apply it to all existing XML logs using a simple shell script or batch file. The resulting XML follows.

Example: Transformation result

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="schemaUmwandlung.xslt"?>
<log>
  <meldung typ="FEHLER">
    <text>Eingabeparameter war null</text>
    <timestamp zeit="03:12:18" tag="15" monat="01" jahr="2000"/>
    <wo>
      <klasse>com.irgendwas.util.StringUtil</klasse>
      <methode>reverse(String)</methode>
    </wo>
  </meldung>
  <meldung typ="WARNUNG">
    <text>config-Datei kann nicht gelesen werden</text>
    <timestamp zeit="06:35:44" tag="15" monat="01" jahr="2000"/>
    <wo>
      <klasse>com.irgendwas.servlet.MainServlet</klasse>
      <methode>init( )</methode>
    </wo>
  </meldung>
  <meldung typ="FEHLER">
    <text>negative Dauer ist nicht erlaubt</text>
    <timestamp zeit="10:01:49" tag="17" monat="01" jahr="2000"/>
    <wo>
      <klasse>com.irgendwas.util.DateUtil</klasse>
      <methode>getWoche(int)</methode>
    </wo>
  </meldung>
</log>

   

 

 

 

back to top