XPath 是XML的查询语言,和SQL的角色很类似。以下面XML为例,介绍XPath 的语法。
<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<cd country="USA">
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<price>10.90</price>
</cd>
<cd country="UK">
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<price>9.90</price>
</cd>
<cd country="USA">
<title>Greatest Hits</title>
<artist>Dolly Parton</artist>
<price>9.90</price>
</cd>
</catalog>
定位节点
XML是树状结构,类似档案系统内数据夹的结构,XPath也类似档案系统的路径命名方式。不过XPath 是一种模式(Pattern),可以选出 XML档案中,路径符合某个模式的所有节点出来。例如要选catalog底下的cd中所有price元素可以用:
/catalog/cd/price
如果XPath的开头是一个斜线(/)代表这是绝对路径。如果开头是两个斜线(//)表示文件中所有符合模式的元素都会被选出来,即使是处于树中不同的层级也会被选出来。以下的语法会选出文件中所有叫做cd的元素(在树中的任何层级都会被选出来):
//cd
选择未知的元素
使用星号(Wildcards,*)可以选择未知的元素。下面这个语法会选出/catalog/cd 的所有子元素:
/catalog/cd/*
以下的语法会选出所有catalog的子元素中,包含有price作为子元素的元素。
/catalog/*/price
以下的语法会选出有两层父节点,叫做price的所有元素。
/*/*/price
以下的语法会选择出文件中的所有元素。
//*
要注意的是,想要存取不分层级的元素,XPath语法必须以两个斜线开头(//),想要存取未知元素才用星号(*),星号只能代表未知名称的元素,不能代表未知层级的元素。
选择分支
使用中括号可以选择分支。以下的语法从catalog的子元素中取出第一个叫做cd的元素。XPath的定义中没有第0元素这种东西。
/catalog/cd[1]
以下语法选择catalog中的最后一个cd元素:(XPathj并没有定义 first() 这种函式喔,用上例的 [1]就可以取出第一个元素。
/catalog/cd[last()]
以下语法选出含有price子元素的所有/catalog/cd元素。
/catalog/cd[price]
以下语法选出price元素的值等于10.90的所有/catalog/cd元素
/catalog/cd[price=10.90]这里如果price的文本中有特殊字符时可以用''或""把它引起来。例如
/catalog/cd[price=http://writeblog.csdn.net/]因为文本中有//而会出错,这时用
/catalog/cd[price='http://writeblog.csdn.net/']就不会出错。
以下语法选出price元素的值等于10.90的所有/catalog/cd元素 的price元素
/catalog/cd[price=10.90]/price
选择一个以上的路径
使用Or操作数(|)就可以选择一个以上的路径。例如:
/catalog/cd/title | catalog/cd/artist
选择所有title以及artist元素
//title | //artist
选择所有title以及artist以及price元素
//title | //artist | //price
选择属性
在XPath中,除了选择元素以外,也可以选择属性。属性都是以@开头。例如选择文件中所有叫做country的属性:
//@country
选择所有含有country这个属性的cd元素:
//cd[@country]
以下语法选择出含有属性的所有cd元素
//cd[@*]
以下语法选择出country属性值为UK的cd元素
//cd[@country='UK']
http://www.zvon.org/xxl/XPathTutorial/Output/example1.html有详细例子。
/AAA /AAA/CCC /AAA/DDD/BBB 得到从AAA开始的相应元素。
/AAA/CCC/DDD/* 得到DDD以后的元素。
/*/*/*/BBB Select all elements BBB which have 3 ancestors
//* Select all elements
/AAA/BBB[1] Select the first BBB child of element AAA
/AAA/BBB[last()] Select the last BBB child of element AAA
//@id Select all attributes @id
//BBB[@id] Select BBB elements which have attribute id
//BBB[@name] Select BBB elements which have attribute name
//BBB[@*] Select BBB elements which have any attribute
//BBB[not(@*)] Select BBB elements without an attribute
//BBB[@id='b1'] Select BBB elements which have attribute id with value b1
//BBB[normalize-space(@name)='bbb'] Select BBB elements which have attribute name with value bbb, leading and trailing spaces are removed before comparison
//*[count(BBB)=2] Select elements which have two children BBB
//*[count(*)=2] Select elements which have 2 children
//*[name()='BBB'] Select all elements with name BBB, equivalent with //BBB
//*[starts-with(name(),'B')] Select all elements name of which starts with letter B
//*[contains(name(),'C')] Select all elements name of which contain letter C
//*[string-length(name()) < 3] Select elements name of which has one or two characters
//*[string-length(name()) > 3] Select elements with name longer than three characters
//*[string-length(name()) = 3] Select elements with three-letter name
//CCC | //BBB Select all elements CCC and BBB
/AAA/EEE | //BBB Select all elements BBB and elements EEE which are children of root element AAA
/AAA/EEE | //DDD/CCC | /AAA | //BBB
/AAA =/child::AAA 相等都是得到要元素AAA
/AAA/BBB =/child::AAA/child::BBB =/child::AAA/BBB 得到根元素AAA下的BBB元素
/descendant::* 得到文档是的所有元素节点。
/AAA/BBB/descendant::* 得到根元素AAA下的BBB元素下的所有节点。
//CCC/descendant::*
//CCC/descendant::DDD 得到CCC元素下的所有DDD节点。
//DDD/parent::* 得到DDD元素上的所有直接父节点。
/AAA/BBB/DDD/CCC/EEE/ancestor::* 得到EEE节点的所有父节点CCC,DDD,BBB,AAA
//FFF/ancestor::* 得到FFF节点的所有父节点,这个表达式不包括FFF节点的。
/AAA/BBB/following-sibling::* 得到BBB节点的兄弟节点。
//CCC/following-sibling::* 得到CCC节点的所有兄弟节点。
/AAA/XXX/preceding-sibling::* 得到XXX的所有前面兄弟节点。
//CCC/preceding-sibling::* 得到CCC的所有前面兄弟节点。
/AAA/XXX/following::* 得到XXX的所有后继节点。
//ZZZ/following::* 得到ZZZ的所有后继节点。
/AAA/XXX/preceding::* 得到XXX的所有前续节点。
//GGG/preceding::* 得到GGG的所有前续节点。
/AAA/XXX/descendant-or-self::* 得到XXX的所有子节点及本身。
//CCC/descendant-or-self::* 得到CCC的所有子节点及本身。
/AAA/XXX/DDD/EEE/ancestor-or-self::* 得到EEE的祖先节点及本身。
//GGG/ancestor-or-self::* 得到GGG的祖先节点及本身。
//BBB[position() mod 2 = 0 ] 得到所有位置mod2为0的BBB节点。
//BBB[ position() = floor(last() div 2 + 0.5) or position() = ceiling(last() div 2 + 0.5) ] 得到中间的BBB元素。
<AAA>
<BBB/>
<BBB/>
<BBB/>
<BBB/>
<BBB/>
<BBB/>
<BBB/>
<BBB/>
<CCC/>
<CCC/>
<CCC/>
</AAA>
//CCC[ position() = floor(last() div 2 + 0.5) or position() = ceiling(last() div 2 + 0.5) ] 得到中间的CCC节点