28
Dr. Alexandra I. Cristea http://www.dcs.warwick.ac.uk/ ~acristea/ XPath and Namespaces

Dr. Alexandra I. Cristea acristea/ XPath and Namespaces

Embed Size (px)

Citation preview

Page 1: Dr. Alexandra I. Cristea acristea/ XPath and Namespaces

Dr. Alexandra I. Cristea

http://www.dcs.warwick.ac.uk/~acristea/

XPath and Namespaces

Page 2: Dr. Alexandra I. Cristea acristea/ XPath and Namespaces

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 )

Page 3: Dr. Alexandra I. Cristea acristea/ XPath and Namespaces

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.

Page 4: Dr. Alexandra I. Cristea acristea/ XPath and Namespaces

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.

Page 5: Dr. Alexandra I. Cristea acristea/ XPath and Namespaces

5

XPath Terminology• Nodes

• Atomic values

• Items (atomic values or nodes)

• Relationships of nodes– Parent– Children– Siblings– Ancestors– Descendants

Page 6: Dr. Alexandra I. Cristea acristea/ XPath and Namespaces

6

XPath Nodes• 7 kinds of nodes:

1. element,

2. attribute,

3. text,

4. namespace,

5. processing-instruction,

6. comment, and

7. document (root) nodes. • XML documents are treated as trees of nodes. The root

of the tree is called the document node (or root node).

Page 7: Dr. Alexandra I. Cristea acristea/ XPath and Namespaces

7

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>

Document (root) node

Element node

Attribute node

Page 8: Dr. Alexandra I. Cristea acristea/ XPath and Namespaces

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

Page 9: Dr. Alexandra I. Cristea acristea/ XPath and Namespaces

9

Selecting nodes

Expression Description

nodename Selects all child nodes of the node

/ Selects from the root node

// Selects nodes in the document from the current node down that match the selection no matter where they are

. Selects the current node

.. Selects the parent of the current node

@ Selects attributes

Page 10: Dr. Alexandra I. Cristea acristea/ XPath and Namespaces

10

Examples of selecting nodesPath Expression Result

bookstore Selects all the child nodes of the bookstore element

/bookstore Selects the root element bookstoreNote: If the path starts with a slash ( / ) it always represents an absolute path to an element!

bookstore/book Selects all book elements that are children of bookstore

//book Selects all book elements no matter where they are in the document

bookstore//book Selects all book elements that are descendant of the bookstore element, no matter where they are under the bookstore element

//@lang Selects all attributes that are named lang

Page 11: Dr. Alexandra I. Cristea acristea/ XPath and Namespaces

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.

Page 12: Dr. Alexandra I. Cristea acristea/ XPath and Namespaces

12

Example predicates

Path Expression Result

/bookstore/book[1] Selects the first book element that is the child of the bookstore element

/bookstore/book[last()] Selects the last book element that is the child of the bookstore element

/bookstore/book[last()-1] Selects the last but one book element thatis the child of the bookstore element

/bookstore/book[position()<3]

Selects the first two book elements that are children of the bookstore element

Page 13: Dr. Alexandra I. Cristea acristea/ XPath and Namespaces

13

Example predicates – cont. Path Expression Result

//title[@lang] Selects all the title elements that have an attribute named lang

//title[@lang='eng'] Selects all the title elements that have an attribute named lang with a value of 'eng'

/bookstore/book[price>35.00]

/bookstore/book[price>35.00]/title

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

Page 14: Dr. Alexandra I. Cristea acristea/ XPath and Namespaces

14

Selecting Unknown Nodes

Wildcard Description

* Matches any element node

@* Matches any attribute node

node() Matches any node of any kind

Page 15: Dr. Alexandra I. Cristea acristea/ XPath and Namespaces

15

Example: selecting several paths

Path Expression Result

//book/title | //book/price Selects all the title AND price elements of all book elements

//title | //price

/bookstore/book/title | //price

Selects all the title AND price elements in the documentSelects all the title elements of the book element of the bookstore element AND all the price elements in the document

Page 16: Dr. Alexandra I. Cristea acristea/ XPath and Namespaces

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]

Page 17: Dr. Alexandra I. Cristea acristea/ XPath and Namespaces

17

XPath Axesself

child parent

ancestor descendant

ancestor-or-self descendant-or-self

preceding-sibling following-sibling

preceding following

attribute

namespace

Page 18: Dr. Alexandra I. Cristea acristea/ XPath and Namespaces

18

AxisName Result

ancestor Selects all ancestors (parent, grandparent, etc.) of the current node

ancestor-or-self Selects all ancestors (parent, grandparent, etc.) of the current node and the current node itself

attribute Selects all attributes of the current node

child Selects all children of the current node

descendant Selects all descendants (children, grandchildren, etc.) of the current node

descendant-or-self

Selects all descendants (children, grandchildren, etc.) of the current node and the current node itself

following Selects everything in the document after the closing tag of the current node

following-sibling

Selects all siblings after the current node

namespace Selects all namespace nodes of the current node

parent Selects the parent of the current node

preceding Selects everything in the document that is before the start tag ofthe current node

preceding-sibling

Selects all siblings before the current node

self Selects the current node

Page 19: Dr. Alexandra I. Cristea acristea/ XPath and Namespaces

19

axisname::nodetest[predicate]

• //DDD/parent::*

<AAA>           <BBB>               <DDD>

               </DDD>           </BBB>

</AAA>

Page 20: Dr. Alexandra I. Cristea acristea/ XPath and Namespaces

20

axisname::nodetest[predicate]

• //BBB/child::*

<AAA>           <BBB>               <DDD>

               </DDD>           </BBB>

</AAA>

Note: /AAA is equivalent to /child::AAA

Page 21: Dr. Alexandra I. Cristea acristea/ XPath and Namespaces

21

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)

Page 22: Dr. Alexandra I. Cristea acristea/ XPath and Namespaces

22

XPath Conclusion• We have learned:

– XPath definition– Path expressions– Standard functions– Terminology– Predicates– Location paths– Axes– Some operators

Page 23: Dr. Alexandra I. Cristea acristea/ XPath and Namespaces

23

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

• XML Namespaces

Page 24: Dr. Alexandra I. Cristea acristea/ XPath and Namespaces

24

Naming ambiguity

Page 25: Dr. Alexandra I. Cristea acristea/ XPath and Namespaces

25

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, ...)

Page 26: Dr. Alexandra I. Cristea acristea/ XPath and Namespaces

26

The actual solution

• Namespace declarations bind URIs to prefixes:

• Default namespace (no prefix) declared with: xmlns=“…”

• Lexical Scope

• Attribute names can also be prefixed

Page 27: Dr. Alexandra I. Cristea acristea/ XPath and Namespaces

27

Applying namespaces

Page 28: Dr. Alexandra I. Cristea acristea/ XPath and Namespaces

28

• 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 …