HOME  |    TRAINING  |   FREE TUTORIALS   |   JOBS
Find out more about our new RSS feed.
FREE Tutorial
PROFESSIONAL VISUAL BASIC 6 XML PART 5 - XSLT LANGUAGE EXTENSIONS

CATEGORY
SEARCH OUR OTHER TUTORIALS

DESCRIPTION

XSLT processor vendors are free to add their own private extensions to the language. The XSLT specification even specifies how they should indicate if an extension element or extension function is supported by their implementation.
Click here to be kept informed of our new Tutorials.


This free tutorial is a sample from the book Professional Visual Basic 6 for XML.


In the stylesheet, certain namespaces can be specified to be XSLT extension namespaces with the xsl:extension-element-prefixes attribute on the stylesheet element. Elements in those namespaces will be processed using the extensions of the used processor.

If the stylesheet author wants to know if the processor supports a certain extension element, the function element-available() can be called with the element name as the parameter. If the processor supports this element, the function should return true.

The same information can be retrieved about extension functions using the function-available() function.

The IE5 Implementation

When Microsoft released Internet Explorer 5.0, it wanted to ship with it an XML parser that conformed as much as possible to all XML-related standards at that time. XSLT was at that time still a part of the XSL working draft. The XSLT support in IE5 is based on the transformations chapter in the working draft of December 1998. They did quite a good job, but the specification moved on, split itself in two, and by now the MSXML implementation is a very weak and non-compliant version of the now final recommendation of XSLT 1.0. This IE5 implementation of MSXML is version 2.0.

Microsoft has announced that they will support the full specification in a next release. When this book is available, at least a developer's preview is available (called MSXML 2.6). This preview implements the standards much better, but still a lot remains to be done. More information can be found from http://msdn.microsoft.com/downloads/webtechnology/xml/msxml.asp.

The new implementation will support both the W3C XSLT 1.0 recommendation as well as the MSXML 2.0 implementation. Which implementation is used depends on the namespace of the stylesheet elements. The MSXML 2.0 implementation uses the namespace:

xmlns:xsl="http://www.w3.org/TR/WD-xsl

In Appendix D, you can see for each element if it is supported in IE5 (the MSXML 2.0 library). Here we will try to give you a notion of what is unsupported, what is ill-supported and what works fine.

Complete Implementation

MSXML 2.0 does a good job on:

  • literal elements and attributes.
  • the element element, the attribute element, the comment element.
  • the choose, when and otherwise elements.
  • the for-each element.
  • the if element.

Partially Implemented

Some elements can be used in most cases, but fail to support more complex uses or certain attributes. These include:

  • apply-templates: you cannot use the mode attribute.
  • template: you cannot use the name attribute and the mode attribute. The priority rules are not implemented (see the section entitled 'What if Several Templates Match?').
  • processing-instruction: is called pi in MSXML 2.0.
  • stylesheet: IE5 does not support any of the attributes for the stylesheet element. Note that the version attribute is defined as required in XSLT.
  • value-of: disable-output-escaping is not supported. See below for undocumented tricks to do this anyway.

The XPath expressions that can be used in lots of places in XSLT are only partially implemented. Basically only the shorthand notation is supported. For details, see the XPath section earlier in this chapter.

Unsupported

The following elements are not supported in MSXML 2.0:

  • apply-imports, import, include
  • attribute-set (and the related attributes)
  • call-template
  • copy-of
  • key
  • number
  • param, with-param, variable
  • sort (MSXML 2.0 has implemented attributes on some elements to allow sorting)
  • text (MSXML 2.0 has an undocumented cdata element that does more or less the same)
  • transform (which is the same thing as stylesheet anyway)
  • and a whole bunch of top level elements

Although this is a fairly long list, most of these unimplemented elements are the kind you will rarely use anyway. Some of them, however, are dearly missed.

Most of the specified additional functions that can be used in XSLT are unsupported in IE5. At the same time, MSXML 2.0 features some functions that can be very useful in overcoming these shortcomings.

