Presentation is loading. Please wait.

Presentation is loading. Please wait.

XPath. XPath Used to access part of XML document Used to access part of XML document Compact, non-XML syntax Compact, non-XML syntax Use a pattern expression.

Similar presentations


Presentation on theme: "XPath. XPath Used to access part of XML document Used to access part of XML document Compact, non-XML syntax Compact, non-XML syntax Use a pattern expression."— Presentation transcript:

1 XPath

2 XPath Used to access part of XML document Used to access part of XML document Compact, non-XML syntax Compact, non-XML syntax Use a pattern expression to identify nodes in an XML document Use a pattern expression to identify nodes in an XML document Have a library of standard functions Have a library of standard functions W3C Standard W3C Standard W3C Standard W3C Standard

3 XPath Example Sample XML Sample XML Sample XML Sample XML The root element The root element /STATES /STATES The SCODE of all STATE elements of STATES element The SCODE of all STATE elements of STATES element /STATES/STATE/SCODE /STATES/STATE/SCODE All the CAPTIAL element with a CNAME sub-element of the STATE element of the STATES element All the CAPTIAL element with a CNAME sub-element of the STATE element of the STATES element /STATES/STATE/CAPITAL[CNAME=‘Atlanta’] /STATES/STATE/CAPITAL[CNAME=‘Atlanta’] All CITIES elements in the XML document All CITIES elements in the XML document //CITIES //CITIES

4 More XPath Example Element AA with two ancestors Element AA with two ancestors /*/*/AA /*/*/AA First BB element of AA element First BB element of AA element /AA/BB[1] /AA/BB[1] All the CC elements of the BB elements which has an sub-element A with value ‘3’ All the CC elements of the BB elements which has an sub-element A with value ‘3’ /BB[A=‘3’]/CC /BB[A=‘3’]/CC Any elements AA or elements CC of elements BB Any elements AA or elements CC of elements BB //AA | /BB/CC //AA | /BB/CC

5 Even More XPath Example Select all sub-elements of elements BB of elements AA Select all sub-elements of elements BB of elements AA /BB/AA/* /BB/AA/* When you do not know the sub-elements When you do not know the sub-elements Different from /BB/AA Different from /BB/AA Select all attributes named ‘aa’ Select all attributes named ‘aa’ //@aa //@aa //@aa Select all CITIES elements with an attribute named aa Select all CITIES elements with an attribute named aa //CITIES[@aa] //CITIES[@aa] Select all CITIES elements with an attribute named aa with value ‘123’ Select all CITIES elements with an attribute named aa with value ‘123’ //CITIES[@aa = ‘123’] //CITIES[@aa = ‘123’]

6 Axis Context node Context node Evaluation of XPath is from left to right Evaluation of XPath is from left to right The context node the current node (set) being evaluated The context node the current node (set) being evaluated Axis Axis Specifies the relationship of the resulting nodes relative to context node Specifies the relationship of the resulting nodes relative to context node Example: Example: /child::AA – children of AA, abbreviated by /AA /child::AA – children of AA, abbreviated by /AA //AA/ancestor::BB – BB elements who are ancestor of any AA elements //AA/ancestor::BB – BB elements who are ancestor of any AA elements

7 Axes ancestor: //BBB/ancestor::* ancestor: //BBB/ancestor::*

8 Axes ancestor: //BBB/ancestor::DDD ancestor: //BBB/ancestor::DDD

9 Axes attribute: Contains all attributes of the current node attribute: Contains all attributes of the current node //BBB/attribute::* – abbreviated by //@ //BBB/attribute::* – abbreviated by //@ //BBB/attribute::bb //BBB/attribute::bb

10 Axes child child /AAA/DDD/child::BBB – child can be omitted for abbreviation /AAA/DDD/child::BBB – child can be omitted for abbreviation

11 Axes descendant descendant /AAA/descendent::* /AAA/descendent::* /AAA/descendent::CCC ? /AAA/descendent::CCC ?

12 Axes parent parent //BBB/parent::* //BBB/parent::* //BBB/parent::DDD ? //BBB/parent::DDD ?

13 Axes descendant-or-self descendant-or-self following following following-sibling following-sibling preceding: preceding: preceding-sibling preceding-sibling self self

14 Predicates Filters a element set Filters a element set A predicate is placed inside square brackets ( [ ] ) A predicate is placed inside square brackets ( [ ] ) Example: // BBB[position() mod 2 = 0 ] Example: // BBB[position() mod 2 = 0 ]

15 Predicates //BBB[@aa=’31’] //BBB[@aa=’31’] Is it different from //BBB/attribute::bb? Is it different from //BBB/attribute::bb?


Download ppt "XPath. XPath Used to access part of XML document Used to access part of XML document Compact, non-XML syntax Compact, non-XML syntax Use a pattern expression."

Similar presentations


Ads by Google