DOC PREVIEW
Penn CIT 597 - XPath Lecture

This preview shows page 1-2-20-21 out of 21 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

XPathWhat is XPath?TerminologyPathsSlashesBrackets and last()StarsAttributes IAttributes IIValues of attributesAxesAxes (outline view)Axes (tree view)Axis examplesMore axesAbbreviations for axesArithmetic expressionsEquality testsOther boolean operatorsSome XPath functionsThe EndJan 14, 2019XPath2What is XPath?XPath is a syntax used for selecting parts of an XML documentThe way XPath describes paths to elements is similar to the way an operating system describes paths to filesXPath is almost a small programming language; it has functions, tests, and expressionsXPath is a W3C standardXPath is not itself written as XML, but is used heavily in XSLT3Terminology <library> <book> <chapter> </chapter> <chapter> <section> <paragraph/> <paragraph/> </section> </chapter> </book></library>library is the parent of book; book is the parent of the two chaptersThe two chapters are the children of book, and the section is the child of the second chapterThe two chapters of the book are siblings (they have the same parent)library, book, and the second chapter are the ancestors of the sectionThe two chapters, the section, and the two paragraphs are the descendents of the book4PathsOperating system: XPath:/ = the root directory /library = the root element (if named library )/users/dave/foo = the (one) file named foo in dave in users/library/book/chapter/section = every section element in a chapter in every book in the library. = the current directory . = the current element.. = the parent directory.. = parent of the current element/users/dave/* = all the files in /users/dave/library/book/chapter/* = all the elements in /library/book/chapterfoo = the (one) file named foo in the current directorysection = every section element that is a child of the current element5SlashesA path that begins with a / represents an absolute path, starting from the top of the documentExample: /email/message/header/fromNote that even an absolute path can select more than one elementA slash by itself means “the whole document”A path that does not begin with a / represents a path starting from the current elementExample: header/fromA path that begins with // can start from anywhere in the documentExample: //header/from selects every element from that is a child of an element headerThis can be expensive, since it involves searching the entire document6Brackets and last()A number in brackets selects a particular matching child (counting starts from 1, except in Internet Explorer)Example: /library/book[1] selects the first book of the libraryExample: //chapter/section[2] selects the second section of every chapter in the XML documentExample: //book/chapter[1]/section[2]Only matching elements are counted; for example, if a book has both sections and exercises, the latter are ignored when counting sectionsThe function last() in brackets selects the last matching childExample: /library/book/chapter[last()]You can even do simple arithmeticExample: /library/book/chapter[last()-1]7StarsA star, or asterisk, is a “wild card”—it means “all the elements at this level”Example: /library/book/chapter/* selects every child of every chapter of every book in the libraryExample: //book/* selects every child of every book (chapters, tableOfContents, index, etc.)Example: /*/*/*/paragraph selects every paragraph that has exactly three ancestorsExample: //* selects every element in the entire document8Attributes IYou can select attributes by themselves, or elements that have certain attributesRemember: an attribute consists of a name-value pair, for example in <chapter num="5">, the attribute is named numTo choose the attribute itself, prefix the name with @Example: @num will choose every attribute named numExample: //@* will choose every attribute, everywhere in the documentTo choose elements that have a given attribute, put the attribute name in square bracketsExample: //chapter[@num] will select every chapter element (anywhere in the document) that has an attribute named num9Attributes II//chapter[@num] selects every chapter element with an attribute num//chapter[not(@num)] selects every chapter element that does not have a num attribute//chapter[@*] selects every chapter element that has any attribute //chapter[not(@*)] selects every chapter element with no attributes10Values of attributes//chapter[@num='3'] selects every chapter element with an attribute num with value 3The normalize-space() function can be used to remove leading and trailing spaces from a value before comparisonExample: //chapter[normalize-space(@num)="3"]11AxesAn axis (plural axes) is a set of nodes relative to a given node; X::Y means “choose Y from the X axis”self:: is the set of current nodes (not too useful)self::node() is the current nodechild:: is the default, so /child::X is the same as /Xparent:: is the parent of the current nodeancestor:: is all ancestors of the current node, up to and including the rootdescendant:: is all descendants of the current node (Note: never contains attribute or namespace nodes)preceding:: is everything before the current node in the entire XML documentfollowing:: is everything after the current node in the entire XML document12Axes (outline view) <library> <book> <chapter/> <chapter> <section> <paragraph/> <paragraph/> </section> </chapter> <chapter/> </book> <book/></library>//chapter[2]/self::*//chapter[2]/preceding::*//chapter[2]/following::*//chapter[2]/ancestor::*//chapter[2]/descendant::*Starting from a given node, the self, preceding, following, ancestor, and descendant axes form a partition of all the nodes (if we ignore attribute and namespace nodes)13Axes (tree view)Starting from a given node, the self, ancestor, descendant , preceding, and following axes form a partition of all the nodes (if we ignore attribute and namespace nodes)paragraph[1] paragraph[2]section[1]chapter[2]chapter[1] chapter[3]book[1]book[2]libraryselfancestordescendantprecedingfollowing14Axis examples//book/descendant::* is all descendants of every book//book/descendant::section is all section descendants of every book//parent::* is every element that is a parent, i.e., is not a leaf//section/parent::* is every parent of a section


View Full Document

Penn CIT 597 - XPath Lecture

Documents in this Course
DOM

DOM

21 pages

More DOM

More DOM

11 pages

Rails

Rails

33 pages

DOM

DOM

21 pages

RELAX NG

RELAX NG

31 pages

RELAX NG

RELAX NG

31 pages

RELAX NG

RELAX NG

31 pages

RELAX NG

RELAX NG

31 pages

Rake

Rake

12 pages

Ruby

Ruby

58 pages

DOM

DOM

21 pages

Tomcat

Tomcat

16 pages

DOM

DOM

21 pages

Servlets

Servlets

29 pages

Logging

Logging

17 pages

Html

Html

27 pages

DOM

DOM

22 pages

RELAX NG

RELAX NG

30 pages

Servlets

Servlets

28 pages

XHTML

XHTML

13 pages

DOM

DOM

21 pages

DOM

DOM

21 pages

Servlets

Servlets

26 pages

More CSS

More CSS

18 pages

Servlets

Servlets

29 pages

Logging

Logging

17 pages

Load more
Download XPath Lecture
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view XPath Lecture and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view XPath Lecture 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?