1 XPath XPath became a W3C Recommendation 16. November 1999 XPath is a language for finding...

24
1 XPath XPath became a W3C Recommendation 16. November 1999 XPath is a language for finding information in an XML document XPath is used to navigate through elements and attributes in an XML document

Transcript of 1 XPath XPath became a W3C Recommendation 16. November 1999 XPath is a language for finding...

Page 1: 1 XPath XPath became a W3C Recommendation 16. November 1999 XPath is a language for finding information in an XML document XPath is used to navigate through.

1

XPath• XPath became a W3C Recommendation 16.

November 1999 • XPath is a language for finding information in

an XML document • XPath is used to navigate through elements

and attributes in an XML document

Page 2: 1 XPath XPath became a W3C Recommendation 16. November 1999 XPath is a language for finding information in an XML document XPath is used to navigate through.

2

XPath• XPath uses path expressions to select nodes or node-

sets in an XML document. • XPath includes over 100 built-in functions• XML document is treated as a tree of nodes• there are seven kinds of nodes: element, attribute, text,

namespace, processing-instruction, comment, and document (root) nodes.

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

Atomic values are nodes with no children or parent.

Page 3: 1 XPath XPath became a W3C Recommendation 16. November 1999 XPath is a language for finding information in an XML document XPath is used to navigate through.

3

Relationship of Nodes

• Parent• Children• Siblings• Ancestors• Descendants

<bookstore>

<book>

<title>Harry Potter</title>

<author>J K. Rowling</author>

<year>2005</year>

<price>29.99</price>

</book>

</bookstore>

Page 4: 1 XPath XPath became a W3C Recommendation 16. November 1999 XPath is a language for finding information in an XML document XPath is used to navigate through.

4

Relationship of Nodes

• Parent• Children• Siblings• Ancestors• Descendants

<bookstore>

<book>

<title>Harry Potter</title>

<author>J K. Rowling</author>

<year>2005</year>

<price>29.99</price>

</book>

</bookstore>

Page 5: 1 XPath XPath became a W3C Recommendation 16. November 1999 XPath is a language for finding information in an XML document XPath is used to navigate through.

5

Relationship of Nodes

• Parent• Children• Siblings• Ancestors• Descendants

<bookstore>

<book>

<title>Harry Potter</title>

<author>J K. Rowling</author>

<year>2005</year>

<price>29.99</price>

</book>

</bookstore>

Page 6: 1 XPath XPath became a W3C Recommendation 16. November 1999 XPath is a language for finding information in an XML document XPath is used to navigate through.

6

Xpath Syntax

Page 7: 1 XPath XPath became a W3C Recommendation 16. November 1999 XPath is a language for finding information in an XML document XPath is used to navigate through.

7

Sample xml document

<?xml version="1.0" encoding="ISO-8859-1"?>

<bookstore>

<book>

<title lang="eng">Harry Potter</title> <price>29.99</price>

</book>

<book>

<title lang="eng">Learning XML</title>

<price>39.95</price>

</book>

</bookstore>

Page 8: 1 XPath XPath became a W3C Recommendation 16. November 1999 XPath is a language for finding information in an XML document XPath is used to navigate through.

Xpath Expressions

8

Page 9: 1 XPath XPath became a W3C Recommendation 16. November 1999 XPath is a language for finding information in an XML document XPath is used to navigate through.

9

XPath Syntax

• Predicates– Predicates are used to find a specific node or

a node that contains a specific value.– Predicates are always embedded in square

brackets.– Example

• Selects the first book element that is the child of the bookstore element.

– /bookstore/book[1]

Page 10: 1 XPath XPath became a W3C Recommendation 16. November 1999 XPath is a language for finding information in an XML document XPath is used to navigate through.

10

Predicate (examples)

Page 11: 1 XPath XPath became a W3C Recommendation 16. November 1999 XPath is a language for finding information in an XML document XPath is used to navigate through.

11

Predicate (examples)

Page 12: 1 XPath XPath became a W3C Recommendation 16. November 1999 XPath is a language for finding information in an XML document XPath is used to navigate through.

12

Predicate (examples)

• Selecting Unknown Nodes– XPath wildcards can be used to select

unknown XML elements.

Page 13: 1 XPath XPath became a W3C Recommendation 16. November 1999 XPath is a language for finding information in an XML document XPath is used to navigate through.

