Skip to main content

Tip: XSL stylesheets and processor-specific features

Test the platform you're running on and adapt your code accordingly

Benoit Marchal (bmarchal@pineapplesoft.com), Consultant, Pineapplesoft
Benoit Marchal is a Belgian consultant. He is the author of XML by Example, Second Edition and other XML books. Benoit is available to help you with XML projects. You can contact him at bmarchal@pineapplesoft.com or through his personal site at marchal.com.

Summary:  This tip illustrates how to write stylesheets that take advantage of processor-specific features while remaining portable.

View more content in this series

Date:  15 Oct 2003
Level:  Intermediate
Activity:  1303 views

XSLT does a decent job of ensuring interoperability between platforms and processors. Porting a stylesheet to a new processor is typically as simple as copying the file. Still, at times, you need to forgo some portability and adapt your stylesheets to a given processor. This tip shows you how to test the platform you're running on and adapt your code accordingly.

How portable do you want to be?

Why forgo portability? One reason might be because you need a function that XSLT 1.0 does not support -- for example, the ability to output to multiple documents or to create an intermediate result tree. The workarounds are always specific to a given processor.

Another reason might be because you're doing something that is platform-specific. For example, I recently wrote a stylesheet for processing filenames. Since the file separator is unique to each platform ("/" for all UNIX variants, "\" under Windows, and ":" for MacOS classic), the stylesheet has to be platform-specific.

Whatever the reason, it is best to isolate the platform-specific code in a template, a variable, or a parameter so it's easy to port to another platform. The ideal solution is to test which processor you're using in the stylesheet and branch to the appropriate code section. To test the platform, you would use the system-property() function.

As the name implies, system-property() returns the value of system-specific properties. The XSLT standard defines the following three properties:

  • xsl:version: XSL version supported by the processor (currently 1.0)
  • xsl:vendor: the processor vendor
  • xsl:vendor-url: a URL identifying the processor vendor

Processor vendors are free to recognize more properties. Most Java processors also return Java system properties, which include the following:

  • java.version: version of the JVM you're running on
  • java.vendor: JVM manufacturer
  • os.name: OS you're running on
  • file.separator: system-dependent file separator

Many more properties are defined -- see the complete list in Resources.


system-property() in action

Listing 1 demonstrates one application of system-property(). The stylesheet attempts to guess the file separator for your system by querying xsl:vendor and other properties. The stylesheet stores the result in the file-separator parameter. The logic is as follows:

  • If the vendor is Microsoft or Altova (both well-known Windows processors), assume the Windows system separator (/).
  • If the processor accepts Java properties, query the file.separator property. This works with most Java processors such as Xalan or Saxon.
  • If the processor is XT, use a proprietary extension mechanism to retrieve the value of the file.separator property.
  • If all tests fail, terminate.

Listing 1. A stylesheet that tests its environment
                <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xm="http://www.ananas.org/2001/XM/Walk/Directory"
                version="1.0">

<xsl:output method="html"/>

<xsl:param name="file-separator">
 <xsl:variable name="vendor" 
       select="system-property('xsl:vendor')"/>
 <xsl:choose>
  <!-- two well-known XSLT processors for Windows -->
  <xsl:when test="contains($vendor,'Microsoft') 
       or contains($vendor,'Altova')">
   <xsl:text>\</xsl:text>
  </xsl:when>
  <!-- the processor returns Java properties -->
  <xsl:when 
       test="string-length(system-property('java.version')) != 0">
   <xsl:value-of select="system-property('file.separator')"/>
  </xsl:when>
  <!-- the processor is XT, use an extension -->
  <xsl:when test="contains($vendor,'James Clark') and
                  function-available('xt-sys:getProperty')"
       xmlns:xt-sys="http://www.jclark.com/xt/java/java.lang.System">
   <xsl:value-of select="xt-sys:getProperty('file.separator')"/>
  </xsl:when>
  <xsl:otherwise>
   <xsl:message terminate="yes">unknown file separator</xsl:message>
  </xsl:otherwise>
 </xsl:choose>
</xsl:param>

<xsl:template match="xm:Directory">
<html>
 <head><title>File list</title></head>
 <body><ul><xsl:apply-templates/></ul></body>
</html>
</xsl:template>

<xsl:template match="xm:File">
<li><a href="file:{.}">
 <xsl:call-template name="extract-fname">
  <xsl:with-param name="path" select="."/>
 </xsl:call-template>
</a></li>
</xsl:template>

<xsl:template name="extract-fname">
 <xsl:param name="path"/>
 <xsl:choose>
  <xsl:when test="contains($path,$file-separator)">
   <xsl:call-template name="extract-fname">
    <xsl:with-param name="path"
                    select="substring-after($path,$file-separator)"/>
   </xsl:call-template>
  </xsl:when>
  <xsl:otherwise>
   <xsl:value-of select="$path"/>
  </xsl:otherwise>
 </xsl:choose>
</xsl:template>

</xsl:stylesheet>


Conclusion

Ideally you would never need processor-specific code, but we don't live in a perfect world. The next best solution is to isolate the processor-specific code in a variable, a parameter, or a template. Use the system-property() as you port your stylesheet to different processors.


Resources

About the author

Benoit Marchal

Benoit Marchal is a Belgian consultant. He is the author of XML by Example, Second Edition and other XML books. Benoit is available to help you with XML projects. You can contact him at bmarchal@pineapplesoft.com or through his personal site at marchal.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=12329
ArticleTitle=Tip: XSL stylesheets and processor-specific features
publish-date=10152003
author1-email=bmarchal@pineapplesoft.com
author1-email-cc=dwxed@us.ibm.com

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