XPathExpression.Compile メソッド

定義

指定された XPath 式をコンパイルし、XPath 式を表す XPathExpression オブジェクトを返します。

オーバーロード

Compile(String)

指定された XPath 式をコンパイルし、XPath 式を表す XPathExpression オブジェクトを返します。

Compile(String, IXmlNamespaceResolver)

指定された XPath 式を、名前空間の解決用に指定された IXmlNamespaceResolver オブジェクトを使用してコンパイルし、XPath 式を表す XPathExpression オブジェクトを返します。

Compile(String)

指定された XPath 式をコンパイルし、XPath 式を表す XPathExpression オブジェクトを返します。

public:
 static System::Xml::XPath::XPathExpression ^ Compile(System::String ^ xpath);
public static System.Xml.XPath.XPathExpression Compile (string xpath);
static member Compile : string -> System.Xml.XPath.XPathExpression
Public Shared Function Compile (xpath As String) As XPathExpression

パラメーター

xpath
String

XPath 式。

戻り値

XPathExpression

XPathExpression オブジェクト。

例外

XPath 式のパラメーターが有効な XPath 式ではありません。

XPath 式が有効ではありません。

次の例は、XPath 戻り値の型を使用して XPath 式を処理する方法を決定する方法を示しています。 この例では、メソッドを Compile 使用して新しい XPathExpression オブジェクトを返します。

public ref class Sample
{
public:
   static void Evaluate( XPathExpression^ expr, XPathNavigator^ nav )
   {
      XPathNodeIterator^ i = nav->Select(expr);
      switch ( expr->ReturnType )
      {
         case XPathResultType::Number:
            Console::WriteLine( nav->Evaluate( expr ) );
            break;

         case XPathResultType::NodeSet:
            while ( i->MoveNext() )
                        Console::WriteLine( i->Current );
            break;

         case XPathResultType::Boolean:
            if ( *safe_cast<bool^>(nav->Evaluate( expr )) )
                        Console::WriteLine( "True!" );
            break;

         case XPathResultType::String:
            Console::WriteLine( nav->Evaluate( expr ) );
            break;
      }
   }

};

int main()
{
   XPathDocument^ doc = gcnew XPathDocument( "contosoBooks.xml" );
   XPathNavigator^ nav = doc->CreateNavigator();
   XPathExpression^ expr1 = nav->Compile( ".//price/text()*10" ); // Returns a number.

   XPathExpression^ expr2 = nav->Compile( "bookstore/book/price" ); // Returns a nodeset.

   Sample^ MySample = gcnew Sample;
   MySample->Evaluate( expr1, nav );
   MySample->Evaluate( expr2, nav );
}
using System;
using System.Xml;
using System.Xml.XPath;

public class XPathExpressionExample
{
    public static void Main()
    {
        XPathDocument document = new XPathDocument("contosoBooks.xml");
        XPathNavigator navigator = document.CreateNavigator();

        XPathExpression expression1 = XPathExpression.Compile(".//bk:price/text()*10"); // Returns a number.
        XPathExpression expression2 = XPathExpression.Compile("bk:bookstore/bk:book/bk:price"); // Returns a nodeset.

        XmlNamespaceManager manager = new XmlNamespaceManager(navigator.NameTable);
        manager.AddNamespace("bk", "http://www.contoso.com/books");

        expression1.SetContext(manager);
        expression2.SetContext(manager);

        Evaluate(expression1, navigator);
        Evaluate(expression2, navigator);
    }

    public static void Evaluate(XPathExpression expression, XPathNavigator navigator)
    {
        switch (expression.ReturnType)
        {
            case XPathResultType.Number:
                Console.WriteLine(navigator.Evaluate(expression));
                break;

            case XPathResultType.NodeSet:
                XPathNodeIterator nodes = navigator.Select(expression);
                while (nodes.MoveNext())
                {
                    Console.WriteLine(nodes.Current.ToString());
                }
                break;

            case XPathResultType.Boolean:
                if ((bool)navigator.Evaluate(expression))
                    Console.WriteLine("True!");
                break;

            case XPathResultType.String:
                Console.WriteLine(navigator.Evaluate(expression));
                break;
        }
    }
}
Imports System.Xml
Imports System.Xml.XPath

