1. 程式人生 > >Xpath用法總結

Xpath用法總結

<root xmlns:foo="http://www.foo.org/" xmlns:bar="http://www.bar.org">
	<employees>
		<employee id="1">Johnny Dapp</employee>
		<employee id="2">Al Pacino</employee>
		<employee id="3">Robert De Niro</employee>  		
	</employees>
	<foo:companies>
		<foo:company id="4">Microsoft</foo:company>
		<foo:company id="5">IBM</foo:company>
		<foo:company id="6">Apple</foo:company>
		<foo:company id="7">Oracle</foo:company>
	</foo:companies>
</root>

1、選擇文件節點

/
Document='<root xmlns:bar="http://www.bar.org" xmlns:foo="http://www.foo.org/">
    	<employees>
    		<employee id="1">Johnny Dapp</employee>
    		<employee id="2">Al Pacino</employee>
    		<employee id="3">Robert De Niro</employee>  		
    	</employees>
    	<foo:companies>
    		<foo:company id="4">Microsoft</foo:company>
    		<foo:company id="5">IBM</foo:company>
    		<foo:company id="6">Apple</foo:company>
    		<foo:company id="7">Oracle</foo:company>
    	</foo:companies>
    </root>
'

2、選擇根元素

/root
Element='<root xmlns:bar="http://www.bar.org" xmlns:foo="http://www.foo.org/">
    	<employees>
    		<employee id="1">Johnny Dapp</employee>
    		<employee id="2">Al Pacino</employee>
    		<employee id="3">Robert De Niro</employee>  		
    	</employees>
    	<foo:companies>
    		<foo:company id="4">Microsoft</foo:company>
    		<foo:company id="5">IBM</foo:company>
    		<foo:company id="6">Apple</foo:company>
    		<foo:company id="7">Oracle</foo:company>
    	</foo:companies>
    </root>'

3、選擇所有employees下的employee

/root/employees/employee
Element='<employee id="1">Johnny Dapp</employee>'
Element='<employee id="2">Al Pacino</employee>'
Element='<employee id="3">Robert De Niro</employee>'

4、選擇所有foo:companies

//foo:companies
Element='<foo:companies xmlns:foo="http://www.foo.org/">
    		<foo:company id="4">Microsoft</foo:company>
    		<foo:company id="5">IBM</foo:company>
    		<foo:company id="6">Apple</foo:company>
    		<foo:company id="7">Oracle</foo:company>
    	</foo:companies>'

5、選擇所有foo:company屬性元素ID

//foo:companies/@id
Attribute='id=4'
Attribute='id=5'
Attribute='id=6'
Attribute='id=7'

6、選擇第二個employee的text

//employee[2]/text()
Text='Al Pacino'

7、選擇最後一個employee

//employee[last()]
Element='<employee id="3">Robert De Niro</employee>'

8、選擇第一個和第二個employee的text

//employee[position() <= 2]
Element='<employee id="1">Johnny Dapp</employee>'
Element='<employee id="2">Al Pacino</employee>'

9、選擇所有包含屬性ID的employee

//employee[@id]
Element='<employee id="1">Johnny Dapp</employee>'
Element='<employee id="2">Al Pacino</employee>'
Element='<employee id="3">Robert De Niro</employee>'

10、選擇屬性ID=3的employee

//employee[@id=3]
Element='<employee id="3">Robert De Niro</employee>'

11、選擇屬性ID<=3的employee

//employee[@id<=3]
Element='<employee id="1">Johnny Dapp</employee>'
Element='<employee id="2">Al Pacino</employee>'
Element='<employee id="3">Robert De Niro</employee>'

12、選擇companies下的所有元素

//foo:companies/*
Element='<foo:company xmlns:foo="http://www.foo.org/" id="4">Microsoft</foo:company>'
Element='<foo:company xmlns:foo="http://www.foo.org/" id="5">IBM</foo:company>'
Element='<foo:company xmlns:foo="http://www.foo.org/" id="6">Apple</foo:company>'
Element='<foo:company xmlns:foo="http://www.foo.org/" id="7">Oracle</foo:company>'

13、選擇文件中所有元素

//*
Element='<root xmlns:bar="http://www.bar.org" xmlns:foo="http://www.foo.org/">
    	<employees>
    		<employee id="1">Johnny Dapp</employee>
    		<employee id="2">Al Pacino</employee>
    		<employee id="3">Robert De Niro</employee>  		
    	</employees>
    	<foo:companies>
    		<foo:company id="4">Microsoft</foo:company>
    		<foo:company id="5">IBM</foo:company>
    		<foo:company id="6">Apple</foo:company>
    		<foo:company id="7">Oracle</foo:company>
    	</foo:companies>
    </root>'
Element='<employees>
    		<employee id="1">Johnny Dapp</employee>
    		<employee id="2">Al Pacino</employee>
    		<employee id="3">Robert De Niro</employee>  		
    	</employees>'
Element='<employee id="1">Johnny Dapp</employee>'

14、選擇所有employee和foo:company

//employee|//foo:company
Element='<employee id="1">Johnny Dapp</employee>'
Element='<employee id="2">Al Pacino</employee>'
Element='<employee id="3">Robert De Niro</employee>'
Element='<foo:company xmlns:foo="http://www.foo.org/" id="4">Microsoft</foo:company>'
Element='<foo:company xmlns:foo="http://www.foo.org/" id="5">IBM</foo:company>'
Element='<foo:company xmlns:foo="http://www.foo.org/" id="6">Apple</foo:company>'
Element='<foo:company xmlns:foo="http://www.foo.org/" id="7">Oracle</foo:company>'

15、選擇第一個employee的id的值(數值表示)

number(//employee[1]/@id)
Double='1'

16、選擇第一個employee的id的值(字串表示)

string(//employee[1]/@id)
String='1'

17、第一個employee的文字長度

string-length(//employee[1]/text())
Int64='11'

18、Select the local name of the first ‘company’ element, i.e. without the namespace

local-name(//foo:company[1])
String='company'

19、計算foo:company的數量

count(//foo:company)
Int64='4'

20、計算所有company內ID屬性值的總和

sum(//foo:company/@id)
Double='22'

其他用法:
開啟百度,按下F12開啟開發者工具,左側標籤選擇Elements
在這裡插入圖片描述
按Ctrl+F進入搜尋,就可以練習Xpath啦!
在這裡插入圖片描述

21、contains

//*[contains(@name,"wd")]

定位了百度的輸入框

21、start-with 和 endwith(個人用的時候不行?不知道為什麼)

//*[start-with(@name,"wd")]    #這個只是舉例,實際是找不到的