XSL and XSLT Support in XML DOM

[Previous] [Next]

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

NameDescription
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.

click to view at full size.

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

XMLDOMXSLTemplate and XMLDOMXSLProcessor Objects

Beginning with the third release of the Microsoft XML parser, the XMLDOMXSLTemplate object can be used to hold an instance of an XSL or XSLT template. You can create an XMLDOMXSLTemplate object variable in an ASP application for XSL or XSLT documents that may be used frequently or by multiple ASP documents. Since the XSL or XSLT documents will be stored in the XMLDOMXSLTemplate object variable, they will not need to be reloaded or compiled when they are needed. The XMLDOMXSLProcessor object is designed to work with the XMLDOMXSLTemplate object to transform a given document. The XMLDOMXSLTemplate object implements the IXMLDOMXSLTemplate interface and the XMLDOMXSLProcessor object implements the IXMLDOMXSLProcessor interface. The methods and properties of the IXMLDOMXSLTemplate and IXMLDOMXSLProcessor interfaces are as follows:

IXMLDOMXSLTemplate Property

NameDescription
Stylesheet Allows an XSL style sheet to compile to an XSL template

IXMLDOMXSLTemplate Method

NameDescription
CreateProcessor Creates an XMLDOMXSLProcessor object that can use the template

IXMLDOMXSLProcessor Properties

NameDescription
input Sets the XML document that will be transformed.
output Retrieves the output object that will hold the results of the transformation. Can be any object that supports IStream interface, IPersistStream interface, IXMLDOMDocument interface, ASP Response object, and ADODB.stream object.
readyState The current state of the processor object.
startMode The base name of the start mode.
startModeURI Returns the namespace URI of the start mode.
stylesheet The XSL style sheet that will be used to perform the transformation.
ownerTemplate Returns the style sheet template that was used when creating the XSL processor object.

IXMLDOMXSLProcessor Methods

NameDescription
addObject (object, namespaceURI) Used to pass objects (such as an XMLDOMDocument object) to a style sheet.
AddParameter (baseName, parameter, namespaceURI) Used to pass variables into a style sheet. The variables will be referenced by the base name. The parameter can be a number, Boolean, string, XMLDOMNodeList object or XMLDOMNode object. The namespace is optional. You can reference these variables in the style sheet by using <xsl:param>.
Reset() Resets the processor object back to the state the object was in prior to calling the transform method.
SetStartMode (mode, namespace)

This method allows you to perform subsets of a larger XSL transformation. Using this method is the same as adding the following to the XSL style sheet:

 <xsl:template match="/"> <xsl:apply-templates select="*" mode="{mode}"/> </xsl:template> 

The namespace is optional.
Transform() Used to begin the transformation or resume a transformation that returned VARIANT_FALSE.

To see how to use these objects in ASP pages, create a new ASP application. In the global ASP page add the following code:

 Sub Application_OnStart Dim l_oXSLDoc Dim g_oAcmePOXSLTemplate Set l_oXSLDoc = CreateObject("Msxml2.FreeThreadedDOMDocument") Set g_oNorthwindPOXSLTemplate=CreateObject("Msxml2.XSLTemplate") l_oXSLDoc.async = false l_oXSLDoc.load("C:\chapter 12\NorthwindPODOM.xsl") Set g_oNorthwindPOXSLTemplate.stylesheet = l_oXSLDoc Set Application("template") = g_oNorthwindPOXSLTemplate End Sub 

In this example, we created a local document object variable and an XMLDOMXSLTemplate object variable that will have application wide scope, and then we loaded the NorthwindPODOM.xsl XSL document. This document will be compiled and stored in the g_oNorthwindPOXSLTemplate variable and can be used by multiple ASP documents and clients at the same time. To reference this document in an ASP document named NPODOM.asp, we would need to add the following code to the ASP document:

 <%@ Language="VBScript" %> <SCRIPT LANGUAGE="VBScript" RUNAT="Server"> Dim l_oXMLDoc Dim l_oXMLProccessor Set l_oXMLDoc = CreateObject("Msxml2.DOMDocument") l_oXMLDoc.async = false l_oXMLDoc.load("C:\chapter 12\NorthwindPO.xml") set l_oXMLProccessor = Application("template").createProcessor() l_oXMLProccessor.output = Response l_oXMLProccessor.input = l_oXMLDoc l_oXMLProccessor.transform() </SCRIPT> 

In this case, the NorthwindPO.xml document has been transformed in the ASP page using the XSL document stored in the application variable named template.



Developing XML Solutions
Developing XML Solutions (DV-MPS General)
ISBN: 0735607966
EAN: 2147483647
Year: 2000
Pages: 115
Authors: Jake Sturm

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net