150 likes | 298 Views
XPath. XPath. Used to access part of XML document Compact, non-XML syntax Use a pattern expression to identify nodes in an XML document Have a library of standard functions W3C Standard. XPath Example. Sample XML The root element /STATES
E N D
XPath • Used to access part of XML document • Compact, non-XML syntax • Use a pattern expression to identify nodes in an XML document • Have a library of standard functions • W3C Standard
XPath Example • Sample XML • The root element • /STATES • The SCODE of all STATE elements of STATES element • /STATES/STATE/SCODE • All the CAPTIAL element with a CNAME sub-element of the STATE element of the STATES element • /STATES/STATE/CAPITAL[CNAME=‘Atlanta’] • All CITIES elements in the XML document • //CITIES
More XPath Example • Element AA with two ancestors • /*/*/AA • First BB element of AA element • /AA/BB[1] • All the CC elements of the BB elements which has an sub-element A with value ‘3’ • /BB[A=‘3’]/CC • Any elements AA or elements CC of elements BB • //AA | /BB/CC
Even More XPath Example • Select all sub-elements of elements BB of elements AA • /BB/AA/* • When you do not know the sub-elements • Different from /BB/AA • Select all attributes named ‘aa’ • //@aa • Select all CITIES elements with an attribute named aa • //CITIES[@aa] • Select all CITIES elements with an attribute named aa with value ‘123’ • //CITIES[@aa = ‘123’]
Axis • Context node • Evaluation of XPath is from left to right • The context node the current node (set) being evaluated • Axis • Specifies the relationship of the resulting nodes relative to context node • Example: • /child::AA – children of AA, abbreviated by /AA • //AA/ancestor::BB – BB elements who are ancestor of any AA elements
Axes • ancestor: //BBB/ancestor::* • <AAA> <BBB/> <CCC/> <BBB/> <BBB/> <DDD> <BBB/> </DDD> <CCC/> </AAA>
Axes • ancestor: //BBB/ancestor::DDD • <AAA> <BBB/> <CCC/> <BBB/> <BBB/> <DDD> <BBB/> </DDD> <CCC/> </AAA>
Axes • attribute: Contains all attributes of the current node • //BBB/attribute::* – abbreviated by //@ • <AAA> <BBB aa=‘1’/> <CCC/> <BBB aa=‘2’ /> <BBB aa=‘3’ /> <DDD> <BBB bb=‘31’ /> </DDD> <CCC/> </AAA> • //BBB/attribute::bb
Axes • child • /AAA/DDD/child::BBB – child can be omitted for abbreviation • <AAA> <BBB/> <CCC/> <BBB/> <BBB/> <DDD> <BBB/> </DDD> <CCC/> </AAA>
Axes • descendant • /AAA/descendent::* • <AAA> <BBB/> <CCC/> <BBB/> <BBB/> <DDD> <BBB/> </DDD> <CCC/> </AAA> • /AAA/descendent::CCC ?
Axes • parent • //BBB/parent::* • <AAA> <BBB/> <CCC/> <BBB/> <BBB/> <DDD> <BBB/> </DDD> < CCC/> </AAA> • //BBB/parent::DDD ?
Axes • descendant-or-self • following • following-sibling • preceding: • preceding-sibling • self
Predicates • Filters a element set • A predicate is placed inside square brackets ( [ ] ) • Example: //BBB[position() mod 2 = 0 ] • <AAA> <BBB/> <BBB/> <BBB/> <BBB/> <BBB/> <BBB/> <BBB/> <BBB/> <CCC/> <CCC/> <CCC/> </AAA>
Predicates • //BBB[@aa=’31’] • <AAA> <BBB aa=‘1’/> <CCC/> <BBB aa=‘2’ /> <BBB aa=‘3’ /> <DDD> <BBB bb=‘31’ /> </DDD> <CCC/> </AAA> • Is it different from //BBB/attribute::bb?