Ask the Wireless Pro 10-Minute Solution

Using XML Stylesheet Language Transformations
By Jeromy Hill

XML stylesheets are helpful in addressing your concern, as a developer, about how best to support multiple browsers on multiple devices. Web developers have applied the concept of stylesheets for a while now. They create a source document and a set of rules for transforming that document into any output type that they could possibly expect. When a change is requested, developers only need to change the source document rather than each of the resultant pages. This also provides them with a quick way to add devices that can read their Web apps.

In this 10-Minute Solution I show you a sample piece of XML code for a library application that lists a book, its author, and a few other bits of information.

<?xml version="1.0" ?>
<library>
  <book>
     <book-title> Huckleberry Finn </book-title>
     <book-author> Mark Twain </book-author>
     <number-of-pages> 300 </number-of-pages>
     <checked-out> False </checked-out>
  </book>
</library>
This code is standard XML with its own tags, defined in a related DTD (Document Type Definition).

Here are the XML stylesheet transformations (with numbered lines) that process that information from raw XML format:

  1. <?xml version="1.0" ?>
  2. <xsl:stylesheet xmlns:xsl=http://www.w3.org/TR/WD-xsl>
  3. <xsl:template match="/">
  4. <xsl:pi name="xml">version="1.0"</xsl:pi>
  5. <![CDATA [
  6. <!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN"
  7. http://www.wapforum.org/DTD/wml_1.1.xml>
  8. ]]>
  9. <wml>
  10. <card id="index" title="Library">
  11. <p> Library holdings: </p>
  12. <xsl:apply-templates select="//book" />
  13. </card>
  14. </wml>
  15. </xsl:template>
  16. <xsl:template match="book">
  17. </xsl:apply-templates select="book-title" />
  18. </xsl:template>
  19. <xsl:template match="book-title">
  20. <xsl:element name="do">
  21. <xsl:attribute name="label"><xsl:value-of /></xsl:attribute>
  22. <xsl:attribute name="type">show</xsl:attribute>
  23. </xsl:element>
  24. </xsl:template>
  25. </xsl:stylesheet>
This is what each group of code lines does:
  • Line 1 should look familiar. I'm just declaring an XML document. Line 2 is where I actually start letting the parser know that I'm using a stylesheet.
  • Line 4 opens the first new tag for XSLT. This tag lets the parser know that I'm going to search for the root of the source XML document and perform some transformation on it. The template is going to look for all instances of the match that I specify and perform some action on it. In this case, I'm looking for the root of the document, or just "/".
  • Line 5 indicates that I am going to start the processing instruction element, or "pi." It also later goes on in lines 6–9 to define the location for the WML DTD.
  • Lines 10–15 should look pretty familiar. The one exception comes on line 13. This is where the stylesheet really comes in handy. What I'm doing here is something similar to a SQL statement. I'm going through and pulling out all the instances of the book element. This is also referred to as "setting the context." The parser will later go to line 19 and drill down the context to just the book titles.
  • Lines 18–20 are there to drill down the context so that I get only the book titles. In lines 22–27 I am actually setting the "do" element to provide me with a list of all the books currently in the library.
  • Line 29 closes out the stylesheet.
The best way to understand this code is to compare the original XML document with the output and see if you can logically follow the transformation. Once you can understand that, the beauty of stylesheets becomes apparent.

My XML document above translates into the following WML code:

<?xml version="1.0" ?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN"
         http://www.wapforum.org/DTD/wml_1.1.xml>
<wml>
 <card id="index" title="Library">
 <p> Library holdings: </p>
<do label="Huckleberry Finn" type="show">
</do>
</card>
</wml>
A lot of work for just a little bit of code, but you can also use that same XML source for any device or output language. XSLT can save you hours upon hours of work. By using them as extensively as possible, you can be sure to provide tons of content to as many people as possible.

Return to Get Help with Wireless Dev Page

Return to Main Get Help Page
 
Other 10-Minute Solutions
 Anatomy of a WAP Site
 Writing Java for the Palm
 Configuration of IIS for WML MIME Content Types
 Making Dynamic Content Available via ASP
 Building a Web Clipping Application
 Examining the Wireless Telephony Application Interface
 Using XML Stylesheet Language Transformations
 Creating Banner Ads for Wireless Sites
 Taking a Look at the WAP Stack's Security Level
 Create Novel Wireless Features with Refresh and Timer Tag Combinations
 Visually Enhancing Wireless Web Sites
 Creating Controls by Hand in the .NET Compact Framework


Sponsored Links


Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map
Jupiterweb networks

internet.comearthweb.comDevx.comClickZ

Search Jupiterweb:

Jupitermedia Corporation has four divisions:
JupiterWeb, JupiterResearch, JupiterEvents, and JupiterImages

Copyright 2004 Jupitermedia Corporation All Rights Reserved.
Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Jupitermedia Corporate Info | Newsletters | Tech Jobs | E-mail Offers