Procedura: trasformare un frammento di nodo

Quando si trasformano i dati contenuti in un oggetto XmlDocument o in un oggetto XPathDocument, le trasformazioni XSLT si applicano a un documento completo. In altre parole, se viene passato un nodo diverso dal nodo radice del documento, il processo di trasformazione accederà comunque a tutti i nodi nel documento caricato. Per trasformare un frammento di nodo, è necessario creare un oggetto contenente solo il frammento di nodo e passare tale oggetto al metodo Transform.

Procedure

Per trasformare un frammento di nodo

  1. Creare un oggetto contenente il documento di origine.

  2. Individuare il frammento di nodo da trasformare.

  3. Creare un oggetto separato con il frammento di nodo.

  4. Passare il frammento di nodo al metodo Transform.

Esempio

Nell'esempio seguente un frammento di nodo viene trasformato e il risultato viene visualizzato sulla 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

Il titolo del libro è L'uomo di fiducia.

Vedi anche