Top Banner
1 Quick Intro to XPath Roger L. Costello 14 December, 2012
70

1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

Mar 26, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

1

Quick Intro to XPath

Roger L. Costello14 December, 2012

Page 2: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

2

Objective

• XML Schema 1.1 uses XPath a lot, so if you don't know XPath then you're at a disadvantage.

• The purpose of this short tutorial is to teach you enough XPath that you won't be at a disadvantage.

Page 3: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

3

XPath is not a standalone language

• XPath requires a host language. There are currently several XML languages that host XPath.

Page 4: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

4

XPath is not a standalone language

XPath

XSLT

XQuery

XMLSchemas

XPointer

Schematron

Page 5: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

5

Document/

PI<?xml version=“1.0”?>

ElementFitnessCenter

ElementMember

ElementMember

ElementMember

ElementName

ElementFavoriteColor

TextJeff

Textlightgrey

ElementName

ElementFavoriteColor

TextDavid

Textlightblue

ElementName

ElementFavoriteColor

TextRoger

Textlightyellow

<?xml version="1.0"?><FitnessCenter> <Member> <Name>Jeff</Name> <FavoriteColor>lightgrey</FavoriteColor> </Member> <Member> <Name>David</Name> <FavoriteColor>lightblue</FavoriteColor> </Member> <Member> <Name>Roger</Name> <FavoriteColor>lightyellow</FavoriteColor> </Member></FitnessCenter>

This XML document can berepresented as a tree, as shown below

Page 6: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

6

Terminology - node

Document/

PI<?xml version=“1.0”?>

ElementFitnessCenter

ElementMember

ElementMember

ElementMember

ElementName

ElementFavoriteColor

TextJeff

Textlightgrey

ElementName

ElementFavoriteColor

TextDavid

Textlightblue

ElementName

ElementFavoriteColor

TextRoger

Textlightyellow

Document nodeProcessing Instruction (PI) node

Element nodes

Text nodes

Page 7: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

7

Document/

PI<?xml version=“1.0”?>

ElementFitnessCenter

ElementMember

ElementMember

ElementMember

ElementName

ElementFavoriteColor

TextJeff

Textlightgrey

ElementName

ElementFavoriteColor

TextDavid

Textlightblue

ElementName

ElementFavoriteColor

TextRoger

Textlightyellow

With respect to this node, these are its children

Page 8: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

8

Document/

PI<?xml version=“1.0”?>

ElementFitnessCenter

ElementMember

ElementMember

ElementMember

ElementName

ElementFavoriteColor

TextJeff

Textlightgrey

ElementName

ElementFavoriteColor

TextDavid

Textlightblue

ElementName

ElementFavoriteColor

TextRoger

Textlightyellow

These are its descendant nodes

Page 9: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

9

Document/

PI<?xml version=“1.0”?>

ElementFitnessCenter

ElementMember

ElementMember

ElementMember

ElementName

ElementFavoriteColor

TextJeff

Textlightgrey

ElementName

ElementFavoriteColor

TextDavid

Textlightblue

ElementName

ElementFavoriteColor

TextRoger

Textlightyellow

This is the context node

Page 10: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

10

Document/

PI<?xml version=“1.0”?>

ElementFitnessCenter

ElementMember

ElementMember

ElementMember

ElementName

ElementFavoriteColor

TextJeff

Textlightgrey

ElementName

ElementFavoriteColor

TextDavid

Textlightblue

ElementName

ElementFavoriteColor

TextRoger

Textlightyellow

That's its parent

Page 11: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

11

Document/

PI<?xml version=“1.0”?>

ElementFitnessCenter

ElementMember

ElementMember

ElementMember

ElementName

ElementFavoriteColor

TextJeff

Textlightgrey

ElementName

ElementFavoriteColor

TextDavid

Textlightblue

ElementName

ElementFavoriteColor

TextRoger

Textlightyellow

Those are its ancestors

Page 12: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

12

Document/

PI<?xml version=“1.0”?>

ElementFitnessCenter

ElementMember

ElementMember

ElementMember

ElementName

ElementFavoriteColor

TextJeff

Textlightgrey

ElementName

ElementFavoriteColor

TextDavid

Textlightblue

