XPath基础语法(1)
Xpath是一种能够在XML文档中寻找信息的语言。它通过XML文档中的元素和属性来进行导航。
1. "/"
/AAA:
<AAA>
<BBB/>
<CCC/>
<BBB/>
<BBB/>
<DDD>
<BBB/>
<DDD>
<CCC/>
<AAA>
/AAA/CCC:
<AAA>
<BBB/>
<CCC/>
<BBB/>
<BBB/>
<DDD>
<BBB/>
<DDD>
<CCC/>
<AAA>
/AAA/DDD/BBB:
<AAA>
<BBB/>
<CCC/>
<BBB/>
<BBB/>
<DDD>
<BBB/>
<DDD>
<CCC/>
<AAA>
2. "//"
//BBB
<AAA>
<BBB/>
<CCC/>
<BBB/>
<DDD>
<BBB/>
<DDD>
<CCC>
<DDD>
<BBB/>
<BBB/>
<DDD>
<CCC>
<AAA>
//DDD/BBB
<AAA>
<BBB/>
<CCC/>
<BBB/>
<DDD>
<BBB/>
DDD>
<CCC>
<DDD>
<BBB/>
<BBB/>
<DDD>
<CCC>
<AAA>
3. "*":
/AAA/CCC/DDD*/*/BBB
<AAA>
<XXX>
<DDD>
<BBB/>
<BBB/>
<EEE/>
<FFF/>
<DDD>
<XXX>
<CCC>
<DDD>
<BBB/>
<BBB/>
<EEE/>
<FFF/>
<DDD>
<CCC>
<CCC>
<BBB>
<BBB>
<BBB/>
<BBB>
<BBB>
<CCC>
<AAA>
//*
<AAA>
<XXX>
<DDD>
<BBB/>
<BBB/>
<EEE/>
<FFF/>
DDD>
XXX>
<CCC>
<DDD>
<BBB/>
<BBB/>
<EEE/>
<FFF/>
DDD>
CCC>
AAA>
4. [1] , last()
/AAA/BBB[1]
<AAA>
<BBB/>
<BBB/>
<BBB/>
<BBB/>
AAA>
/AAA/BBB[last()]
<AAA>
<BBB/>
<BBB/>
<BBB/>
<BBB/>
AAA>
5. "@" :
<AAA>
<BBB id = "b1"/>
<BBB id = "b2"/>
<BBB name = "bbb"/>
<BBB/>
AAA>
//BBB[@id]
<AAA>
<BBB id = "b1"/>
<BBB id = "b2"/>
<BBB name = "bbb"/>
<BBB/>
AAA>
//BBB[@name]
<AAA>
<BBB id = "b1"/>
<BBB id = "b2"/>
<BBB name = "bbb"/>
<BBB/>
AAA>
//BBB[@*]
<AAA>
<BBB id = "b1"/>
<BBB id = "b2"/>
<BBB name = "bbb"/>
<BBB/>
AAA>
//BBB[not(@*)]
<AAA>
<BBB id = "b1"/>
<BBB id = "b2"/>
<BBB name = "bbb"/>
<BBB/>
AAA>
//BBB[@id='b1']
<AAA>
<BBB id = "b1"/>
<BBB name = " bbb "/>
<BBB name = "bbb"/>
AAA>
//BBB[@name='bbb']
<AAA>
<BBB id = "b1"/>
<BBB name = " bbb "/>
<BBB name = "bbb"/>
AAA>
//BBB[normalize-space(@name)='bbb']
<AAA>
<BBB id = "b1"/>
<BBB name = " bbb "/>
<BBB name = "bbb"/>
AAA>
6. count() :
//*[count(BBB)=2]
<AAA>
<CCC>
<BBB/>
<BBB/>
<BBB/>
CCC>
<DDD>
<BBB/>
<BBB/>
DDD>
<EEE>
<CCC/>
<DDD/>
EEE>
AAA>
//*[count(*)=2]
<AAA>
<CCC>
<BBB/>
<BBB/>
<BBB/>
CCC>
<DDD>
<BBB/>
<BBB/>
DDD>
<EEE>
<CCC/>
<DDD/>
EEE>
AAA>
//*[count(*)=3]
<AAA>
<CCC>
<BBB/>
<BBB/>
<BBB/>
CCC>
<DDD>
<BBB/>
<BBB/>
DDD>
<EEE>
<CCC/>
<DDD/>
EEE>
AAA>