XPath Lecture 32 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... ·...

Post on 26-Jul-2020

8 views 0 download

Transcript of XPath Lecture 32 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... ·...

XPathLecture 32

Robb T. Koether

Hampden-Sydney College

Mon, Apr 9, 2012

Robb T. Koether (Hampden-Sydney College) XPathLecture 32 Mon, Apr 9, 2012 1 / 30

1 Introduction

2 XPath Expressions

3 XPath Functions

4 Predicates

5 Axes

Robb T. Koether (Hampden-Sydney College) XPathLecture 32 Mon, Apr 9, 2012 2 / 30

Outline

1 Introduction

2 XPath Expressions

3 XPath Functions

4 Predicates

5 Axes

Robb T. Koether (Hampden-Sydney College) XPathLecture 32 Mon, Apr 9, 2012 3 / 30

What is XPath?

XPath is a method of writing expressions that will parse an XMLdocument to extract desired information.XPath expressions describe paths through the XML tree.

Robb T. Koether (Hampden-Sydney College) XPathLecture 32 Mon, Apr 9, 2012 4 / 30

XPath Nodes

XPath expressions search for matching nodes.There are several types of node in an XML document.

Document nodeElement nodesAttribute nodesText nodesComment nodesInstruction nodesNamespace nodes

Look at the files company_rdb.xml and company.xml to seethe structure.

Robb T. Koether (Hampden-Sydney College) XPathLecture 32 Mon, Apr 9, 2012 5 / 30

Outline

1 Introduction

2 XPath Expressions

3 XPath Functions

4 Predicates

5 Axes

Robb T. Koether (Hampden-Sydney College) XPathLecture 32 Mon, Apr 9, 2012 6 / 30

XPath Expressions

The Root Nodedoc("company.xml")/company

XPath expressions begin with doc("file_name");If the root node is named company, then the above expressionmatches the entire XML document, beginning at the root elementcompany.

Robb T. Koether (Hampden-Sydney College) XPathLecture 32 Mon, Apr 9, 2012 7 / 30

XPath Expressions

Navigating Down the Treedoc("company.xml")/company/departments/department

We may move any number of levels down the tree by naming theelements.The value of the expression is a list of all elements (including theircontents) that match the expression.The above expression matches all department elements thatare children of departments elements that are children of thecompany element.Try out various paths with the company database.

Robb T. Koether (Hampden-Sydney College) XPathLecture 32 Mon, Apr 9, 2012 8 / 30

XPath Expressions

The Double Slashdoc("company.xml")//dep_name

The double slash // means “any number of levels down.”The above expression matches dep_name elements at any levelin the document.

Robb T. Koether (Hampden-Sydney College) XPathLecture 32 Mon, Apr 9, 2012 9 / 30

XPath Expressions

The Wildcarddoc("company.xml")//department/*

The asterisk * is a wildcard that means “any child.”The above expression matches all child elements of departmentelements.

Robb T. Koether (Hampden-Sydney College) XPathLecture 32 Mon, Apr 9, 2012 10 / 30

XPath Expressions

The Or Operatordoc("company.xml")//(fname | lname)

The vertical stroke | means “or.”We may use it to match more than one type of element.The above expression matches fname and lname elements.

Robb T. Koether (Hampden-Sydney College) XPathLecture 32 Mon, Apr 9, 2012 11 / 30

XPath Expressions

The Current Nodedoc("company.xml")//dependent/.doc("company.xml")//dependent

The dot . means “the current node.”The two expressions above have the same value.As we will see, the notion of “current node” is fundamental inXPath expressions.

Robb T. Koether (Hampden-Sydney College) XPathLecture 32 Mon, Apr 9, 2012 12 / 30

XPath Expressions

The Current Nodedoc("company.xml")//department/dnamedoc("company.xml")//department/./dname

These two expressions also have the same value.

Robb T. Koether (Hampden-Sydney College) XPathLecture 32 Mon, Apr 9, 2012 13 / 30

XPath Expressions

The Parent Nodedoc("company.xml")//dependents/../salary

Two dots .. means “the parent node.”The above expression will match the salary elements of everyemployee with dependents.

Robb T. Koether (Hampden-Sydney College) XPathLecture 32 Mon, Apr 9, 2012 14 / 30

Outline

1 Introduction

2 XPath Expressions

3 XPath Functions

4 Predicates

5 Axes

Robb T. Koether (Hampden-Sydney College) XPathLecture 32 Mon, Apr 9, 2012 15 / 30

XPath Functions

XPath provides a number of functions.data()count()sum()avg()concat()

