XML

XSL and XSLT Support in XML DOM

Now that we have covered XSL and XSLT documents, we can cover the XTLRuntime object from the XML parser that shipped with Internet Explorer 5. The XTLRuntime object is an extension to the W3C DOM. As we mentioned in Chapter 11, the XTLRuntime object works with XSL. This object implements the IXTLRuntime interface. The methods implemented by the IXTLRuntime interface can be called from within XSL or XSLT documents. Let's look at the properties and methods of the IXTLRuntime interface first.

IXTLRuntime Interface

The IXTLRuntime interface inherits the IXMLDOMNode object interface. In addition to the properties and methods of the IXMLDOMNode interface, IXTLRuntime extends the IXMLDOMNode interface with a set of methods that are accessible to style sheets. The extended methods are as follows:

Extended IXTLRuntime Methods

Name Description
absoluteChildNumber (node)* Returns the index of the node relative to all its siblings within its parent's child node list. The first node in the child node list is number 1.
ancestorChildNumber (nodeName, node)* Returns the index of the nearest ancestor of a node in the child node list with the name nodeName.
childNumber (node)* Returns the first node in the child node list with the same node name.
depth (startNode)* Returns the depth of a level within the document tree at which the startNode appears.
formatDate (date, format, locale)

Formats the supplied date using the specified formatting options. The following formats are allowed: m - Month (1-12)
mm - Month (01-12)
mmm - Month (Jan-Dec)
mmmm - Month (January - December)
mmmmm - Month, first letter
d - Day (1-31)
dd - Day (01-31)
dddd - Day (Sunday-Saturday)
yy - Year (00-99)
yyyy - Year (1900-9999)

The locale determines the sequence of values in the date. The default is month-day-year.

formatIndex (number, format) Formats the supplied integer using the specified numerical system. The format can be one of the following:

1 - Standard numbering system
01 - Standard numbering with leading zeros
A - Uppercase letter sequence "A" to "Z", then "AA" to "ZZ"
a - Lowercase letter sequence "a" to "z", then "aa" to "zz"
I - Uppercase Roman numerals, such as I, II, III, and IV
i - Lowercase Roman numberals, such as i, ii, iii, and iv

formatNumber (number, format)

Formats the supplied number using the specified format. The format string can have one or more of the following characters: # - Displays only significant digits and omit the insignificant digits.
0 - Displays insignificant zeros if a number has fewer digits than there are zeros in the format.
? - Adds spaces for the insignificant zeros on either side of the decimal points, so that decimal points align when using a fixed-point font.
. - Indicates the position of the decimal point.
, - Displays a thousands separator or scales a number by a multiple of one thousand.
% - Displays the number as a percentage.
E or e - Displays the number in
scientific format.
E- or e- - Places a negative sign by negative exponents.
E+ or e+ - Places a negative sign by a negative exponent and a plus sign by a positive exponent.

formatTime (time, format, locale)

Formats the supplied time using the specified formatting options. The format can be the following values: h - Hours (0-23)
hh - Hours (00 - 23)
m - Minutes (0-59)
mm- Minutes (00-59)
s - Seconds (0-59)
ss - Seconds (00-59)
AM/PM - Adds AM or PM and displays value in a 12 hour format
am/pm - Adds am or pm and displays value in 12 hour format
A/P - Adds A or P and displays the value in a 12 hour format
a/p - Adds a or p and displays the value in a 12 hour format
[h]:mm - Displays the elapsed time in hours
[mm]:ss - Displays the elapsed time in minutes
[ss] - Displays the elapsed time in
seconds
ss.00 - Displays fractions of a second
The locale is used to determine the correct separator characters.

uniqueID (node) Returns the unique identifier for the supplied node.

To reference these methods in an XSL or XSLT document, you need to use the eval element in your style sheets.

As mentioned, the eval element evaluates a script expression and generates a text string. We can create a new XSL document named NorthwindPODOM.xsl for the NorthwindPO.xml document that uses the methods of the IXTLRuntime interface as follows:

  <?xml version="1.0"?>
  <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
     <xsl:template match="/">
        <xsl:for-each match="//">
           <html>
           <body style="{color:black}">
              nodeName: <xsl:node-name />
              Absolute Child Number:
              <xsl:eval>absoluteChildNumber(this)</xsl:eval>
              Depth:
              <xsl:eval>depth(this)</xsl:eval>
              <br></br>
              <xsl:for-each match=".//">
                 <p style="{color:blue}">
                    nodeName: <xsl:node-name />
                 Absolute Child Number:
                 <xsl:eval>absoluteChildNumber(this)</xsl:eval>
                 Depth:
                 <xsl:eval>depth(this)</xsl:eval>
                 <br></br>
                 </p>
                 <xsl:for-each match=".//">
                    <p style="{color:green}">
                       nodeName: <xsl:node-name />
                       Absolute Child Number:
                       <xsl:eval>absoluteChildNumber(this)
                       </xsl:eval>
                       Depth:
                       <xsl:eval>depth(this)</xsl:eval>
                       <br></br>
                    </p>
                 </xsl:for-each>
              </xsl:for-each>
           </body>
           </html>
        </xsl:for-each>
     </xsl:template>
  </xsl:stylesheet>

In this code, we use three for-each element loops to apply templates to all the nodes in the top-three levels of the XML document. We also retrieve the name of the selected node, the absolute child number of the selected node, and the depth within the document tree at which the selected node appears. The results of applying this XSL document to the NorthwindPO.xml document is shown in Figure 12-5.

Figure 12-5. The NorthwindPO.xml document using the XTLRuntime object in the style sheet.