Skip to main content

XML for Data: Styling with schemas

Using XML Schema archetypes and XSLT style sheets to simplify your code

Kevin Williams (kevin@blueoxide.com), Chief XML architect, Equient (a division of Veridian)
Kevin Williams is the chief XML architect for Equient, a division of Veridian specializing in XML design for information management systems. He has also co-written several books on XML from Wrox Press. He can be reached for comment at kevin@blueoxide.com. Random XML musings, tips, tricks, and opinionated rants may be found at his Web site www.blueoxide.com.

Summary:  This column by developer and author Kevin Williams demonstrates how to use XML Schema archetyping (and style sheets) to control styling of data for various presentation modes. Ten code samples in XML, XML Schema, and XSLT show how the techniques work to reduce code bulk and simplify maintenance.

View more content in this series

Date:  01 Jul 2001
Level:  Introductory
Activity:  1776 views

In my previous column, I described how simple and complex archetypes (see the sidebar What are archetypes?) may be used to simplify and streamline your XML schema designs. This column takes a look at one practical application of XML Schema archetypes: using style sheets to provide a consistent rendering of archetypes in the presentation layer.

Isn't presentation a fairly simple process?

How simple the presentation process is depends on how you define simple. When you work with XML for data, often you need to represent certain information in a consistent manner. For example, whenever you have a color display you might want to render balances for a bank account as black numbers if they are positive, and as red numbers if they are negative. It's easy enough to write XSLT code that takes a particular element value and renders it this way. The need to repeat that style code everywhere it is needed (for all the various views of your information, and for each piece of information that needs to be styled the same way) makes copy-and-paste errors a real danger. Let's take a look at a quick example. Say you had a document defined by the XML schema in Listing 1.

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <xs:element name="customer" type="customerType" />
   <xs:complexType name="customerType">
      <xs:sequence>
         <xs:element name="customerID" type="xs:integer" />
         <xs:element name="checkingBalance">
           <xs:simpleType>
             <xs:restriction base="xs:decimal">
               <xs:totalDigits value="10" />
               <xs:fractionDigits value="10" />
             </xs:restriction>
           </xs:simpleType>
         </xs:element>  
         <xs:element name="savingsBalance">
           <xs:simpleType>
             <xs:restriction base="xs:decimal">
               <xs:totalDigits value="10" />
               <xs:fractionDigits value="10" />
             </xs:restriction>
           </xs:simpleType>
         </xs:element>  
      </xs:sequence>
   </xs:complexType>
</xs:schema>

In order to style the savingsBalance and checkingBalance elements properly, you might create a template in your XSLT style sheet that looks like Listing 2.

  <xsl:template match="savingsBalance | checkingBalance">
    <xsl:element name="{name()}">
      <xsl:value-of select="format-number(text(), '###,###,##0.00')" />
    </xsl:element>
  </xsl:template>

What are archetypes?

Archetypes are common definitions that can be shared across different elements in your XML schemas. In early versions of the XML Schema specification, archetypes had their own declarations; in the released version, however, archetypes are implemented using the simpleType and complexType elements. My previous column gives some examples.

However, you can see that if you add a new element that represents a balance, you'll need to change the template. This might be a problem if you have many different source document types and many different rendering targets (such as wireless platforms, other XML formats, and so on). What you'd like to be able to do is write one style sheet that handles all of the balance-type fields in the original document, without having to recode each new document type you create. The secret is to define simple archetypes in the XML schema and take advantage of them to save you effort.


Taking advantage of archetypes

Let's revisit the XML structure from Listing 1 and add a simple archetype that represents balance information. The schema would then look something like Listing 3.

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <xs:element name="customer" type="customerType" />
   <xs:complexType name="customerType">
      <xs:sequence>
         <xs:element name="customerID" type="xs:integer" />
         <xs:element name="checkingBalance" type="balance" />
         <xs:element name="savingsBalance" type="balance" />
      </xs:sequence>
   </xs:complexType>
   <xs:simpleType name="balance">   
     <xs:restriction base="xs:decimal">
       <xs:totalDigits value="10" />
       <xs:fractionDigits value="10" />
     </xs:restriction>
   </xs:simpleType>
