XPath 式を表す文字列をコンパイルし、 XPathExpression を返します。
Public Overridable Function Compile( _
ByVal xpath As String _) As XPathExpression
[C#]
public virtual XPathExpression Compile(stringxpath);
[C++]
public: virtual XPathExpression* Compile(String* xpath);
[JScript]
public function Compile(
xpath : String) : XPathExpression;
パラメータ
- xpath
XPath 式を表す文字列。
戻り値
XPath 式を表す XPathExpression オブジェクト。
例外
例外の種類 | 条件 |
---|---|
ArgumentException | xpath パラメータに無効な Xpath 式が含まれています。 |
解説
XPath 式が計算されて、次の戻り値型の 1 つを生成します。
- ノード セット - 複製を持たないノードの順不同のコレクション
- ブール値 - true または false
- 数値 ? 浮動小数点数
- 文字列 - UCS 文字のシーケンス
ノード セットを返す式は、 Select メソッドおよび Evaluate メソッドで使用できます。ブール値、数値、または文字列を返す式は、 Evaluate メソッドで使用できます。 Matches メソッドの有効な式に関する規則は、このメソッドに固有です。
使用例
[Visual Basic, C#, C++] すべての ISBN 属性ノードを表示する例を次に示します。
Option Explicit
Option Strict
Imports System
Imports System.IO
Imports System.Xml
Imports System.Xml.XPath
Public Class Sample
Public Shared Sub Main()
Dim doc As New XPathDocument("booksort.xml")
Dim nav As XPathNavigator = doc.CreateNavigator()
'Select all ISBN attributes.
Dim expr As XPathExpression
expr = nav.Compile("/bookstore/book/@bk:ISBN")
Dim nsmgr As New XmlNamespaceManager(nav.NameTable)
nsmgr.AddNamespace("bk", "urn:samples")
expr.SetContext(nsmgr)
'Display the selection.
Dim iterator As XPathNodeIterator = nav.Select(expr)
While iterator.MoveNext()
Console.WriteLine(iterator.Current.ToString())
End While
End Sub 'Main
End Class 'Sample
[C#]
using System;
using System.IO;
using System.Xml;
using System.Xml.XPath;
public class Sample
{
public static void Main()
{
XPathDocument doc = new XPathDocument("booksort.xml");
XPathNavigator nav = doc.CreateNavigator();
//Select all ISBN attributes.
XPathExpression expr;
expr = nav.Compile("/bookstore/book/@bk:ISBN");
XmlNamespaceManager nsmgr = new XmlNamespaceManager(nav.NameTable);
nsmgr.AddNamespace("bk", "urn:samples");
expr.SetContext(nsmgr);
//Display the selection.
XPathNodeIterator iterator = nav.Select(expr);
while (iterator.MoveNext()){
Console.WriteLine(iterator.Current.ToString());
}
}
}
[C++]
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::XPath;
int main()
{
XPathDocument* doc = new XPathDocument(S"booksort.xml");
XPathNavigator* nav = doc->CreateNavigator();
//Select all ISBN attributes.
XPathExpression* expr;
expr = nav->Compile(S"/bookstore/book/@bk:ISBN");
XmlNamespaceManager* nsmgr = new XmlNamespaceManager(nav->NameTable);
nsmgr->AddNamespace(S"bk", S"urn:samples");
expr->SetContext(nsmgr);
//Display the selection.
XPathNodeIterator* iterator = nav->Select(expr);
while (iterator->MoveNext()){
Console::WriteLine(iterator->Current);
}
}
この例では、入力として、 booksort.xml というファイルを使用しています。
<?xml version="1.0"?>
<!-- a fragment of a book store inventory database -->
<bookstore xmlns:bk="urn:samples">
<book genre="novel" publicationdate="1997" bk:ISBN="1-861001-57-8">
<title>Pride And Prejudice</title>
<author>
<first-name>Jane</first-name>
<last-name>Austen</last-name>
</author>
<price>24.95</price>
</book>
<book genre="novel" publicationdate="1992" bk:ISBN="1-861002-30-1">
<title>The Handmaid's Tale</title>
<author>
<first-name>Margaret</first-name>
<last-name>Atwood</last-name>
</author>
<price>29.95</price>
</book>
<book genre="novel" publicationdate="1991" bk:ISBN="1-861001-57-6">
<title>Emma</title>
<author>
<first-name>Jane</first-name>
<last-name>Austen</last-name>
</author>
<price>19.95</price>
</book>
<book genre="novel" publicationdate="1982" bk:ISBN="1-861001-45-3">
<title>Sense and Sensibility</title>
<author>
<first-name>Jane</first-name>
<last-name>Austen</last-name>
</author>
<price>19.95</price>
</book>
</bookstore>
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ
参照
XPathNavigator クラス | XPathNavigator メンバ | System.Xml.XPath 名前空間