Introduction to XPath Web Engineering, SS 2007 Tomáš Pitner XPath – Why? n Part of the core XML standards family q XML Path Language (XPath) Version 1.0 q W3C Recommendation 16 November 1999 n Mean to navigate in the document tree Example of an XML Document Alfred Gusenbauer SPÖ Wilhelm Molterer ÖVP XPath – The Goal n Navigation in XML documents q Similar to file addressing in a FS q Selects nodes (not just elements) in the tree q Further node processing often takes place: n XSLT, XQuery – both heavily depend on XPath n Works on Logical Structure (XML tree) n Example: an XPath expression against the last document q /staff/person[@id=‘wmo’] XPath Processing n XPath Processor/Engine q Takes XPath expression q Takes context node (position in the doc) q Accesses the source XML tree q Produces node-set/sequence as result XPath – Anatomy of an Expression n XPath Expression q Consists of steps separated by slash / q example – two steps n /staff/person n semantics: Select all person elements under staff (which is the root element). q resembles traversing directories in a FS q applicable to both elements and other nodes! q may be absolute (starting with /) or relative n i.e. evaluated without or against context nodes XPath – Predicates n XPath Expression with Predicates q each step may contain a predicate n It filters the nodes selected at the step n /staff/person[@id=‘wmo’] n Select just the person under staff with id attribute = ‘wmo’ n Predicates may be compound n //person[@id=‘wmo’ or @id=‘agu’] XPath – Functions n To select non-element nodes q comment() q text() q processing-instruction() … n To compute a value q count() q … much more n Example – select just the text of the name of the ‘agu’ person: n /staff/person[@id=‘agu’]/name/text() XPath Processing – Context, Axes n Context nodes and Axes q Context nodes n “central point” for evaluating relative expressions q Axes n “directions” leading from the context node out n node-sets relative to the context node(s) n Steps in the expression mean traversing along axes XPath Expression Examples n All against the doc on the first slide… absolute expressions n //name … selects all name elements n //person[party] … selects all person elements having (any) party child element n /staff/person[party[@url=‘xxx’]] q selects all person elements having party with url xxx n /person … does not select anything from this doc XPath Expression Examples n All against the doc on the first slide… relative expressions n Context node: first person n name … selects this person’s name element n ../staff … selects the staff element n ../* … selects also the staff element n / … also the staff n .//* … selects all descendant elements n staff/name … does not select anything from this doc XPath Axes Example (ancestor) Alfred Gusenbauer SPÖ Wilhelm Molterer ÖVP XPath Axes Example (descendant) Alfred Gusenbauer SPÖ Wilhelm Molterer ÖVP XPath Axis Example (attribute) Alfred Gusenbauer SPÖ Wilhelm Molterer ÖVP XPath Axis Example (following-sibling) Alfred Gusenbauer SPÖ Wilhelm Molterer ÖVP ABC ABC XPath Axis Example (preceding-sibling) Alfred Gusenbauer SPÖ Wilhelm Molterer ÖVP ABC ABC XPath Axis Example (preceding) Alfred Gusenbauer SPÖ Wilhelm Molterer ÖVP ABC ABC XPath Axis Example (following) Alfred Gusenbauer SPÖ Wilhelm Molterer ÖVP ABC ABC XPath Axis Example (self) Alfred Gusenbauer SPÖ Wilhelm Molterer ÖVP ABC ABC XPath Axis Example (namespace) Alfred Gusenbauer SPÖ Wilhelm Molterer ÖVP XPath Processing – Summary of axes q ancestor q ancestor-or-self q attribute q child q descendant q descendant-or-self q following q following-sibling q namespace q parent q preceding q preceding-sibling q self XPath – Long Syntax n The expression q /staff/person can be rewritten as q /child::staff/child::person n Similar for other axes q there are axes expressible in the long form ONLY q Example: preceding-sibling, ancestor-or-self… XPath – Resources n E. R. Harold – XML in a Nutshell q Chapter on XPath free on the web: http://www.oreilly.com/catalog/xmlnut/chapter/ch09.html n XPath (and XSLT) Quick Reference Card q http://www.mulberrytech.com/quickref/XSLT_1quickref-v2.pdf