Seehttp://www.w3schools.com/xpath/xpath_functions.aspfor a complete list of XPath functions.

Robb T. Koether (Hampden-Sydney College) XPathLecture 32 Mon, Apr 9, 2012 16 / 30

XPath Functions

The data() Functiondoc("company.xml")//dep_name/data(.)

The data() function returns the text content of an element.This includes the text content of all of the subelements,concatenated together.The above expression produces the names of the dependents ofall employees.

Robb T. Koether (Hampden-Sydney College) XPathLecture 32 Mon, Apr 9, 2012 17 / 30

XPath Functions

The data() Functiondoc("company.xml")//employee/data(.)

This expression produces all the text (names, birthdays, salaries,etc.) of all employees, concatenated into one long string.Obviously, we need to use data() with caution.

Robb T. Koether (Hampden-Sydney College) XPathLecture 32 Mon, Apr 9, 2012 18 / 30

XPath Functions

The count() Functioncount(doc("company.xml")//dependent)

The count() function returns the number of elements returnedby the XPath expression.The above expression counts the number of dependents of allemployees.

Robb T. Koether (Hampden-Sydney College) XPathLecture 32 Mon, Apr 9, 2012 19 / 30

XPath Functions

The count() Functiondoc("company.xml")//dependents/count(dependent)doc("company.xml")//employee/count(dependent)doc("company.xml")//employee/count(*/dependent)doc("company.xml")//employee/count(dependents/dependent)doc("company.xml")//employee/dependents/count(dependent)doc("company.xml")//count(dependent)

What will each of the above expressions return?

Robb T. Koether (Hampden-Sydney College) XPathLecture 32 Mon, Apr 9, 2012 20 / 30

XPath Functions

The sum() Functiondoc("company.xml")//department/sum(.//salary)

The sum() function returns the sum of the values of elementsreturned by the XPath expression.The above expression returns the sums of the employees’ salariesin each department.

Robb T. Koether (Hampden-Sydney College) XPathLecture 32 Mon, Apr 9, 2012 21 / 30

XPath Functions

The sum() Functiondoc("company.xml")//department/sum(salary)doc("company.xml")//department/sum(//salary)

What do the above expressions return?

Robb T. Koether (Hampden-Sydney College) XPathLecture 32 Mon, Apr 9, 2012 22 / 30

XPath Predicates

Write XPath expressions that will match the following.The total number of hours worked on projects by each employee.The total number of hours worked on projects within eachdepartment.

Robb T. Koether (Hampden-Sydney College) XPathLecture 32 Mon, Apr 9, 2012 23 / 30

Outline

1 Introduction

2 XPath Expressions

3 XPath Functions

4 Predicates

5 Axes

Robb T. Koether (Hampden-Sydney College) XPathLecture 32 Mon, Apr 9, 2012 24 / 30

XPath Predicates

A predicate is a statement that may be true or false.In an XPath expression, we may include predicates at any level.The predicate is applied to the children of the current node.The XPath expression will match elements in the expression forwhich the predicate is true.

Robb T. Koether (Hampden-Sydney College) XPathLecture 32 Mon, Apr 9, 2012 25 / 30

XPath Predicates

The Parent Nodedoc("company.xml")//employee[salary < 40000]/lname

The above expression will match the lname elements ofemployees who make less than $40,000.

Robb T. Koether (Hampden-Sydney College) XPathLecture 32 Mon, Apr 9, 2012 26 / 30

XPath Predicates

Write XPath expressions that will match the following.The first and last names of all employees.The last names of all employees with at least two dependents.The last names of all employees who work on the Clothing project.The last names of all employees who work on at least two projects.The sum of the salaries of all employees making over $50,000.The average salary of all female employees.

Robb T. Koether (Hampden-Sydney College) XPathLecture 32 Mon, Apr 9, 2012 27 / 30

Outline

1 Introduction

2 XPath Expressions

3 XPath Functions

4 Predicates

5 Axes

Robb T. Koether (Hampden-Sydney College) XPathLecture 32 Mon, Apr 9, 2012 28 / 30

XPath Axes

An axis is an expression that helps navigate through the XML tree.Examples are /, ., and ..

The following axes are available.selfchildparentancestordescendentpreceding-siblingfollowing-sibling

The axis is always relative to the current node.

Robb T. Koether (Hampden-Sydney College) XPathLecture 32 Mon, Apr 9, 2012 29 / 30

XPath

The Parent Axisdoc("company.xml")//dependent/../../lname/data(.)

The above expression will produce the last name of the parent ofeach dependent.

Robb T. Koether (Hampden-Sydney College) XPathLecture 32 Mon, Apr 9, 2012 30 / 30