1 / 15

XPath

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

laszlo
Télécharger la présentation

XPath

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. XPath

  2. 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

  3. 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

  4. 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

  5. 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’]

  6. 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

  7. Axes • ancestor: //BBB/ancestor::* • <AAA>          <BBB/>           <CCC/>           <BBB/>           <BBB/> <DDD>               <BBB/>  </DDD>          <CCC/> </AAA>

  8. Axes • ancestor: //BBB/ancestor::DDD •  <AAA>           <BBB/>           <CCC/>           <BBB/>           <BBB/>  <DDD>               <BBB/>  </DDD>           <CCC/>   </AAA>

  9. 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

  10. Axes • child • /AAA/DDD/child::BBB – child can be omitted for abbreviation •  <AAA>           <BBB/>           <CCC/>           <BBB/>           <BBB/> <DDD>   <BBB/>           </DDD>           <CCC/>   </AAA>

  11. Axes • descendant • /AAA/descendent::* • <AAA>  <BBB/>           <CCC/>           <BBB/>           <BBB/>           <DDD>                <BBB/>           </DDD>           <CCC/>   </AAA> • /AAA/descendent::CCC ?

  12. Axes • parent • //BBB/parent::* • <AAA>          <BBB/>           <CCC/>           <BBB/>           <BBB/> <DDD>               <BBB/> </DDD>          < CCC/> </AAA> • //BBB/parent::DDD ?

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

  14. 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>

  15. 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?

More Related