ElementName

ElementFavoriteColor

TextRoger

Textlightyellow

It has 2 siblings

Page 13: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

13

Document/

PI<?xml version=“1.0”?>

ElementFitnessCenter

ElementMember

ElementMember

ElementMember

ElementName

ElementFavoriteColor

TextJeff

Textlightgrey

ElementName

ElementFavoriteColor

TextDavid

Textlightblue

ElementName

ElementFavoriteColor

TextRoger

Textlightyellow

They are following-siblings

Page 14: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

14

Document/

PI<?xml version=“1.0”?>

ElementFitnessCenter

ElementMember

ElementMember

ElementMember

ElementName

ElementFavoriteColor

TextJeff

Textlightgrey

ElementName

ElementFavoriteColor

TextDavid

Textlightblue

ElementName

ElementFavoriteColor

TextRoger

Textlightyellow

It has no preceding-siblings

Page 15: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

15

Here are the capabilities of XPath

• XPath provides a syntax for: – navigating around an XML document– selecting nodes and values – comparing node values– performing arithmetic on node values

• XPath provides some functions (e.g., concat(), substring(), etc.) to facilitate the above.

Page 16: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

16

Document/

PI<?xml version=“1.0”?>

ElementDocument

ElementPara

ElementPara

ElementPara

TextOne if …

TextAnd I …

TextReady to

<?xml version="1.0"?>Document classification="secret"> <Para classification="unclassified"> One if by land, two if by sea; </Para> <Para classification="confidential"> And I on the opposite shore will be, Ready to ride and spread the alarm </Para> <Para classification="unclassified"> Ready to ride and spread the alarm Through every Middlesex, village and farm, </Para></Document>

This XML document can berepresented as a tree, as shown below

Attributeclassification=“secret”

Attributeclassification=“unclassified”

Attributeclassification=“confidential”

Attributeclassification=“unclassified”

See document.xml in the xpathfolder, within the examples folder.

Page 17: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

17

Execute XPath using Oxygen XML

Type your XPath expression here

Change this to XPath 1.0

Page 18: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

18

Use XPath Builderfor long XPathexpressions

Page 19: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

19

Please Run the XPath Expressions

• The following slides contain XPath expressions.

• It's important that you copy the expression on the slide and paste it into Oxygen XML to see what the expression does.

• First, copy the XML document on slide 16, save it to a file, then drag and drop the file into Oxygen XML.

Page 20: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

20

Select all Para Elements

/Document/Para

Page 21: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

21

/Document/Para

This is an absolute XPath expression

Page 22: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

22

Establish a Context Node

Click on this to establish it as the "context node"(any XPath expressions will be relative to it)

Page 23: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

23

Relative XPath Expression

In Oxygen XML click on <Document> toestablish the “context node” and then typethis in the XPath box:

Para

Page 24: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

24

Select all Para Elements

//Para

descendents

Page 25: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

25

Select the first Para

//Para[1]

Page 26: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

26

Select the last Para

//Para[last()]

Page 27: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

27

Select the classification attribute of the first Para

//Para[1]/@classification

Page 28: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

28

Is the Document element’s classification top-secret?

/Document/@classification = 'top-secret'

Page 29: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

29

Is the Document element’s classification top-secret or

secret?

(/Document/@classification = 'top-secret') or (/Document/@classification='secret')

Page 30: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

30

A or BA and Bnot(A)

Logical Operators

Page 31: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

31

Select all Para’s with a secret classification

//Para[@classification = 'secret']

Page 32: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

32

Check that no Para has a top-secret classification

