XML DOM Methods


transformNodeToObject Method

Processes this node and its children using the supplied XSL Transformations (XSLT) style sheet and returns the resulting transformation in the supplied object.

JScript Syntax

oXMLDOMNode.transformNodeToObject(stylesheet, outputObject);

Parameters

stylesheet

An object. A valid XML document or DOM node that consists of XSLT elements that direct the transformation of this node.

outputObject

An object. On return, contains the product of the transformation of this XML document based on the XSLT style sheet. If the variant represents the DOMDocument object, the document is built according to its properties and its child nodes are replaced during this transformation process. The XML transformation can also be sent to a stream.

Example

The following Microsoft JScript example sets up the new XML document object named result before making the call to transformNodeToObject.

NoteNote

You can use hello.xml and hello.xsl in the Hello World! (XSLT) topic topic to run this sample code.

JScript
// Load data.
var source = new ActiveXObject("Msxml2.DOMDocument.3.0");
source.async = false;
source.load("hello.xml");
if (source.parseError.errorCode != 0) {
   var myErr = source.parseError;
   WScript.Echo("You have error " + myErr.reason);
} else {
   // Load style sheet.
   var stylesheet = new ActiveXObject("Msxml2.DOMDocument.3.0");
   stylesheet.async = false;
   stylesheet.load("hello.xsl");
   if (stylesheet.parseError.errorCode != 0) {
      var myErr = stylesheet.parseError;
      WScript.Echo("You have error " + myErr.reason);
   } else {
      // Set up the resulting document.
      var result = new ActiveXObject("Msxml2.DOMDocument.3.0");
      result.async = false;
      result.validateOnParse = true;
      // Parse results into a result DOM Document.
      WScript.Echo(source.transformNodeToObject(stylesheet, result));
   }
}

Visual Basic Syntax

oXMLDOMNode.transformNodeToObject(stylesheet, outputObject)

Parameters

stylesheet

An object. A valid XML document or DOM node that consists of XSLT elements that direct the transformation of this node.

outputObject

An object. On return, contains the product of the transformation of this XML document based on the XSLT style sheet. If the variant represents the DOMDocument object, the document is built according to its properties and its child nodes are replaced during this transformation process. The XML transformation can also be sent to a stream.

Example

The following Microsoft Visual Basic example demonstrates the application of multiple style sheets to an XML file in succession.

Visual Basic
Dim Source As New Msxml2.DOMDocument60
Dim stylesheet As New Msxml2.DOMDocument60
Dim stylesheet2 As New Msxml2.DOMDocument60
Dim result As New Msxml2.DOMDocument60
Dim result2 As New Msxml2.DOMDocument60

' Load data.
Source.async = False
Source.Load App.Path & "\sample.xml"
If (Source.parseError.errorCode <> 0) Then
   Dim myErr
   Set myErr = Source.parseError
   MsgBox("You have error " & myErr.reason)
Else
   ' Load style sheet.
   stylesheet.async = False
   stylesheet.Load "stylesheet1.xsl"
   If (stylesheet.parseError.errorCode <> 0) Then
      Dim myErr
      Set myErr = stylesheet.parseError
      MsgBox("You have error " & myErr.reason)
   Else
      ' Set up the resulting document.
      result.async = False
      result.validateOnParse = True
      result2.async = False
      result2.validateOnParse = True

      ' Parse results into a result DOM Document.
      Source.transformNodeToObject stylesheet, result

      stylesheet2.async = False
      stylesheet2.Load "stylesheet2.xsl"

      result.transformNodeToObject stylesheet2, result2
      MsgBox result2.xml
   End If
End If

Resource file: Sample.xml

<?xml version="1.0"?>
<COLLECTION dateCreated="01-04-2000">
 <BOOK>
    <TITLE>Splish Splash</TITLE>
    <AUTHOR>Paula Thurman</AUTHOR>
    <PUBLISHER>Scootney</PUBLISHER>
    <PRICE>250</PRICE>
 </BOOK>
  <BOOK>
    <TITLE>Lover Birds</TITLE>
    <AUTHOR>Cynthia Randall</AUTHOR>
    <PUBLISHER>Lucerne Publishing</PUBLISHER>
    <PRICE>200</PRICE>
 </BOOK>
 <BOOK>
    <TITLE>The Sundered Grail</TITLE>
    <AUTHOR>Eva Corets</AUTHOR>
    <PUBLISHER>Lucerne Publishing</PUBLISHER>
    <PRICE>100</PRICE>
 </BOOK>
