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

30
XPath Lecture 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

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

Page 1: XPath Lecture 32 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 Introduction 2 XPath Expressions 3 XPath Functions 4 Predicates 5 Axes Robb T.

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

Page 2: XPath Lecture 32 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 Introduction 2 XPath Expressions 3 XPath Functions 4 Predicates 5 Axes Robb T.

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

Page 3: XPath Lecture 32 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 Introduction 2 XPath Expressions 3 XPath Functions 4 Predicates 5 Axes Robb T.

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

Page 4: XPath Lecture 32 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 Introduction 2 XPath Expressions 3 XPath Functions 4 Predicates 5 Axes Robb T.

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

Page 5: XPath Lecture 32 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 Introduction 2 XPath Expressions 3 XPath Functions 4 Predicates 5 Axes Robb T.

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

Page 6: XPath Lecture 32 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 Introduction 2 XPath Expressions 3 XPath Functions 4 Predicates 5 Axes Robb T.

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

Page 7: XPath Lecture 32 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 Introduction 2 XPath Expressions 3 XPath Functions 4 Predicates 5 Axes Robb T.

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

Page 8: XPath Lecture 32 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 Introduction 2 XPath Expressions 3 XPath Functions 4 Predicates 5 Axes Robb T.

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

Page 9: XPath Lecture 32 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 Introduction 2 XPath Expressions 3 XPath Functions 4 Predicates 5 Axes Robb T.

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

Page 10: XPath Lecture 32 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 Introduction 2 XPath Expressions 3 XPath Functions 4 Predicates 5 Axes Robb T.

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

Page 11: XPath Lecture 32 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 Introduction 2 XPath Expressions 3 XPath Functions 4 Predicates 5 Axes Robb T.

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

Page 12: XPath Lecture 32 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 Introduction 2 XPath Expressions 3 XPath Functions 4 Predicates 5 Axes Robb T.

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

Page 13: XPath Lecture 32 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 Introduction 2 XPath Expressions 3 XPath Functions 4 Predicates 5 Axes Robb T.

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

Page 14: XPath Lecture 32 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 Introduction 2 XPath Expressions 3 XPath Functions 4 Predicates 5 Axes Robb T.

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

Page 15: XPath Lecture 32 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 Introduction 2 XPath Expressions 3 XPath Functions 4 Predicates 5 Axes Robb T.

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

Page 16: XPath Lecture 32 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 Introduction 2 XPath Expressions 3 XPath Functions 4 Predicates 5 Axes Robb T.

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

Page 17: XPath Lecture 32 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 Introduction 2 XPath Expressions 3 XPath Functions 4 Predicates 5 Axes Robb T.

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

Page 18: XPath Lecture 32 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 Introduction 2 XPath Expressions 3 XPath Functions 4 Predicates 5 Axes Robb T.

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

Page 19: XPath Lecture 32 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 Introduction 2 XPath Expressions 3 XPath Functions 4 Predicates 5 Axes Robb T.

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

Page 20: XPath Lecture 32 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 Introduction 2 XPath Expressions 3 XPath Functions 4 Predicates 5 Axes Robb T.

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

Page 21: XPath Lecture 32 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 Introduction 2 XPath Expressions 3 XPath Functions 4 Predicates 5 Axes Robb T.

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

Page 22: XPath Lecture 32 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 Introduction 2 XPath Expressions 3 XPath Functions 4 Predicates 5 Axes Robb T.

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

Page 23: XPath Lecture 32 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 Introduction 2 XPath Expressions 3 XPath Functions 4 Predicates 5 Axes Robb T.

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

Page 24: XPath Lecture 32 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 Introduction 2 XPath Expressions 3 XPath Functions 4 Predicates 5 Axes Robb T.

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

Page 25: XPath Lecture 32 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 Introduction 2 XPath Expressions 3 XPath Functions 4 Predicates 5 Axes Robb T.

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

Page 26: XPath Lecture 32 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 Introduction 2 XPath Expressions 3 XPath Functions 4 Predicates 5 Axes Robb T.

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

Page 27: XPath Lecture 32 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 Introduction 2 XPath Expressions 3 XPath Functions 4 Predicates 5 Axes Robb T.

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

Page 28: XPath Lecture 32 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 Introduction 2 XPath Expressions 3 XPath Functions 4 Predicates 5 Axes Robb T.

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

Page 29: XPath Lecture 32 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 Introduction 2 XPath Expressions 3 XPath Functions 4 Predicates 5 Axes Robb T.

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

Page 30: XPath Lecture 32 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 Introduction 2 XPath Expressions 3 XPath Functions 4 Predicates 5 Axes Robb T.

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