</xs:schema>

Note that the XML schema declaration using archetypes is smaller and, arguably, easier to read. You gain other benefits from structuring the schemas that way, as you'll see later in this column. You can take advantage of the fact that an XML schema is just an XML document and write a style sheet that processes it into another form. Listing 4 illustrates a style sheet that takes the XML schema and creates another style sheet from it.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xsl:strip-space elements="*" />
  <xsl:template match="/">
    <xsl:element name="xsl:stylesheet">
      <xsl:apply-templates />
    </xsl:element>
  </xsl:template>
  <xsl:template match="xs:element[@type='balance']">
    <xsl:element name="xsl:template">
      <xsl:attribute name="match"><xsl:value-of select="@name" /></xsl:attribute>
      <xsl:element name="xsl:element">
        <xsl:attribute name="name"><xsl:value-of select="@name" /></xsl:attribute>
        <xsl:element name="xsl:value-of">
          <xsl:attribute name="select">format-number(text(),
           '###,###,##0.00')</xsl:attribute>
        </xsl:element>
      </xsl:element>      
    </xsl:element>
  </xsl:template>      
</xsl:stylesheet>

There are a few things worth noting in the style sheet in Listing 4. First, it declares the XML schema namespace up front, because the XSLT processor needs to be aware that you intend it to match on elements in the original schema that are declared in the XML schema namespace. Next, space stripping for all elements is turned on. Because this style sheet is not intended to be human-readable, you can save space by compressing the result as much as possible. Also, note that matching is driven by the archetypes: No reference is made to the specific elements in the original schema document. Instead, it looks to match elements of the type balance. Additionally, you need to include the actual style code only once for an archetype. When you use this style sheet to style the original schema fragment, the resulting presentation style sheet would look something like Listing 5.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="checkingBalance">
    <xsl:element name="checkingBalance">
      <xsl:value-of select="format-number(text(), '###,###,##0.00')"/>
    </xsl:element>
  </xsl:template>
  <xsl:template match="savingsBalance">
    <xsl:element name="savingsBalance">
      <xsl:value-of select="format-number(text(), '###,###,##0.00')"/>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>

By generating the style sheets dynamically whenever the schema changes, you greatly reduce code size and the chance of introducing copy-and-paste errors. For example, let's say you decide to change the document structure to look like Listing 6.

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <xs:element name="customer" type="customerType" />
   <xs:complexType name="customerType">
      <xs:sequence>
         <xs:element name="customerID" type="xs:integer" />
         <xs:element name="checkingBalance" type="balance" />
         <xs:element name="savingsBalance" type="balance" />
         <xs:element name="moneyMarketBalance" type="balance" />
      </xs:sequence>
   </xs:complexType>
   <xs:simpleType name="balance">   
     <xs:restriction base="xs:decimal">
       <xs:totalDigits value="10" />
       <xs:fractionDigits value="10" />
     </xs:restriction>
   </xs:simpleType>
</xs:schema>

Since the system automatically regenerates the presentation style sheets for each new version of the schema, you just run the schema through the same style sheet (from Listing 4) to generate the following style sheet (again with whitespace added) that includes moneyMarketBalance.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="checkingBalance">
    <xsl:element name="checkingBalance">
      <xsl:value-of select="format-number(text(), '###,###,##0.00')"/>
    </xsl:element>
  </xsl:template>
  <xsl:template match="savingsBalance">
    <xsl:element name="savingsBalance">
      <xsl:value-of select="format-number(text(), '###,###,##0.00')"/>
    </xsl:element>
  </xsl:template>
  <xsl:template match="moneyMarketBalance">
    <xsl:element name="moneyMarketBalance">
      <xsl:value-of select="format-number(text(), '###,###,##0.00')"/>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>

Thus, without modifying the style sheets, you can properly render changes in the source document (as long as you use archetypes properly). This makes it very easy to create new source document types or change the presentation format of archetypes without having to search and replace through hundreds of style sheets.


Complex archetypes