</COLLECTION>

Resource file: Stylesheet1.xsl

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
   <PriceList>
      <xsl:for-each select="COLLECTION/BOOK">
   <xsl:sort select="TITLE" data-type="text"/>
   <xsl:copy>
   <xsl:apply-templates select="*"/>
   </xsl:copy>
      </xsl:for-each>
   </PriceList>
  </xsl:template>

  <xsl:template match="*">
   <xsl:copy>
   <xsl:apply-templates />
   </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

Resource file: Stylesheet2.xsl

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
   <LowPriceBooks>
      <xsl:for-each select="*/BOOK[not(PRICE >'220')]">
   <xsl:copy>
   <xsl:apply-templates select="*"/>
   </xsl:copy>
      </xsl:for-each>
   </LowPriceBooks>
  </xsl:template>
  <xsl:template match="*">
   <xsl:copy>
   <xsl:apply-templates />
   </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

Output

<?xml version="1.0"?>
<LowPriceBooks><BOOK>
<TITLE>The Sundered Grail</TITLE>
<AUTHOR>Eva Corets</AUTHOR><PUBLISHER>Lucerne Publishing</PUBLISHER>
<PRICE>100</PRICE></BOOK><BOOK><TITLE>Lover Birds</TITLE>
<AUTHOR>Cynthia Randall</AUTHOR>
<PUBLISHER>Lucerne Publishing</PUBLISHER><PRICE>200</PRICE>
</BOOK></LowPriceBooks>

C/C++ Syntax

HRESULT transformNodeToObject(
    IXMLDOMNode *stylesheet,
    VARIANT outputObject);

Parameters

stylesheet[in]

A valid XML document or DOM node that consists of XSL elements that direct the transformation of this node.

outputObject[in]

An object that contains the product of the transformation of this XML document based on the XSLT style sheet. If the variant represents DOMDocument, the document is built according to its properties and its child nodes are replaced during this transformation process. If the variant contains an IStream interface, the XML transformation is sent to this stream.

Return Values

S_OK

The value returned if successful.

E_INVALIDARG

The value returned if the stylesheet or outputObject parameter is Null.

Example

This example sets up the new DOMDocument object and puts it into a VARIANT before making the call to transformNodeToObject.

C++
// p is the XML source that is to be transformed, pXSL is the style sheet.
// Create an empty DOM document for the result. (Error checking omitted 
// for brevity.)
hr = CoCreateInstance(CLSID_DOMDocument60, NULL, CLSCTX_INPROC_SERVER,
    IID_IXMLDOMDocument, (void**)&pDoc); 
hr = pDoc->QueryInterface(IID_IDispatch, (void **)&pDisp);
vObject.vt = VT_DISPATCH;   // the new object 
vObject.pdispVal = pDisp; 
hr = p->transformNodeToObject(pXSL, vObject);   // Transformation is
                                                // present in pDoc.

Remarks

The stylesheet parameter must be either a DOMDocument node, in which case the document is assumed to be an XSLT style sheet, or a DOM node in the XSLT style sheet, in which case this node is treated as a standalone style sheet fragment.

The source node defines a context in which the style sheet operates, but navigation outside this scope is allowed. For example, a style sheet can use the id function to access other parts of the document.

This method supports both standalone and embedded style sheets and also provides the ability to run a localized style sheet fragment against a particular source node.

The transformNodeToObject method always generates a Unicode byte-order mark, which means it cannot be used in conjunction with other Active Server Pages (ASP) Response.Write or Response.BinaryWrite calls.

For more information about XSLT, see the XSLT Reference.

This member is an extension of the World Wide Web Consortium (W3C) DOM.

Versioning

Implemented in: MSXML 3.0 and later

See Also

Page view tracker