Muokkaa

Jaa


How to: Transform a Node Fragment

When you transform data contained in an XmlDocument or XPathDocument object the XSLT transformations apply to a document as a whole. In other words, if you pass in a node other than the document root node, this does not prevent the transformation process from accessing all nodes in the loaded document. To transform a node fragment, you must create a separate object containing just the node fragment, and pass that object to the Transform method.

Procedures

To transform a node fragment

  1. Create an object containing the source document.

  2. Locate the node fragment you wish to transform.

  3. Create separate object with just the node fragment.

  4. Pass the node fragment to the Transform method.

Example

The following example transforms a node fragment and outputs the results to the console.

// Load an XPathDocument.
XPathDocument doc = new XPathDocument("books.xml");

// Locate the node fragment.
XPathNavigator nav = doc.CreateNavigator();
XPathNavigator myBook = nav.SelectSingleNode("descendant::book[@ISBN = '0-201-63361-2']");

// Create a new object with just the node fragment.
XmlReader reader = myBook.ReadSubtree();
reader.MoveToContent();

// Load the style sheet.
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load("single.xsl");

// Transform the node fragment.
xslt.Transform(reader, XmlWriter.Create(Console.Out, xslt.OutputSettings));
' Load an XPathDocument.
Dim doc As XPathDocument = New XPathDocument("books.xml")

' Locate the node fragment.
Dim nav As XPathNavigator = doc.CreateNavigator()
Dim myBook As XPathNavigator = nav.SelectSingleNode("descendant::book[@ISBN = '0-201-63361-2']")

' Create a new object with just the node fragment.
Dim reader As XmlReader = myBook.ReadSubtree()
reader.MoveToContent()

' Load the style sheet.
Dim xslt As XslCompiledTransform = New XslCompiledTransform()
xslt.Load("single.xsl")

' Transform the node fragment.
xslt.Transform(reader, XmlWriter.Create(Console.Out, xslt.OutputSettings))

Input

books.xml
<bookstore>
  <book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
    <title>The Autobiography of Benjamin Franklin</title>
    <author>
      <first-name>Benjamin</first-name>
      <last-name>Franklin</last-name>
    </author>
    <price>8.99</price>
  </book>
  <book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
    <title>The Confidence Man</title>
    <author>
      <first-name>Herman</first-name>
      <last-name>Melville</last-name>
    </author>
    <price>11.99</price>
  </book>
  <book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">
    <title>The Gorgias</title>
    <author>
      <name>Plato</name>
    </author>
    <price>9.99</price>
  </book>
</bookstore>
single.xsl
<stylesheet version="1.0" xmlns="http://www.w3.org/1999/XSL/Transform" >
   <output method="text" /> 
   <template match="/">
      Book title is <value-of select="//title" /> 
   </template>
</stylesheet>

Output

Book title is The Confidence Man.

See also