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) 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 |
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. |
formatTime (time, format, locale) |
Formats the supplied time using the specified formatting options. The format can be the following values: h - Hours (0-23) |
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.