1
play

1 XPath Navigating Xml Xml is similar to a file structure, but you - PDF document

XPath Based on slides by Dan Suciu University of Washington CS/INFO 1 330 XPath http://www.w3.org/TR/xpath (11/99) Building block for other W3C standards: XSL Transformations (XSLT) XML Link (XLink) XML Pointer (XPointer)


  1. XPath Based on slides by Dan Suciu University of Washington CS/INFO 1 330 XPath � http://www.w3.org/TR/xpath (11/99) � Building block for other W3C standards: • XSL Transformations (XSLT) • XML Link (XLink) • XML Pointer (XPointer) • XML Query � Was originally part of XSL CS/INFO 2 330 XPath – Navigating Xml � When Xml is stored in a tree, XPath allows you to navigate to different nodes: Class < Class> < Student> Jeff< /Student> Student Student < Student> Pat< /Student> < /Class> Text: Text: Jeff Pat CS/INFO 3 330 1

  2. XPath – Navigating Xml � Xml is similar to a file structure, but you can select more than one node: //Class/Student Class < Class> < Student> Jeff< /Student> Student Student < Student> Pat< /Student> < /Class> Text: Text: Jeff Pat CS/INFO 4 330 XPath – Navigating Xml � An XPath expression looks just like a file path • Elements are accessed as /<element>/ • Attributes are accessed as @attribute � Everything that satisfies the path is selected • You can add constraints in brackets [ ] to further refine your selection CS/INFO 5 330 XPath – Navigating Xml < class name= ‘CS 330’> < location building= ‘Hollister’ room= ‘110’/ > < professor> Johannes Gehrke< / professor> < ta> Scott Selikoff < / ta> < student_list> < student id= ‘999-991’> John Smith< / student> < student id= ‘999-992’> Jane Doe< / student> < / student_list> < / class> Starting Element Attribute Constraint //class[@name= ‘CS 330’]/student_list/student/@id Element Path Selection Selection Result: The attribute nodes containing 999-991 and 999-992 CS/INFO 6 330 2

  3. XPath - Context � Context – your current focus in an Xml document � Use: //<root>/… When you want to start from the beginning of the Xml document CS/INFO 7 330 XPath - Context XPath: List/Student Class Prof Location List Text: Attr: Student Student Gehrke Olin Text: Text: Jeff Pat CS/INFO 8 330 XPath - Context XPath: Student Class Prof Location List Text: Attr: Student Student Gehrke Olin Text: Text: Jeff Pat CS/INFO 9 330 3

  4. XPath – Examples <Basket> <Cherry flavor=‘sweet’/> <Cherry flavor=‘bitter’/> <Cherry/> <Apple color=‘red’/> <Apple color=‘red’/> <Apple color=‘green’/> … </Basket> Select all of the red apples: //Basket/Apple[@color= ‘red’] CS/INFO 10 330 XPath – Examples <Basket> <Cherry flavor=‘sweet’/> <Cherry flavor=‘bitter’/> <Cherry/> <Apple color=‘red’/> <Apple color=‘red’/> <Apple color=‘green’/> … </Basket> Select the cherries that have some flavor: //Basket/Cherry[@flavor] CS/INFO 11 330 XPath – Examples <orchard> <tree> <apple color=‘red’/> <apple color=‘red’/> </tree> <basket> <apple color=‘green’/> <orange/> </basket> </orchard> Select all the apples in the orchard: //orchard/descendant()/apple CS/INFO 12 330 4

  5. Example for XPath Queries <bib> <bib> <book> <publisher> Addison-Wesley </publisher> <book> <publisher> Addison-Wesley </publisher> <author> Serge Abiteboul </author> <author> Serge Abiteboul </author> <author> <first-name> Rick </first-name> <author> <first-name> Rick </first-name> <last-name> Hull </last-name> <last-name> Hull </last-name> </author> </author> <author> Victor Vianu </author> <author> Victor Vianu </author> <title> Foundations of Databases </title> <title> Foundations of Databases </title> <year> 1995 </year> <year> 1995 </year> </book> </book> <book price=“55”> <book price=“55”> <publisher> Freeman </publisher> <publisher> Freeman </publisher> <author> Jeffrey D. Ullman </author> <author> Jeffrey D. Ullman </author> <title> Principles of Database and Knowledge Base Systems </title> <title> Principles of Database and Knowledge Base Systems </title> <year> 1998 </year> <year> 1998 </year> </book> </book> </bib> </bib> CS/INFO 13 330 Data Model for XPath The root Processing Comment bib The root element instruction book book publisher author . . . . Addison-Wesley Serge Abiteboul CS/INFO 14 330 XPath: Simple Expressions /bib/book/year Result: <year> 1995 </year> <year> 1998 </year> /bib/paper/year Result: empty (there were no papers) CS/INFO 15 330 5

  6. XPath: Restricted Kleene Closure //author Result: <author> Serge Abiteboul </author> <author> <first-name> Rick </first-name> <last-name> Hull </last-name> </author> <author> Victor Vianu </author> <author> Jeffrey D. Ullman </author> /bib//first-name Result: <first-name> Rick </first-name> CS/INFO 16 330 Xpath: Functions /bib/book/author/text() Result: Serge Abiteboul Jeffrey D. Ullman Rick Hull doesn’t appear because he has firstname, lastname Functions in XPath: • text() = matches the text value • node() = matches any node (= * or @* or text()) • name() = returns the name of the current tag CS/INFO 17 330 Xpath: Wildcard //author/* Result: <first-name> Rick </first-name> <last-name> Hull </last-name> * Matches any element CS/INFO 18 330 6

  7. Xpath: Attribute Nodes /bib/book/@price Result: “55” @price means that price is has to be an attribute CS/INFO 19 330 Xpath: Qualifiers /bib/book/author[firstname] Result: <author> <first-name> Rick </first-name> <last-name> Hull </last-name> </author> CS/INFO 20 330 Xpath: More Qualifiers /bib/book/author[firstname][address[//zip][city]]/lastname Result: <lastname> … </lastname> <lastname> … </lastname> CS/INFO 21 330 7

  8. Xpath: More Qualifiers /bib/book[@price < “60”] /bib/book[author/@age < “25”] /bib/book[author/text()] CS/INFO 22 330 Xpath: Summary bib matches a bib element * matches any element / matches the root element /bib matches a bib element under root bib/paper matches a paper in bib bib//paper matches a paper in bib, at any depth //paper matches a paper at any depth paper|book matches a paper or a book @price matches a price attribute bib/book/@price matches price attribute in book, in bib bib/book/[@price<“55”]/author/lastname matches… CS/INFO 23 330 8

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend