Mastering XPath: Examples & Practice with Llandwp Members XML
Learn XPath data model with structured location paths, axes, node tests, predicates, and examples in BaseX using Llandwp Members XML. Practice filtering nodes and attributes efficiently.
Mastering XPath: Examples & Practice with Llandwp Members XML
E N D
Presentation Transcript
Xpath Examples Edel Sherratt
Llandwp Sports Club Members <?xml version="1.0"?> <?xml-stylesheet type="text/xml" href="members.xsl"?> <members> … <member> …</member> <member> …</member> … </members>
XPath examples to try in BaseX with LlandwpMembers.xml • /child::members • /descendant::member • /descendant::member[@number="17"] • /descendant::sport[string()="soccer"] • /descendant::sport[string()="soccer"]/ancestor::member • /descendant::com_role/ancestor::member • /descendant::sport[string()="squash"]/ancestor::member/name
Structure of an XPath location path • /step/step/step … • step/step … • Each step in the location path has three parts • axis • nodetest • predicates • axis::nodetest[predicate][predicate]…
What each step means • Starting from a context (you are here) • Specify an axis: parent, child, ancestor, descendant, attribute … • Filter the axis to get the kind of nodes you want (e.g., name an element) • Then filter again using expressions to get the precise nodes you want • do the same until all the steps are complete
More examples to try in BaseX with LlandwpMembers.xml • /descendant::renewal_due • /descendant::renewal_due/ancestor::member • /descendant::sport[string()="soccer"]/ancestor::member • /descendant::sport[string()="soccer"]/ancestor::member/child::name • /descendant::team[string()="Soccer A"]/ancestor::member/child::name • /descendant::member/attribute::number
More about node tests • A name returns nodes with that name • node() selects all nodes • processing-instruction() returns processing instructions • text() selects character data nodes • comment() selects comment nodes
Examples to try with LlandwpMembers.xml • /child::processing-instruction() • /child::members • /child::node() • /descendant::member • /descendant::member/descendant::text() • /descendant::sport • /descendant::sport/child::text()
Some shortcuts • child is the default axis, so /members is equivalent to /child::members • // is shorthand for /descendant-or-self::node(), so //name is equivalent to /descendant-or-self::node()/name • . is shorthand for self::node() • .. is shorthand for parent::node()
More queries to try • //sport • //sport/.. • //sport/../.. • //sport/../../name
And yet more … • /processing-instruction() • /members • /node() • //member//text() • //name • //name/text()
And more • //renewal_due • //renewal_due/ancestor::member • //sport[string()="soccer"]/ancestor::member • //sport[string()="soccer"]/ancestor::member/name • //team[string()="Soccer A"]/ancestor::member/name
Wildcards in node tests • node() matches any kind of node • * matches any element • @* matches any attribute
Final set of examples • //team[string()="Soccer A"]/ancestor::member/@number • //team[string()="Soccer A"]/ancestor::member/@* • /* • /node() • //@*