Microsoft XML SDK 2.6 - XML Reference

transformNodeToObject Method

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

obj.transformNodeToObject(stylesheet, outputObject);

Parameters

stylesheet
Object. Valid XML document or DOM node that consists of XSLT elements that direct the transformation of this node.
outputObject
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.

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 stand-alone style sheet fragment.

The source node defines a context for the style sheet to operate on, but navigation outside this scope is allowed. For instance, a style sheet could use the id function to access other parts of the document.

This method supports both stand-alone 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 ASP Response.Write or Response.BinaryWrite calls.

For more information about XSLT, see the XSLT Reference.

This member is an extension of the W3C DOM.

Example

The following 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");
  source.async = false
  source.load("data.xml");
 
  // Load style sheet.
  var stylesheet = new ActiveXObject("Msxml2.DOMDocument");
  stylesheet.async = false
  stylesheet.load("style.xsl");
 
  // Set up the resulting document.
  var result = new ActiveXObject("Msxml2.DOMDocument");
  result.async = false
  result.validateOnParse = true;
 
  // Parse results into a result DOM Document.
  source.transformNodeToObject(stylesheet, result); 
</SCRIPT>

Visual Basic Example

This sample demonstrates the application of multiple style sheets to an XML file in succession:

  Dim Source As New MSXML2.DOMDocument
  Dim stylesheet As New MSXML2.DOMDocument
  Dim stylesheet2 As New MSXML2.DOMDocument
  Dim result As New MSXML2.DOMDocument
  Dim result2 As New MSXML2.DOMDocument
  
  ' Load data.
  Source.async = False
  Source.Load "sample.xml"
 
  ' Load style sheet.
  stylesheet.async = False
  stylesheet.Load "stylesheet1.xsl"
 
  ' 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

Sample.xml

<?xml version="1.0"?>
<COLLECTION dateCreated="01-04-2000">
 <BOOK>
    <TITLE>Home Town</TITLE>
    <AUTHOR>Tracy Kidder</AUTHOR>
    <PUBLISHER>Random House</PUBLISHER>
    <PRICE>250</PRICE>
 </BOOK>
  <BOOK>
    <TITLE>Cosmos</TITLE>
    <AUTHOR>Carl Sagan</AUTHOR>
    <PUBLISHER>Ballantine Books</PUBLISHER>
    <PRICE>200</PRICE>
 </BOOK>
 <BOOK>
    <TITLE>Catwings</TITLE>
    <AUTHOR>Ursula K. Le Guin</AUTHOR>
    <PUBLISHER>Scholastic</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>Catwings</TITLE><AUTHOR>Ursula K. Le Guin</AUTHOR><PUBLISHER>Scholastic</PUBLISHER><PRICE>100</PRICE></BOOK><BOOK><TITLE>Cosmos</TITLE><AUTHOR>Carl Sagan</AUTHOR><PUBLISHER>Ballantine Books</PUBLISHER><PRICE>200</PRICE></BOOK></LowPriceBooks>

See Also

Using the XSLT Processor

Applies To: XMLDOMAttribute Object | XMLDOMCDATASection Object | XMLDOMCharacterData Object | XMLDOMComment Object | DOMDocument Object | XMLDOMDocumentFragment Object | XMLDOMDocumentType Object | XMLDOMElement Object | XMLDOMEntity Object | XMLDOMEntityReference Object | XMLDOMNode Object | XMLDOMNotation Object | XMLDOMProcessingInstruction Object | XMLDOMText Object | XTLRuntime Object