There are some unsupported standard functions:

  • document()
  • key()
  • generate-id(): MSXML 2.0 has a function available called uniqueID() that can do the same
  • format-number(): MSXML 2.0 has a formatNumber() function that works almost identically, except for localization using a decimal-format element
  • current(): IE5 has a very powerful context() function. This can be used to do the same. context(-1) is equivalent to current().

Tricks for using MSXML 2.0

Although MSXML 2.0 has some limitations compared to the full XSLT specification, it is still a very useful transformation tool. When using it, there are some problems that all developers stumble into. The developer community has been looking for solutions and work-arounds for almost two years now. These are a few of the most important ones.

Output Escaping Off

If you have an XML document containing a piece of text that should appear literally in the output, you can run into trouble. The XML parser and XSLT processor will replace some characters with XML entities, to keep the output well-formed. That is fine, but sometimes we don't care whether the output is well-formed, we just want that exact string to appear in the output. The output is not supposed to be XML anyway - it might be HTML. XSLT allows us to do so by using the disable-output-escaping on the value-of and text elements. IE5 does not support disable-output-escaping, but it does allow the use of an undocumented attribute: no-entities='true' on the eval element. We can use this to generate unescaped content, for example, using the following code:

<Author>Teun <![CDATA[<BR>]]> Duynstee </Author>

with the following template:

<xsl:template match="Author/text()">
<xsl:eval no-entities='true'>this.nodeValue</xsl:eval>
</xsl:template>

This would generate this output:

Teun <BR> Duynstee

Note that this is not well-formed XML, but that was exactly what we where trying to do. But this also means that we must be very careful using this feature. Note also that this feature is undocumented, so Microsoft might remove it from future versions just when you least expect it.

Local Templates

IE5 does not support modes and calling templates by name, but it does allow something else: locally scoped templates. These can be included as a child element of an apply-templates element and the processor will try to use this template before any of the globally scoped templates. Look at this sample:

<xsl:template match="Author">
<b><xsl:value-of/></b>
</xsl:template>

<xsl:template match="/">
<xsl:apply-templates select="//Author"/>
<xsl:apply-templates select="//Author">
 <xsl:template match="Author">
  <i><xsl:value-of/></i>
 </xsl:template>
</xsl:apply-templates>
</xsl:template>

The stylesheet has a template defined for use with Author elements. It generates a b element with the Author element's content in it. The root template performs two apply-template actions on all authors in the source document. The first one will match on the template for Author elements and output the following:

<b>Teun Duynstee</b>

The second apply-templates element has a template defined locally. This local template also matches the selected nodes, so the second apply-templates element will generate:

<i>Teun Duynstee</i>

XSLT Examples

Let's have a look at some more examples to demonstrate the use of XSLT. In the last part of this section, we will look at using XSLT to style an XML document in HTML. There will be more examples there. Here we will cover examples that are not HTML-related, but targeted to converting one XML dialect into another. This will be a very common case in business-to-business e-commerce, where XML documents containing orders, inventories, product descriptions, etc., are sent automatically and converted on the fly to a format that is suitable for the target system.

Continued...


NEXT PAGE



5 RELATED COURSES AVAILABLE
HTML 4.0 INTRODUCTION
To create, format and publish a small website using HTML 4.0. You will learn to create web pages incorporating fo....
MICROSOFT INTERNET EXPLORER 6.0 INTERNET INTRODUCTION
This course provides readers with an introduction to the concept of the Internet and the opportunity to gain a br....
A+ MODULE 5 - THE INTERNET
At the end of this course you will be able to: describe the functions of an operating system, describe the featur....
JAVASCRIPT PROGRAMMING
This training course aims to teach the reader the fundamentals of JavaScript. This course covers topics such as -....
I-NET+ MODULE 8 - DEVELOPING A WEB SITE
On completion of this module, readers will be able to: create HTML pages incorporating different document-, parag....
 
0 RELATED JOBS AVAILABLE
CONTACT US
Saturday 22nd November 2008  © COPYRIGHT 2008 - VISUALSOFT