解析XPath語(yǔ)法之在C#中使用XPath的示例詳解
XPath可以快速定位到Xml中的節(jié)點(diǎn)或者屬性。XPath語(yǔ)法很簡(jiǎn)單,但是強(qiáng)大夠用,它也是使用xslt的基礎(chǔ)知識(shí)。
示例Xml:
<?xml version="1.0" encoding="utf-8" ?>
<pets>
<cat color="black" weight="10">
<price>100</price>
<desc>this is a black cat</desc>
</cat>
<cat color="white" weight="9">
<price>80</price>
<desc>this is a white cat</desc>
</cat>
<cat color="yellow" weight="15">
<price>80</price>
<desc>this is a yellow cat</desc>
</cat>
<dog color="black" weight="10">
<price>100</price>
<desc>this is a black dog</desc>
</dog>
<dog color="white" weight="9">
<price>80</price>
<desc>this is a white dog</desc>
</dog>
<dog color="yellow" weight="15">
<price>80</price>
<desc>this is a yellow dog</desc>
</dog>
</pets>
XPath的語(yǔ)法:
1. XPath中的符號(hào)
符號(hào) |
說(shuō)明 |
示例 |
示例說(shuō)明 |
/ |
表示從根節(jié)點(diǎn)開(kāi)始選擇 |
/pets |
選擇根節(jié)點(diǎn)pets |
表示節(jié)點(diǎn)和子節(jié)點(diǎn)之間的間隔符 |
/pets/dog |
選擇pets節(jié)點(diǎn)下的dog節(jié)點(diǎn) | |
//xx |
表示從整個(gè)xml文檔中查找,而不考慮當(dāng)前節(jié)點(diǎn)位置 |
//price |
選擇文檔中所有的price節(jié)點(diǎn) |
. |
單個(gè)英文半角句點(diǎn)表示選擇當(dāng)前節(jié)點(diǎn) |
/pets/. |
選擇pets節(jié)點(diǎn) |
.. |
雙點(diǎn),表示選擇父節(jié)點(diǎn) |
/pets/dog[0]/.. |
表示pets節(jié)點(diǎn),也就是第一個(gè)dog節(jié)點(diǎn)的父節(jié)點(diǎn) |
@xx |
表示選擇屬性 |
//dog/@color |
表示選擇所有dog節(jié)點(diǎn)的color屬性集合 |
[…] |
中括號(hào)表示選擇條件,括號(hào)內(nèi)為條件 |
//dog[@color='white'] |
所有color為white的dog節(jié)點(diǎn) |
//dog[/price<100] |
所有price字節(jié)點(diǎn)值小于100的dog節(jié)點(diǎn) | ||
中括號(hào)內(nèi)數(shù)字為節(jié)點(diǎn)索引,類似c#等語(yǔ)言中的數(shù)組,數(shù)組下標(biāo)是從1開(kāi)始的 |
//dog[1] |
第1個(gè)dog節(jié)點(diǎn) | |
//dog[last()] |
最后一個(gè)dog節(jié)點(diǎn),last()是xPath內(nèi)置函數(shù) | ||
| |
單豎杠表示合并節(jié)點(diǎn)結(jié)合 |
//dog[@color='white'] | //cat[@color='white'] |
color屬性為white的dog節(jié)點(diǎn)和color屬性為white的cat節(jié)點(diǎn) |
* |
星號(hào)表示任何名字的節(jié)點(diǎn)或者屬性 |
//dog/* |
表示dog節(jié)點(diǎn)的所有子節(jié)點(diǎn) |
//dog/@* |
表示dog節(jié)點(diǎn)的所有屬性節(jié)點(diǎn) |
2. XPath數(shù)學(xué)運(yùn)算符
+ 加號(hào)表示加
- 表示數(shù)字相減
* 表示乘以
div 表示除以,這里數(shù)學(xué)上的除號(hào)/已經(jīng)被用作節(jié)點(diǎn)之間分隔符了
mod 表示取余
3. XPath邏輯運(yùn)算符
= 等于,相當(dāng)于c#中的 ==
!= 不等于
> 大于
>= 大于等于
< 小于
<= 小于等于
and 并且 與關(guān)系
or 或者 或關(guān)系
4. XPath Axes 從字面翻譯這個(gè)是XPath軸的意思,但根據(jù)我的理解這個(gè)翻譯成XPath節(jié)點(diǎn)關(guān)系運(yùn)算關(guān)鍵字更合適,就是一組關(guān)鍵字加上::雙冒號(hào)表示和當(dāng)前節(jié)點(diǎn)有關(guān)系的一個(gè)或者一組節(jié)點(diǎn).
使用語(yǔ)法: axisname::nodetest[predicate] 即軸名字::節(jié)點(diǎn)名字[取節(jié)點(diǎn)條件]
具體說(shuō)明如下:
關(guān)鍵字 |
說(shuō)明 |
示例 |
示例說(shuō)明 |
ancestor |
當(dāng)前節(jié)點(diǎn)的父祖節(jié)點(diǎn) |
ancestor::pig |
當(dāng)前節(jié)點(diǎn)的祖先節(jié)點(diǎn)中的pig節(jié)點(diǎn) |
ancestor-or-self |
當(dāng)前節(jié)點(diǎn)以及其父祖節(jié)點(diǎn) |
ancestor::pig |
|
attribute |
當(dāng)前節(jié)點(diǎn)的所有屬性 |
attribute::weight |
相當(dāng)于@weight,attribute::和@是等價(jià)的 |
child |
當(dāng)前節(jié)點(diǎn)的所有字節(jié)點(diǎn) |
child::*[name()!='price'] |
選擇名字不是price的子節(jié)點(diǎn) |
descendant |
子孫節(jié)點(diǎn) |
descendant::*[@*] |
有屬性的子孫節(jié)點(diǎn) |
descendant-or-self |
子孫節(jié)點(diǎn)以及當(dāng)前節(jié)點(diǎn) |
descendant-or-self::* |
|
following |
Xml文檔中當(dāng)前節(jié)點(diǎn)之后的所有節(jié)點(diǎn) |
following::* |
|
following-sibling |
當(dāng)前節(jié)點(diǎn)的同父弟弟節(jié)點(diǎn) |
following-sibling:: |
|
preceding |
Xml文檔中當(dāng)前節(jié)點(diǎn)之前的所有節(jié)點(diǎn) |
preceding::* |
|
namespace |
選取當(dāng)前節(jié)點(diǎn)的所有命名空間節(jié)點(diǎn) |
namespace::* |
|
parent |
當(dāng)前節(jié)點(diǎn)的父節(jié)點(diǎn) |
parent:: |
相當(dāng)于雙點(diǎn).. |
preceding-sibling |
當(dāng)前節(jié)點(diǎn)之后的同父兄節(jié)點(diǎn) |
preceding-sibling::* |
|
self |
當(dāng)前節(jié)點(diǎn) |
self::* |
相當(dāng)于單點(diǎn). |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.XPath;
using System.Xml;
namespace UseXPathDotNet
{
class Program
{
static void Main(string[] args)
{
UseXPathWithXPathDocument();
UseXPathWithXmlDocument();
Console.Read();
}
static void UseXPathWithXmlDocument()
{
XmlDocument doc = new XmlDocument();
doc.Load("http://chabaoo.cn");
//使用xPath選擇需要的節(jié)點(diǎn)
XmlNodeList nodes = doc.SelectNodes("/rss/channel/item[position()<=10]");
foreach (XmlNode item in nodes)
{
string title = item.SelectSingleNode("title").InnerText;
string url = item.SelectSingleNode("link").InnerText;
Console.WriteLine("{0} = {1}", title, url);
}
}
static void UseXPathWithXPathDocument()
{
XPathDocument doc = new XPathDocument("http://chabaoo.cn");
XPathNavigator xPathNav = doc.CreateNavigator();
//使用xPath取rss中最新的10條隨筆
XPathNodeIterator nodeIterator = xPathNav.Select("/rss/channel/item[position()<=10]");
while (nodeIterator.MoveNext())
{
XPathNavigator itemNav = nodeIterator.Current;
string title = itemNav.SelectSingleNode("title").Value;
string url = itemNav.SelectSingleNode("link").Value;
Console.WriteLine("{0} = {1}",title,url);
}
}
}
}
XPath使用示例,請(qǐng)看下面的代碼注釋
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
namespace UseXPath1
{
class Program
{
static void Main(string[] args)
{
string xml = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
<pets>
<cat color=""black"" weight=""10"" count=""4"">
<price>100</price>
<desc>this is a black cat</desc>
</cat>
<cat color=""white"" weight=""9"" count=""5"">
<price>80</price>
<desc>this is a white cat</desc>
</cat>
<cat color=""yellow"" weight=""15"" count=""1"">
<price>110</price>
<desc>this is a yellow cat</desc>
</cat>
<dog color=""black"" weight=""10"" count=""7"">
<price>114</price>
<desc>this is a black dog</desc>
</dog>
<dog color=""white"" weight=""9"" count=""4"">
<price>80</price>
<desc>this is a white dog</desc>
</dog>
<dog color=""yellow"" weight=""15"" count=""15"">
<price>80</price>
<desc>this is a yellow dog</desc>
</dog>
<pig color=""white"" weight=""100"" count=""2"">
<price>8000</price>
<desc>this is a white pig</desc>
</pig>
</pets>";
using (StringReader rdr = new StringReader(xml))
{
XmlDocument doc = new XmlDocument();
doc.Load(rdr);
//取所有pets節(jié)點(diǎn)下的dog字節(jié)點(diǎn)
XmlNodeList nodeListAllDog = doc.SelectNodes("/pets/dog");
//所有的price節(jié)點(diǎn)
XmlNodeList allPriceNodes = doc.SelectNodes("http://price");
//取最后一個(gè)price節(jié)點(diǎn)
XmlNode lastPriceNode = doc.SelectSingleNode("http://price[last()]");
//用雙點(diǎn)號(hào)取price節(jié)點(diǎn)的父節(jié)點(diǎn)
XmlNode lastPriceParentNode = lastPriceNode.SelectSingleNode("..");
//選擇weight*count=40的所有動(dòng)物,使用通配符*
XmlNodeList nodeList = doc.SelectNodes("/pets/*[@weight*@count=40]");
//選擇除了pig之外的所有動(dòng)物,使用name()函數(shù)返回節(jié)點(diǎn)名字
XmlNodeList animalsExceptPigNodes = doc.SelectNodes("/pets/*[name() != 'pig']");
//選擇價(jià)格大于100而不是pig的動(dòng)物
XmlNodeList priceGreaterThan100s = doc.SelectNodes("/pets/*[price div @weight >10 and name() != 'pig']");
foreach (XmlNode item in priceGreaterThan100s)
{
Console.WriteLine(item.OuterXml);
}
//選擇第二個(gè)dog節(jié)點(diǎn)
XmlNode theSecondDogNode = doc.SelectSingleNode("http://dog[position() = 2]");
//使用xpath ,axes 的 parent 取父節(jié)點(diǎn)
XmlNode parentNode = theSecondDogNode.SelectSingleNode("parent::*");
//使用xPath選擇第二個(gè)dog節(jié)點(diǎn)前面的所有dog節(jié)點(diǎn)
XmlNodeList dogPresibling = theSecondDogNode.SelectNodes("preceding::dog");
//取文檔的所有子孫節(jié)點(diǎn)price
XmlNodeList childrenNodes = doc.SelectNodes("descendant::price");
}
Console.Read();
}
}
}
相關(guān)文章
C#開(kāi)發(fā)之Socket網(wǎng)絡(luò)編程TCP/IP層次模型、端口及報(bào)文等探討
我們?cè)谥v解Socket編程前,先看幾個(gè)和Socket編程緊密相關(guān)的概念2013-03-03Unity實(shí)現(xiàn)動(dòng)物識(shí)別的示例代碼
本文主要介紹了如何通過(guò)Unity實(shí)現(xiàn)動(dòng)物識(shí)別,可以實(shí)現(xiàn)識(shí)別近八千種動(dòng)物,接口返回動(dòng)物名稱,并可獲取識(shí)別結(jié)果對(duì)應(yīng)的百科信息,感興趣的可以了解一下2022-02-02C#利用win32 Api 修改本地系統(tǒng)時(shí)間、獲取硬盤(pán)序列號(hào)
這篇文章主要介紹了C#利用win32 Api 修改本地系統(tǒng)時(shí)間、獲取硬盤(pán)序列號(hào)的方法及代碼分享,需要的朋友可以參考下2015-03-03C#實(shí)現(xiàn)實(shí)體類與字符串互相轉(zhuǎn)換的方法
這篇文章主要介紹了C#實(shí)現(xiàn)實(shí)體類與字符串互相轉(zhuǎn)換的方法,涉及C#字符串及對(duì)象的相互轉(zhuǎn)換技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08C#判斷指定驅(qū)動(dòng)器是否已經(jīng)準(zhǔn)備就緒的方法
這篇文章主要介紹了C#判斷指定驅(qū)動(dòng)器是否已經(jīng)準(zhǔn)備就緒的方法,涉及C#針對(duì)硬件IO操作的技巧,需要的朋友可以參考下2015-04-04