次の方法で共有


変換における XPathNodeIterator

XPathNodeIterator には、XPath クエリの結果として作成されたノード セットの反復処理、または node-set メソッドを使用してノード セットに変換された結果ツリー フラグメントの反復処理を行うためのメソッドがあります。XPathNodeIterator を使用すると、ノード セット内のノードの反復処理ができます。ノード セットが取得されると、XPathNodeIterator クラスは、それらの選択されたノード セットへの読み取り専用の前方向カーソルを提供します。ノード セットはドキュメント順に作成されるため、このメソッドを呼び出すと、ドキュメント順に次のノードに移動します。XPathNodeIterator では、セット内のすべてのノードのノード ツリーは構築しません。その代わりに、XPathNodeIterator は、データへの単一ノード ウィンドウを提供し、ツリー内での移動に合わせて、自身が指している基になるノードを公開します。XPathNodeIterator クラスのメソッドとプロパティを使用すると、現在のノードの情報を取得できます。メソッドとプロパティの一覧については、「XPathNodeIterator のメンバ」を参照してください。

XPathNodeIterator は XPath クエリから作成されたノード セット内を前方向にだけ移動するため、移動するときは MoveNext メソッドを使用します。このメソッドの戻り値の型は Boolean で、選択されている次のノードに移動すると true を返し、選択されているノードがそれ以上ない場合は false を返します。このメソッドが true を返した場合は、次のプロパティを使用できます。

  • Current
  • CurrentPosition
  • Count

ノード セットを初めて参照するときは、MoveNext を呼び出して、選択されているセットの最初のノードに XPathNodeIterator を位置付ける必要があります。これにより、while ループによる書き込みが可能になります。

XsltArgumentListXPathNodeIterator をパラメータとして XslTransform に渡すコード例を次に示します。コードへの入力は books.xml で、スタイル シートは text.xsl です。ファイル test.xml は、XPathDocument です。

Imports System
Imports System.IO
Imports System.Xml
Imports System.Xml.Xsl
Imports System.Xml.XPath
Imports System.Text

Public Class sample

   Public Shared Sub Main()
      Dim Doc As New XPathDocument("books.xml")
      Dim nav As XPathNavigator = Doc.CreateNavigator()
      Dim Iterator As XPathNodeIterator = nav.Select("/bookstore/book")

      Dim arg As New XsltArgumentList()
      arg.AddParam("param1", "", Iterator)

      Dim xslt As New XslTransform()
      xslt.Load("test.xsl")

      Dim xd As New XPathDocument("test.xml")

      Dim strmTemp = New FileStream("out.xml", FileMode.Create, FileAccess.ReadWrite)
      xslt.Transform(xd, arg, strmTemp, Nothing)
   End Sub 'Main
End Class 'sample
[C#]
using System;
using System.IO;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;
using System.Text;

public class sample
{
    public static void Main()
    {
        XPathDocument Doc = new XPathDocument("books.xml");
        XPathNavigator nav = Doc.CreateNavigator();
        XPathNodeIterator Iterator = nav.Select("/bookstore/book");

        XsltArgumentList arg = new XsltArgumentList();
        arg.AddParam("param1", "", Iterator);

        XslTransform xslt = new XslTransform();
        xslt.Load("test.xsl");

        XPathDocument xd = new XPathDocument("test.xml");

        Stream strmTemp = new FileStream("out.xml", FileMode.Create, FileAccess.ReadWrite);
        xslt.Transform(xd, arg, strmTemp, null);
    }
}

books.xml

<?xml version='1.0'?>
<!-- This file represents a fragment of a book store inventory database -->
<bookstore specialty="novel">
    <book style="autobiography">
    <title>Seven Years in Trenton</title>
        <author>
            <first-name>Jay</first-name>
            <last-name>Adams</last-name>
            <award>Trenton Literary Review Honorable Mention</award>
            <country>USA</country>
        </author>
        <price>12</price>
    </book>
    <book style="textbook">
        <title>History of Trenton</title>
        <author>
            <first-name>Kim</first-name>
            <last-name>Akers</last-name>
            <publication>
                Selected Short Stories of
                <first-name>Scott</first-name>
                <last-name>Bishop</last-name>
                <country>US</country>
            </publication>
        </author>
        <price>55</price>
    </book>
</bookstore>

test.xsl

[Visual Basic, C#]

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">

<xsl:output method="xml" indent="yes"/>
<xsl:param name="param1"/>

<xsl:template match="/">
    <out>
        <xsl:for-each select="$param1/title">
            <title><xsl:value-of select="."/></title>
        </xsl:for-each>
    </out>
</xsl:template>

</xsl:stylesheet>

test.xml

<Title attr="Test">this is a test</Title>

出力 (out.xml)

<?xml version="1.0" encoding="utf-8"?>
<out>
  <title>Seven Years in Trenton</title>
  <title>History of Trenton</title>
</out>

参照

XslTransform クラスによる XSLT プロセッサの実装