not(//Para[@classification = 'top-secret'])

Page 33: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

33

Establish a New Context Node

Make the second Para the context node

Page 34: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

34

Select the Following Siblings

following-sibling::*

Page 35: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

35

Select the FirstFollowing Sibling

following-sibling::*[1]

Page 36: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

36

Add Another Element

Add this <Test> element after the last Para

Page 37: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

37

Select the Following Para Siblings

following-sibling::Para

Page 38: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

38

Select all Following Siblings

following-sibling::*

Page 39: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

39

Select all Preceding Siblings

preceding-sibling::*

Page 40: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

40

Make Document the Context

Click on Document to make it thecontext node.

Page 41: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

41

Equivalent!

Para[1]

child::Para[1]

Page 42: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

42

Make Para[2] the context

Establish this as the context node.

Page 43: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

43

Get parent element's classification

../@classification

Page 44: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

44

Equivalent!

../@classification

parent::*/@classification

Page 45: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

45

Axis

following-siblingpreceding-siblingchildparentancestordescendentself

Page 46: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

46

Count the number of Para elements

count(//Para)

Page 47: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

47

Count the number of Para elements with secret

classification

count(//Para[@classification = 'secret'])

Page 48: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

48

Does the first Para element contain the string

“SCRIPT”?

contains(//Para[1], 'SCRIPT')

Page 49: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

49

Select all nodes containing the string “SCRIPT”

//node()[contains(., 'SCRIPT')]

The node() function matches on these nodes: - element - text - comment - processing instructions (PIs)Note that it does not match on these nodes: - attribute - document

Page 50: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

50

Count the number of nodes containing the

string “SCRIPT”

count(//node()[contains(., 'SCRIPT')])

Page 51: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

51

Select the first 20 characters of the first Para

substring(//Para[1], 1, 20)

Page 52: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

52

What's the length of the content of the first Para?

string-length(//Para[1])

Page 53: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

53

translate(/Document/@classification, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')

Convert Document’s classification to lowercase

Page 54: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

54

Add a new <Cost> element

Add this element and establishDocument as the context node.

Page 55: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

55

Multiply Cost by 2

Cost * 2

Page 56: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

56

N mod X = the remainder of dividing N by X

Cost mod 2

Page 57: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

57

Arithmetic Operators

*mod- (leave space on either side)div+

Page 58: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

58

Set this to XPath 2.0

Page 59: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

59

Does Document’s classification match one in

Classifications.xml?

/Document/@classification = doc('Classifications.xml')/Classifications/li

Page 60: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

60

Do the first two Para's have the same classification?

Para[1]/@classification eq Para[2]/@classification

Page 61: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

61

eq means equalne means not equallt means less thangt means greater thanle means less than or equal toge means greater than or equal to

Boolean Operators

Page 62: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

62

if Document's classification is top-secret then there can be no Para with a classification

not equal to top-secret

if (/Document/@classification eq 'top-secret') then not(//Para[@classification ne 'top-secret']) else true()

Page 63: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

63

Two built-in functions

true()

false()

Page 64: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

64

Cast a value to a numeric type

number(Cost)

Page 65: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

65Check that Document's children are: multiple Para's,

1 Test, and 1 Cost (and nothing else)

Para[2] and Test and Cost and empty(* except (Para, Test[1], Cost[1]))

Page 66: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

66

The sum() function

<?xml version="1.0"?><numbers> <number>23</number> <number>5</number> <number>-41</number> <number>50</number> <number>12</number></numbers>

sum(//number) returns 49.0

Page 67: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

67

Check that every Publisher has a string-length le 140

<BookStore> <Book> <Title>My Life and Times</Title> <Author>Paul McCartney</Author> <Date>1998</Date> <ISBN>1-56592-235-2</ISBN> <Publisher>McMillin Publishing</Publisher> <Author>John Ghostwriter</Author> </Book> <Book> <Publisher>Dell Publishing Co.</Publisher> <Author>Richard Bach</Author> <Date>1977</Date> <ISBN>0-440-34319-4</ISBN> <Title>Illusions The Adventures of a Reluctant Messiah</Title> </Book> <Book> <ISBN>0-06-064831-7</ISBN> <Title>The First and Last Freedom</Title> <Author>J. Krishnamurti</Author> <Date>1954</Date> <Publisher>Harper &amp; Row</Publisher> </Book></BookStore>

Page 68: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

68

Check that every Publisher has a string-length le 140

every $i in //Publisher satisfies string-length($i) le 140

Page 69: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

69

The XPath every expression

• The form of the every expression is:every variable in sequence satisfies boolean expression

• The result of the expression is either true or false.

Page 70: 1 Quick Intro to XPath Roger L. Costello 14 December, 2012.

70

Equivalent

every $i in //Publisher satisfies string-length($i) le 140

not(//Publisher[string-length(.) gt 140])