The strategy of styling schemas also extends to the handling of complex archetypes. By using a style sheet to define a template that matches an element with a particular archetype, you can further simplify the code. Take a look at another example: Say you have a document whose schema looks like Listing 8.

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <xs:element name="customer" type="customerType" />
   <xs:complexType name="customerType">
      <xs:sequence>
         <xs:element name="customerID" type="xs:integer" />
         <xs:element name="mailingAddress" type="address" />
         <xs:element name="shippingAddress" type="address" />
      </xs:sequence>
   </xs:complexType>
   <xs:complexType name="address">   
     <xs:sequence>
       <xs:element name="address1" type="xs:string" />
       <xs:element name="address2" type="xs:string" minOccurs="0" />
       <xs:element name="city" type="xs:string" />
       <xs:element name="state" type="xs:string" />
       <xs:element name="zip" type="xs:string" />
     </xs:sequence>
   </xs:complexType>
</xs:schema>

You can leverage the address archetype defined in the style sheet-generating style sheet to ensure that both mailing and shipping addresses are rendered consistently, as shown in Listing 9.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xsl:template match="/">
    <xsl:element name="xsl:stylesheet">
      <xsl:element name="xsl:output">
        <xsl:attribute name="method">html</xsl:attribute>
      </xsl:element>
      <xsl:apply-templates />
    </xsl:element>
  </xsl:template>
  <xsl:template match="xs:element[@type='address']">
    <xsl:element name="xsl:template">
      <xsl:attribute name="match"><xsl:value-of select="@name" /></xsl:attribute>
      <xsl:element name="p">
        <xsl:element name="xsl:value-of">
          <xsl:attribute name="select">address1/text()</xsl:attribute>
        </xsl:element>
        <xsl:element name="br" />
        <xsl:element name="xsl:value-of">
          <xsl:attribute name="select">address2/text()</xsl:attribute>
        </xsl:element>
        <xsl:element name="br" />
        <xsl:element name="xsl:value-of">
          <xsl:attribute name="select">concat(city/text(), ', ', 
           state/text(), ' ', 
           zip/text())</xsl:attribute>
        </xsl:element>
      </xsl:element>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>

When the style sheet generator in Listing 9 is run against the original schema, it generates the following style sheet in Listing 10.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html"/>
  <xsl:template match="mailingAddress">
    <p>
      <xsl:value-of select="address1/text()"/>
      <br/>
      <xsl:value-of select="address2/text()"/>
      <br/>
      <xsl:value-of select="concat(city/text(), ', ', 
       state/text(), ' ', zip/text())"/>
    </p>
  </xsl:template>
  <xsl:template match="shippingAddress">
    <p>
      <xsl:value-of select="address1/text()"/>
      <br/>
      <xsl:value-of select="address2/text()"/>
      <br/>
      <xsl:value-of select="concat(city/text(), ', ', 
       state/text(), ' ', zip/text())"/>
    </p>
  </xsl:template>
</xsl:stylesheet>

Using this technique to style complex archetypes is particularly useful when trying to target the same source XML document type for different target media. For example, if you were sending the address information to a cell phone display, you might include only the city and state.


Conclusion

This column outlines the way you can use archetypes to streamline your coding experience. This discussion really only scratches the surface. In a large system that must support many presentation targets (HTML, wireless, other machine consumers) and many different source-document types (for bandwidth reduction or security reasons), using archetypes properly makes it very easy to keep your style sheet output consistent and correct.


Resources

About the author

Kevin Williams is the chief XML architect for Equient, a division of Veridian specializing in XML design for information management systems. He has also co-written several books on XML from Wrox Press. He can be reached for comment at kevin@blueoxide.com. Random XML musings, tips, tricks, and opinionated rants may be found at his Web site www.blueoxide.com.

Comments (Undergoing maintenance)



Trademarks  |  My developerWorks terms and conditions

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=XML
ArticleID=12015
ArticleTitle=XML for Data: Styling with schemas
publish-date=07012001
author1-email=kevin@blueoxide.com
author1-email-cc=

My developerWorks community

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere).

My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Rate a product. Write a review.

Special offers