xpath. xpath used to access part of xml document used to access part of xml document compact,...

15
XPath XPath

Upload: melvyn-brown

Post on 18-Dec-2015

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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

XPathXPath

Page 2: 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

XPathXPath

Used to access part of XML Used to access part of XML document document

Compact, non-XML syntax Compact, non-XML syntax Use a pattern expression to identify Use a pattern expression to identify

nodes in an XML documentnodes in an XML document Have a library of standard functions Have a library of standard functions W3C Standard W3C Standard

Page 3: 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

XPath ExampleXPath Example Sample XMLSample XML The root elementThe root element

/STATES/STATES The SCODE of all STATE elements of STATES The SCODE of all STATE elements of STATES

element element /STATES/STATE/SCODE/STATES/STATE/SCODE

All the CAPTIAL element with a CNAME sub-All the CAPTIAL element with a CNAME sub-element of the STATE element of the STATES element of the STATE element of the STATES elementelement /STATES/STATE/CAPITAL[CNAME=‘Atlanta’]/STATES/STATE/CAPITAL[CNAME=‘Atlanta’]

All CITIES elements in the XML documentAll CITIES elements in the XML document //CITIES//CITIES

Page 4: 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

More XPath ExampleMore XPath Example

Element AA with two ancestorsElement AA with two ancestors /*/*/AA/*/*/AA

First BB element of AA elementFirst BB element of AA element /AA/BB[1]/AA/BB[1]

All the CC elements of the BB elements All the CC elements of the BB elements which has an sub-element A with value ‘3’ which has an sub-element A with value ‘3’ /BB[A=‘3’]/CC/BB[A=‘3’]/CC

Any elements AA or elements CC of Any elements AA or elements CC of elements BBelements BB //AA | /BB/CC//AA | /BB/CC

Page 5: 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

Even More XPath Even More XPath ExampleExample

Select all sub-elements of elements BB of elements AASelect all sub-elements of elements BB of elements AA /BB/AA/*/BB/AA/* When you do not know the sub-elementsWhen you do not know the sub-elements Different from /BB/AADifferent from /BB/AA

Select all attributes named ‘aa’Select all attributes named ‘aa’ //@aa//@aa

Select all CITIES elements with an attribute named aaSelect all CITIES elements with an attribute named aa //CITIES[@aa]//CITIES[@aa]

Select all CITIES elements with an attribute named aa Select all CITIES elements with an attribute named aa with value ‘123’with value ‘123’ //CITIES[@aa = ‘123’]//CITIES[@aa = ‘123’]

Page 6: 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

AxisAxis

Context nodeContext node Evaluation of XPath is from left to rightEvaluation of XPath is from left to right The context node the current node (set) being The context node the current node (set) being

evaluatedevaluated AxisAxis

Specifies the relationship of the resulting Specifies the relationship of the resulting nodes relative to context nodenodes 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 //AA/ancestor::BB – BB elements who are ancestor of

any AA elementsany AA elements

Page 7: 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

AxesAxes

ancestorancestor: //BBB/ancestor::*: //BBB/ancestor::*   <AAA><AAA>

          <BBB/>           <BBB/>           <CCC/>           <CCC/>           <BBB/>           <BBB/>           <BBB/>           <BBB/>                     <DDD><DDD>                <BBB/>                <BBB/>                    </DDD> </DDD>           <CCC/>           <CCC/>     </AAA></AAA>

Page 8: 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

AxesAxes

ancestorancestor: //BBB/ancestor::DDD: //BBB/ancestor::DDD   <AAA> <AAA>

          <BBB/>           <BBB/>           <CCC/>           <CCC/>           <BBB/>           <BBB/>           <BBB/>           <BBB/>                    <DDD> <DDD>                <BBB/>                <BBB/>                    </DDD>  </DDD>           <CCC/>           <CCC/>   </AAA>   </AAA>

Page 9: 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

AxesAxes attributeattribute: Contains all attributes of the current : Contains all attributes of the current

nodenode //BBB/attribute::* – abbreviated by //@//BBB/attribute::* – abbreviated by //@ <AAA> <AAA>

          <BBB           <BBB aa=‘1’aa=‘1’/> />           <CCC/>           <CCC/>           <BBB           <BBB aa=‘2’aa=‘2’ /> />           <BBB           <BBB aa=‘3’aa=‘3’ /> />                     <DDD> <DDD>                <BBB                <BBB bb=‘31’bb=‘31’ /> />           </DDD>           </DDD>           <CCC/>           <CCC/>   </AAA>   </AAA>

//BBB/attribute::bb//BBB/attribute::bb

Page 10: 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

AxesAxes childchild /AAA/DDD/child::BBB – child can be omitted for /AAA/DDD/child::BBB – child can be omitted for

abbreviationabbreviation   <AAA> <AAA>

          <BBB/>           <BBB/>           <CCC/>           <CCC/>           <BBB/>           <BBB/>           <BBB/>           <BBB/>                     <DDD> <DDD>                             <BBB/>   <BBB/>           </DDD>           </DDD>           <CCC/>           <CCC/>   </AAA>   </AAA>

Page 11: 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

AxesAxes descendantdescendant /AAA/descendent::*/AAA/descendent::* <AAA> <AAA>

                   <BBB/>  <BBB/>           <CCC/>           <CCC/>           <BBB/>           <BBB/>           <BBB/>           <BBB/>           <DDD>           <DDD>                <BBB/>                <BBB/>           </DDD>           </DDD>           <CCC/>           <CCC/>   </AAA>   </AAA>

/AAA/descendent::CCC ?/AAA/descendent::CCC ?

Page 12: 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

AxesAxes parentparent //BBB/parent::*//BBB/parent::* <AAA><AAA>

          <BBB/>           <BBB/>           <CCC/>           <CCC/>           <BBB/>           <BBB/>           <BBB/>           <BBB/>                     <DDD><DDD>                <BBB/>                <BBB/>                     </DDD></DDD>           < CCC/>           < CCC/>     </AAA></AAA>

//BBB/parent::DDD ?//BBB/parent::DDD ?

Page 13: 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

AxesAxes

descendant-or-selfdescendant-or-self followingfollowing following-siblingfollowing-sibling preceding: preceding: preceding-siblingpreceding-sibling selfself

Page 14: 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

PredicatesPredicates Filters a element setFilters a element set A predicate is placed inside square brackets ( [ ] )A predicate is placed inside square brackets ( [ ] ) Example: //Example: //BBB[position() mod 2 = 0 ]BBB[position() mod 2 = 0 ]       <<AAAAAA> >

          <          <BBBBBB/> />           <          <BBBBBB/> />           <          <BBBBBB/> />           <          <BBBBBB/> />           <          <BBBBBB/> />           <          <BBBBBB/> />           <          <BBBBBB/> />           <          <BBBBBB/> />           <          <CCCCCC/> />           <          <CCCCCC/> />           <          <CCCCCC/> />      </     </AAAAAA> >

Page 15: 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

PredicatesPredicates

//BBB[@aa=’31’]//BBB[@aa=’31’] <AAA> <AAA>

          <BBB aa=‘1’/>           <BBB aa=‘1’/>           <CCC/>           <CCC/>           <BBB aa=‘2’ />           <BBB aa=‘2’ />           <BBB aa=‘3’ />           <BBB aa=‘3’ />           <DDD>           <DDD>                               <BBB bb=‘31’ /><BBB bb=‘31’ />           </DDD>           </DDD>           <CCC/>           <CCC/>   </AAA>   </AAA>

Is it different from //BBB/attribute::bb?Is it different from //BBB/attribute::bb?