transformNodeToObject Method

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - DOM Reference

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.

[Script]

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

<SCRIPT>
   // Load data.
   var source = new ActiveXObject("Msxml2.DOMDocument.5.0");
   source.async = false;
   source.load("data.xml");
   if (xmlDoc.parseError.errorCode <> 0) {
      var myErr = xmlDoc.parseError;
      alert("You have error " + myErr.reason);
   } else {
        // Load style sheet.
      var stylesheet = new ActiveXObject("Msxml2.DOMDocument.5.0");
      stylesheet.async = false;
      stylesheet.load("style.xsl");
      if (xmlDoc.parseError.errorCode <> 0) {
         var myErr = xmlDoc.parseError;
         alert("You have error " + myErr.reason);
      } else {
         // Set up the resulting document.
         var result = new ActiveXObject("Msxml2.DOMDocument.5.0");
         result.async = false;
         result.validateOnParse = true;

         // Parse results into a result DOM Document.
         source.transformNodeToObject(stylesheet, result);
      }
   }
}
</SCRIPT>
[Visual Basic]

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.

Dim Source As New Msxml2.DOMDocument50
Dim stylesheet As New Msxml2.DOMDocument50
Dim stylesheet2 As New Msxml2.DOMDocument50
Dim result As New Msxml2.DOMDocument50
Dim result2 As New Msxml2.DOMDocument50

' Load data.
Source.async = False
Source.Load "sample.xml"
If (xmlDoc.parseError.errorCode <> 0) Then
   Dim myErr
   Set myErr = xmlDoc.parseError
   MsgBox("You have error " & myErr.reason)
Else
   ' Load style sheet.
   stylesheet.async = False
   stylesheet.Load "stylesheet1.xsl"
   If (xmlDoc.parseError.errorCode <> 0) Then
      Dim myErr
      Set myErr = xmlDoc.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

File Name: 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>

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>

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>

Final 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++]

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.

C/C++ 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.

// 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_DOMDocument50, 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.

To view reference information for Visual Basic, C/C++, or Script only, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

Using XSLT with DOM or SAX | id Function

Applies to: IXMLDOMAttribute | IXMLDOMCDATASection | IXMLDOMCharacterData | IXMLDOMComment | DOMDocument | IXMLDOMDocumentFragment | IXMLDOMDocumentType | IXMLDOMElement | IXMLDOMEntity | IXMLDOMEntityReference | IXMLDOMNode | IXMLDOMNotation | IXMLDOMProcessingInstruction | IXMLDOMText