Xpath基本用法

來源:互聯網
上載者:User

  編程中經常會用到XML,.Net FrameWork提供了專門對XML進行處理的DLL,裡面提供了很多對XML處理的方法,在這裡簡單介紹一下XPath的使用方法.

  XPath 使用路徑運算式來選取 XML 文檔中的節點或者節點集.類似於用Regex對文本進行目的性匹配.

  首先我們寫一個普通的XML文檔

<?xml version="1.0" encoding="utf-8" ?>
<school>
  <class>
    <number>1</number>
    <teacher property="English">Mr Sun</teacher>
    <student_count>50</student_count>
  </class>
  <class>
    <number>2</number>
    <teacher property="Chinese">Mrs Li</teacher>
    <student_count>35</student_count>
  </class>
  <class>
    <number>3</number>
    <teacher property="Math">Mr Zhang</teacher>
    <student_count>20</student_count>
  </class>
</school>

 

 XPath基本用法

運算式 描述
nodename 選取此節點的所有子節點
/ 從根節點選取
// 從匹配選擇的當前節點選擇文檔中的節點,而不考慮它們的位置
. 選取當前節點
.. 選取當前節點的父節點
@ 選取屬性
執行個體

在下面的表格中,我們已列出了一些路徑運算式以及運算式的結果:

路徑運算式 結果
school 選取 school元素的所有子節點
/school

選取根項目 school

注釋:假如路徑起始於正斜杠( / ),則此路徑始終代表到某元素的絕對路徑!

school/class 選取所有屬於 school的子項目的 class元素。
//class 選取所有 book 子項目,而不管它們在文檔中的位置。
school//book 選擇所有屬於 school元素的後代的 class元素,而不管它們位於 school之下的什麼位置。
//@property 選取所有名為 property的屬性。

 

  我們做一個控制台應用程式,來用XPath操作這個XML:

namespace XPathTest
{
    class Program
    {
        static void Main(string[] args)
        {
           
            XmlDocument doc = new XmlDocument();//建立一個XMLDocument對象
            doc.Load("..\\TestXML.xml");//根據XML檔案的路徑載入XML檔案
        }

       
    }
}

 

  好了,現在我們需要得到number為1的班級裡的所有資訊:

   XmlNodeList list=doc.SelectNodes("//class[number=1]");   //獲得XML裡子節點number為1的節點class

   Console.Write(list[0]["number"].Name+":"+list[0]["number"].InnerText+"\n");

   Console.Write(list[0]["teacher"].Name+":"+list[0]["teacher"].InnerText+"\n");

   Console.Write(list[0]["student_count"].Name+":"+list[0]["student_count"].InnerText+"\n");

   Console.Read();

顯示的結果為:

  number:1

  teacher:Mr Sun

  student_count:50

 

  可能會有朋友說道:我也沒看到哪裡簡單啊,我還不如用While(XmlReader.Read())呢.別急,XPath的優勢還沒體現出來

 

  現在我們又變需求了,我們要找語文老師班上的學生人數,也就是property為Chinese的teacher的class的student_count(有點繞):

  XmlNodeList list = doc.SelectNodes("//class[teacher[@property='English']]");//匹配teacher節點裡property為English的class

  Console.Write(list[0]["student_count"].Name + ":" + list[0]["student_count"].InnerText);

  Console.Read();

 

顯示的結果:

  student_count:35

 

  我們又需要找班級人數不大與40的所有班級資訊:

  XmlNodeList list=doc.SelectNodes("//class[student_count<40]");//匹配student_count小於40的class

  for(int i=0;i<list.Count;i++)

{

  Console.Write("匹配的第"+i+"個節點資訊");

  Console.Write(list[i]["number"].Name+":"+list[i]["number"].InnerText+"\n");

  Console.Write(list[i]["teacher"].Name+":"+list[i]["teacher"].InnerText+"\n");

  Console.Write(list[i]["student_count"].Name+":"+list[i]["student_count"].InnerText+"\n");

  Console.Read();

}

 

顯示的結果:

  匹配的第1個節點資訊:

  number:2

  teacher:Mrs Li

  student_count:35

  匹配的第2個節點資訊:

  number:3

  teacher:Mr Zhang

  student_count:35

 

   是不是很簡單,這就是XPath的優勢:不需要一次次的遍曆,只需用一句運算式就能得到想要的結果

 

還有一些XPath的基本文法:

執行個體

在下面的表格中,我們列出了帶有謂語的一些路徑運算式,以及運算式的結果:(謂語是在方括弧裡的匹配條件)

路徑運算式 結果
/school/class[1] 選取屬於 school子項目的第一個 class元素。
/school/class[last()] 選取屬於 school子項目的最後一個 class元素。
/school/class[last()-1] 選取屬於 school子項目的倒數第二個 class元素。
/school/class[position()<3] 選取最前面的兩個屬於 school元素的子項目的 class元素。
//teacher[@property] 選取所有擁有名為 property的屬性的 teacher元素。
//teacher[@property='English'] 選取所有 teacher元素,且這些元素擁有值為 English 的 property屬性。
/school/class[student_count>35.00] 選取所有 school元素的 class元素,且其中的 student_count元素的值須大於 35.00。
/school/class[student_count>35.00]/title 選取所有 school元素中的 class元素的 teacher 元素,且其中的 student_count元素的值須大於 35.00。
選取未知節點

XPath 萬用字元可用來選取未知的 XML 元素。

萬用字元 描述
* 匹配任何元素節點
@* 匹配任何屬性節點
node() 匹配任何類型的節點
執行個體

在下面的表格中,我們列出了一些路徑運算式,以及這些運算式的結果:

路徑運算式 結果
/school/* 選取 school元素的所有子節點
//* 選取文檔中的所有元素
//property[@*] 選取所有帶有屬性的 property元素。
選取若干路徑

通過在路徑運算式中使用“|”運算子,您可以選取若干個路徑。

執行個體

在下面的表格中,我們列出了一些路徑運算式,以及這些運算式的結果:

路徑運算式 結果
//class/teacher | //class/number 選取所有 class 元素的 teacher 和 number 元素。
//teacher | //number 選取所有文檔中
/school/class/teacher | //number 選取所有屬於 school元素的 class元素的 teacher元素,以及文檔中所有的 number元素。
    XPath還有多個內建函數,分別是對字串,值,日期等的處理和比較.這裡就不一一說明了,XPath的魅力還不止如此.本人是初學者,希望能和大家一起努力

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.