Using XML Queries and Transformations

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 the full specification will be supported in a next release. At the time of printing this book, at least a developers 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-importsimport, 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>

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“The generation of random numbers is too important to be left to chance.” - Robert R. Coveyou