Public Class XPathExpressionExample

    Public Shared Sub Main()
        Dim document As XPathDocument = New XPathDocument("contosoBooks.xml")
        Dim navigator As XPathNavigator = document.CreateNavigator()

        Dim expression1 As XPathExpression = XPathExpression.Compile(".//bk:price/text()*10")  ' Returns a number.
        Dim expression2 As XPathExpression = XPathExpression.Compile("bk:bookstore/bk:book/bk:price")  ' Returns a nodeset.

        Dim manager As XmlNamespaceManager = New XmlNamespaceManager(navigator.NameTable)
        manager.AddNamespace("bk", "http://www.contoso.com/books")

        expression1.SetContext(manager)
        expression2.SetContext(manager)

        Evaluate(expression1, navigator)
        Evaluate(expression2, navigator)

    End Sub

    Public Shared Sub Evaluate(ByVal expression As XPathExpression, ByVal navigator As XPathNavigator)

        Select Case expression.ReturnType
            Case XPathResultType.Number
                Console.WriteLine(navigator.Evaluate(expression))
                Exit Sub

            Case XPathResultType.NodeSet
                Dim nodes As XPathNodeIterator = navigator.Select(expression)
                While nodes.MoveNext()
                    Console.WriteLine(nodes.Current.ToString())
                End While

            Case XPathResultType.Boolean
                If CType(navigator.Evaluate(expression), Boolean) Then
                    Console.WriteLine("True!")
                End If

            Case XPathResultType.String
                Console.WriteLine(navigator.Evaluate(expression))
        End Select

    End Sub
End Class

この例は、contosoBooks.xml ファイルを入力として使用します。

<?xml version="1.0" encoding="utf-8" ?>  
<bookstore xmlns="http://www.contoso.com/books">  
    <book genre="autobiography" publicationdate="1981-03-22" 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-11-17" 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-02-15" ISBN="1-861001-57-6">  
        <title>The Gorgias</title>  
        <author>  
            <name>Plato</name>  
        </author>  
        <price>9.99</price>  
    </book>  
</bookstore>  

注釈

XPath 式が評価され、次のいずれかの結果型が生成されます。

注意

XPath 式で無効な数の引数 (または実装されていないユーザー定義関数) を持つユーザー定義関数が指定されている場合、例外は実行時にのみ発生します。 ユーザー定義関数はコンパイル時にはチェックされず、ユーザー定義関数の結果として発生する例外は、実行によって式が評価された場合にのみ発生します。

こちらもご覧ください

適用対象

Compile(String, IXmlNamespaceResolver)

指定された XPath 式を、名前空間の解決用に指定された IXmlNamespaceResolver オブジェクトを使用してコンパイルし、XPath 式を表す XPathExpression オブジェクトを返します。

public:
 static System::Xml::XPath::XPathExpression ^ Compile(System::String ^ xpath, System::Xml::IXmlNamespaceResolver ^ nsResolver);
public static System.Xml.XPath.XPathExpression Compile (string xpath, System.Xml.IXmlNamespaceResolver? nsResolver);
public static System.Xml.XPath.XPathExpression Compile (string xpath, System.Xml.IXmlNamespaceResolver nsResolver);
static member Compile : string * System.Xml.IXmlNamespaceResolver -> System.Xml.XPath.XPathExpression
Public Shared Function Compile (xpath As String, nsResolver As IXmlNamespaceResolver) As XPathExpression

パラメーター

xpath
String

XPath 式。

nsResolver
IXmlNamespaceResolver

名前空間を解決するための IXmlNamespaceResolver インターフェイスを実装するオブジェクト。

戻り値

XPathExpression

XPathExpression オブジェクト。

例外

XPath 式のパラメーターが有効な XPath 式ではありません。

XPath 式が有効ではありません。

注釈

XPath 式が評価され、次のいずれかの結果型が生成されます。

注意

XPath 式で無効な数の引数 (または実装されていないユーザー定義関数) を持つユーザー定義関数が指定されている場合、例外は実行時にのみ発生します。 ユーザー定義関数はコンパイル時にチェックされず、ユーザー定義関数に起因する例外は、実行によって式が評価された場合にのみ発生します。

注意

このメソッドの引数として 、 を実装IXmlNamespaceResolverする 、 を渡XsltContextすことによって、1 つのステップでカスタム コンテキストを使用してコンパイルXPathExpressionすることはできません。 カスタム コンテキストで a XPathExpression を使用するには、式のコンパイル後にメソッドを SetContext 呼び出す必要があります。

こちらもご覧ください

適用対象