1 / 28

XPath and Namespaces

XPath and Namespaces. Dr. Alexandra I. Cristea http://www.dcs.warwick.ac.uk/~acristea/. XPath. XPath is a syntax for defining parts of an XML document XPath uses path expressions to navigate in XML documents XPath contains a library of standard functions

onofre
Download Presentation

XPath and Namespaces

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 and Namespaces Dr. Alexandra I. Cristea http://www.dcs.warwick.ac.uk/~acristea/

  2. XPath • XPath is a syntax for defining parts of an XML document • XPath uses path expressions to navigate in XML documents • XPath contains a library of standard functions • XPath is a major element in XSLT • XPath is a W3C recommendation, thus a Standard (16. November 1999 )

  3. XPath Path Expressions • Uses path expressions to select nodes or node-sets in an XML document. • These path expressions look very much like the expressions you see when you work with a traditional computer file system.

  4. XPath Standard Functions • over 100 built-in functions. • string values, • numeric values, • date and time comparison, • node and QName manipulation, • sequence manipulation, • Boolean values, • and more.

  5. XPath Terminology • Nodes • Atomic values • Items (atomic values or nodes) • Relationships of nodes • Parent • Children • Siblings • Ancestors • Descendants

  6. XPath Nodes • 7 kinds of nodes: • element, • attribute, • text, • namespace, • processing-instruction, • comment, and • document (root) nodes. • XML documents are treated as trees of nodes. The root of the tree is called the document node (or root node).

  7. Document (root) node Element node Attribute node Nodes Examples <?xml version="1.0" encoding="ISO-8859-1"?> <bookstore> <book> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> </bookstore>

  8. Atomic values Examples* <?xml version="1.0" encoding="ISO-8859-1"?> <bookstore> <book> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> </bookstore> *nodes with no children or parent

  9. Selecting nodes

  10. Examples of selecting nodes

  11. Predicates • Predicates are used to find a specific node or a node that contains a specific value. • Predicates are always embedded in square brackets.

  12. Example predicates

  13. Example predicates – cont. Selects all the book elements of the bookstore element that have a price element with a value greater than 35.00 Selects all the title elements of the book elements of the bookstore element that have a price element with a value greater than 35.00

  14. Selecting Unknown Nodes

  15. Example: selecting several paths Selects all the title AND price elements in the document Selects all the title elements of the book element of the bookstore element AND all the price elements in the document

  16. Location Path Expression • A location path can be absolute or relative. • An absolute location path: /step/step/... • A relative location path: step/step/... • Location step: axisname::nodetest[predicate]

  17. XPath Axes

  18. axisname::nodetest[predicate] • //DDD/parent::* <AAA>  <BBB>               <DDD>                </DDD> </BBB> </AAA>

  19. axisname::nodetest[predicate] • //BBB/child::* <AAA> <BBB>  <DDD>                </DDD>           </BBB> </AAA> Note: /AAA is equivalent to /child::AAA

  20. More examples • http://www.zvon.org/xxl/XPathTutorial/General/examples.html • Check basics, //, *, predicates, attributes, functions (new ones: count, name, normalize-space, starts-with, contains, string-length, floor, ceiling), axes, operators (mod) • Note: The ancestor, descendant, following, preceding and self axes partition a document (ignoring attribute and namespace nodes): they do not overlap and together they contain all the nodes in the document. (see example)

  21. XPath Conclusion • We have learned: • XPath definition • Path expressions • Standard functions • Terminology • Predicates • Location paths • Axes • Some operators

  22. Before we go on, one more thing about XML: • XML Namespaces

  23. Naming ambiguity

  24. The Idea to Solve it • Assign a URI (~ URL) to every sub-language: • E.g., for XHTML 1.0: http://www.w3.org/1999/xhtml • Qnames: Qualify element names with URIs: • {http://www.w3.org/1999/xhtml}head Web Naming and Addressing Overview (URIs, URLs, ...)

  25. The actual solution • Namespace declarations bind URIs to prefixes: • Default namespace (no prefix) declared with: xmlns=“…” • Lexical Scope • Attribute names can also be prefixed

  26. Applying namespaces

  27. Next we look at how to query XML • This can be done, to some extent, as we have seen, within XSLT, • but the main language developed for this purpose is …

More Related