13

Predicate (examples)

• /bookstore/* – Selects all child nodes of the bookstore

element• //*

– Selects all elements in the document• //title[@*]

– Select all title element which have any attribute

Page 14: 1 XPath XPath became a W3C Recommendation 16. November 1999 XPath is a language for finding information in an XML document XPath is used to navigate through.

14

XPath Syntax

• Selecting Several Paths– By using the | operator in an XPath

• Example– //book/title | //book/price

• Selects all the title And price elements of all book elements

Page 15: 1 XPath XPath became a W3C Recommendation 16. November 1999 XPath is a language for finding information in an XML document XPath is used to navigate through.

15

XPath Syntax

• //title | //price– Selects all the title And price elements in the

documents• /bookstore/book/title | //price

– Selects all the title elements of the book element of the bookstore element And all the price elements in the document

Page 16: 1 XPath XPath became a W3C Recommendation 16. November 1999 XPath is a language for finding information in an XML document XPath is used to navigate through.

16

Location Path Expression• An absolute location path:

/step/step/...• A relative location path:

step/step/...

– Where each step is evaluated against the nodes in the current node-set.

– A step consists of:• an axis (defines the tree-relationship between the selected

nodes and the current node) • a node-test (identifies a node within an axis) • zero or more predicates (to further refine the selected node-

set)

Page 17: 1 XPath XPath became a W3C Recommendation 16. November 1999 XPath is a language for finding information in an XML document XPath is used to navigate through.

17

Location Path Expression (cont.)

• The syntax for a location step is:– axisname::nodetest [ predicate ]

• Example– child::book

• Selects all book nodes that are children of the current node

– child::*• Selects all children of the current node

– attribute::*• Selects all attributes of the current node

Page 18: 1 XPath XPath became a W3C Recommendation 16. November 1999 XPath is a language for finding information in an XML document XPath is used to navigate through.

18

Location Path Expression (cont.)

• child::text()– Selects all text child nodes of the current node

• child::node()– Selects all child nodes of the current node

• descendant::book– Selects all book descendants of the current

node• ancestor::book

– Selects all book ancestors of the current node

Page 19: 1 XPath XPath became a W3C Recommendation 16. November 1999 XPath is a language for finding information in an XML document XPath is used to navigate through.

19

XPath Operators

• An XPath expression returns either a node-set, a string, a Boolean, or a number.

• Examples– Operator |

• //book | //cd• Returns a node-set with all book and cd elements

Page 20: 1 XPath XPath became a W3C Recommendation 16. November 1999 XPath is a language for finding information in an XML document XPath is used to navigate through.

20

XPath Operators

– Other Operators• + , - , * , div• = , != ,< ,> ,>= ,<=• or (logical or)• and (logical and) • mod (division remainder)

Page 21: 1 XPath XPath became a W3C Recommendation 16. November 1999 XPath is a language for finding information in an XML document XPath is used to navigate through.

21

XPath Operators (Example)

• Or – price=9.80 or price=9.70

• true if price is 9.80false if price is 9.50

• and (logical and) – price>9.00 and price<9.90

• true if price is 9.80false if price is 8.50

Page 22: 1 XPath XPath became a W3C Recommendation 16. November 1999 XPath is a language for finding information in an XML document XPath is used to navigate through.

22

XPath

• Core Library Function– Node Set Functions

• Last (),Position () , Count ()

– String Functions• Concat, contains, start-with, length

Page 23: 1 XPath XPath became a W3C Recommendation 16. November 1999 XPath is a language for finding information in an XML document XPath is used to navigate through.

23

XPath

Exercise

Page 24: 1 XPath XPath became a W3C Recommendation 16. November 1999 XPath is a language for finding information in an XML document XPath is used to navigate through.

24

An XML Example<library location="Bremen">

<author name="Henry Wise"><book title="Artificial Intelligence"/><book title="Modern Web Services"/><book title="Theory of Computation"/>

</author><author name="William Smart">

<book title="Artificial Intelligence"/></author><author name="Cynthia Singleton">

<book title="The Semantic Web"/><book title="Browser Technology

Revised"/></author>

</library>

Address all author elements

Address the location attribute nodes within library element nodes

Address all title attribute nodes within book elements anywhere in the document, which have the value “Artificial Intelligence”

Address all books with title “Artificial Intelligence”