Download presentation
Presentation is loading. Please wait.
Published byStephanie Schmitt Modified over 11 years ago
1
1 Quick Intro to XPath Roger L. Costello 14 December, 2012
2
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.
3
3 XPath is not a standalone language XPath requires a host language. There are currently several XML languages that host XPath.
4
4 XPath is not a standalone language XPath XSLT XQuery XML Schemas XPointer Schematron
5
5 Document / PI Element FitnessCenter Element Member Element Member Element Member Element Name Element FavoriteColor Text Jeff Text lightgrey Element Name Element FavoriteColor Text David Text lightblue Element Name Element FavoriteColor Text Roger Text lightyellow Jeff lightgrey David lightblue Roger lightyellow This XML document can be represented as a tree, as shown below
6
6 Terminology - node Document / PI Element FitnessCenter Element Member Element Member Element Member Element Name Element FavoriteColor Text Jeff Text lightgrey Element Name Element FavoriteColor Text David Text lightblue Element Name Element FavoriteColor Text Roger Text lightyellow Document node Processing Instruction (PI) node Element nodes Text nodes
7
7 Document / PI Element FitnessCenter Element Member Element Member Element Member Element Name Element FavoriteColor Text Jeff Text lightgrey Element Name Element FavoriteColor Text David Text lightblue Element Name Element FavoriteColor Text Roger Text lightyellow With respect to this node, these are its children
8
8 Document / PI Element FitnessCenter Element Member Element Member Element Member Element Name Element FavoriteColor Text Jeff Text lightgrey Element Name Element FavoriteColor Text David Text lightblue Element Name Element FavoriteColor Text Roger Text lightyellow These are its descendant nodes
9
9 Document / PI Element FitnessCenter Element Member Element Member Element Member Element Name Element FavoriteColor Text Jeff Text lightgrey Element Name Element FavoriteColor Text David Text lightblue Element Name Element FavoriteColor Text Roger Text lightyellow This is the context node
10
10 Document / PI Element FitnessCenter Element Member Element Member Element Member Element Name Element FavoriteColor Text Jeff Text lightgrey Element Name Element FavoriteColor Text David Text lightblue Element Name Element FavoriteColor Text Roger Text lightyellow That's its parent
11
11 Document / PI Element FitnessCenter Element Member Element Member Element Member Element Name Element FavoriteColor Text Jeff Text lightgrey Element Name Element FavoriteColor Text David Text lightblue Element Name Element FavoriteColor Text Roger Text lightyellow Those are its ancestors
12
12 Document / PI Element FitnessCenter Element Member Element Member Element Member Element Name Element FavoriteColor Text Jeff Text lightgrey Element Name Element FavoriteColor Text David Text lightblue Element Name Element FavoriteColor Text Roger Text lightyellow It has 2 siblings
13
13 Document / PI Element FitnessCenter Element Member Element Member Element Member Element Name Element FavoriteColor Text Jeff Text lightgrey Element Name Element FavoriteColor Text David Text lightblue Element Name Element FavoriteColor Text Roger Text lightyellow They are following- siblings
14
14 Document / PI Element FitnessCenter Element Member Element Member Element Member Element Name Element FavoriteColor Text Jeff Text lightgrey Element Name Element FavoriteColor Text David Text lightblue Element Name Element FavoriteColor Text Roger Text lightyellow It has no preceding- siblings
15
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.
16
16 Document / PI Element Document Element Para Element Para Element Para Text One if … Text And I … Text Ready to Document classification="secret"> One if by land, two if by sea; And I on the opposite shore will be, Ready to ride and spread the alarm Ready to ride and spread the alarm Through every Middlesex, village and farm, This XML document can be represented as a tree, as shown below Attribute classification=secret Attribute classification=unclassified Attribute classification=confidential Attribute classification=unclassified See document.xml in the xpath folder, within the examples folder.
17
17 Execute XPath using Oxygen XML Type your XPath expression here Change this to XPath 1.0
18
18 Use XPath Builder for long XPath expressions
19
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.
20
20 Select all Para Elements /Document/Para
21
21 /Document/Para This is an absolute XPath expression
22
22 Establish a Context Node Click on this to establish it as the "context node" (any XPath expressions will be relative to it)
23
23 Relative XPath Expression In Oxygen XML click on to establish the context node and then type this in the XPath box: Para
24
24 Select all Para Elements //Para descendents
25
25 Select the first Para //Para[1]
26
26 Select the last Para //Para[last()]
27
27 Select the classification attribute of the first Para //Para[1]/@classification
28
28 Is the Document elements classification top-secret? /Document/@classification = 'top-secret'
29
29 Is the Document elements classification top-secret or secret? (/Document/@classification = 'top-secret') or (/Document/@classification='secret')
30
30 A or B A and B not(A) Logical Operators
31
31 Select all Paras with a secret classification //Para[@classification = 'secret']
32
32 Check that no Para has a top-secret classification not(//Para[@classification = 'top-secret'])
33
33 Establish a New Context Node Make the second Para the context node
34
34 Select the Following Siblings following-sibling::*
35
35 Select the First Following Sibling following-sibling::*[1]
36
36 Add Another Element Add this element after the last Para
37
37 Select the Following Para Siblings following-sibling::Para
38
38 Select all Following Siblings following-sibling::*
39
39 Select all Preceding Siblings preceding-sibling::*
40
40 Make Document the Context Click on Document to make it the context node.
41
41 Equivalent! Para[1] child::Para[1]
42
42 Make Para[2] the context Establish this as the context node.
43
43 Get parent element's classification../@classification
44
44 Equivalent!../@classification parent::*/@classification
45
45 Axis following-sibling preceding-sibling child parent ancestor descendent self
46
46 Count the number of Para elements count(//Para)
47
47 Count the number of Para elements with secret classification count(//Para[@classification = 'secret'])
48
48 Does the first Para element contain the string SCRIPT? contains(//Para[1], 'SCRIPT')
49
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
50
50 Count the number of nodes containing the string SCRIPT count(//node()[contains(., 'SCRIPT')])
51
51 Select the first 20 characters of the first Para substring(//Para[1], 1, 20)
52
52 What's the length of the content of the first Para? string-length(//Para[1])
53
53 translate(/Document/@classification, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') Convert Documents classification to lowercase
54
54 Add a new element Add this element and establish Document as the context node.
55
55 Multiply Cost by 2 Cost * 2
56
56 N mod X = the remainder of dividing N by X Cost mod 2
57
57 Arithmetic Operators * mod - (leave space on either side) div +
58
58 Set this to XPath 2.0
59
59 Does Documents classification match one in Classifications.xml? /Document/@classification = doc('Classifications.xml')/Classifications/li
60
60 Do the first two Para's have the same classification? Para[1]/@classification eq Para[2]/@classification
61
61 eq means equal ne means not equal lt means less than gt means greater than le means less than or equal to ge means greater than or equal to Boolean Operators
62
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()
63
63 Two built-in functions true() false()
64
64 Cast a value to a numeric type number(Cost)
65
65 Check 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]))
66
66 The sum() function 23 5 -41 50 12 sum(//number) returns 49.0
67
67 Check that every Publisher has a string-length le 140 My Life and Times Paul McCartney 1998 1-56592-235-2 McMillin Publishing John Ghostwriter Dell Publishing Co. Richard Bach 1977 0-440-34319-4 Illusions The Adventures of a Reluctant Messiah 0-06-064831-7 The First and Last Freedom J. Krishnamurti 1954 Harper & Row
68
68 Check that every Publisher has a string-length le 140 every $i in //Publisher satisfies string-length($i) le 140
69
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.
70
70 Equivalent every $i in //Publisher satisfies string-length($i) le 140 not(//Publisher[string-length(.) gt 140])
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.