1 xpath xpath became a w3c recommendation 16. november 1999 xpath is a language for finding...

Post on 11-Jan-2016

219 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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

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.

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>

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>

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>

6

Xpath Syntax

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>

Xpath Expressions

8

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]

10

Predicate (examples)

11

Predicate (examples)

12

Predicate (examples)

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

unknown XML elements.

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

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

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

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)

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

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

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

20

XPath Operators

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

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

22

XPath

• Core Library Function– Node Set Functions

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

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

23

XPath

Exercise

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”

top related