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 documentThe way XPath describes paths to elements is similar to the way an operating system describes paths to filesXPath is almost a small programming language; it has functions, tests, and expressionsXPath is a W3C standardXPath 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 chaptersThe two chapters are the children of book, and the section is the child of the second chapterThe two chapters of the book are siblings (they have the same parent)library, book, and the second chapter are the ancestors of the sectionThe 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 element5SlashesA path that begins with a / represents an absolute path, starting from the top of the documentExample: /email/message/header/fromNote that even an absolute path can select more than one elementA slash by itself means “the whole document”A path that does not begin with a / represents a path starting from the current elementExample: header/fromA path that begins with // can start from anywhere in the documentExample: //header/from selects every element from that is a child of an element headerThis 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 libraryExample: //chapter/section[2] selects the second section of every chapter in the XML documentExample: //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 sectionsThe function last() in brackets selects the last matching childExample: /library/book/chapter[last()]You can even do simple arithmeticExample: /library/book/chapter[last()-1]7StarsA 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 libraryExample: //book/* selects every child of every book (chapters, tableOfContents, index, etc.)Example: /*/*/*/paragraph selects every paragraph that has exactly three ancestorsExample: //* selects every element in the entire document8Attributes IYou can select attributes by themselves, or elements that have certain attributesRemember: an attribute consists of a name-value pair, for example in <chapter num="5">, the attribute is named numTo choose the attribute itself, prefix the name with @Example: @num will choose every attribute named numExample: //@* will choose every attribute, everywhere in the documentTo choose elements that have a given attribute, put the attribute name in square bracketsExample: //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 3The normalize-space() function can be used to remove leading and trailing spaces from a value before comparisonExample: //chapter[normalize-space(@num)="3"]11AxesAn 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 nodechild:: is the default, so /child::X is the same as /Xparent:: is the parent of the current nodeancestor:: is all ancestors of the current node, up to and including the rootdescendant:: 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 documentfollowing:: 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