80 likes | 184 Views
Using Long Names for Xpath Expressions. Roger L. Costello XML Technologies. Problem: iterate through ancestors. Suppose that in your stylesheet you are at an arbitrary node in the XML tree. How would you output the name of all of its ancestor nodes?. Will this work:
E N D
Using Long Names for Xpath Expressions Roger L. Costello XML Technologies
Problem: iterate through ancestors • Suppose that in your stylesheet you are at an arbitrary node in the XML tree. How would you output the name of all of its ancestor nodes? Will this work: <xsl:value-of select="name(..)"/> <xsl:value-of select="name(../..)"/> <xsl:value-of select="name(../../..)"/> How many of these should we do? We don't know since we are at an arbitrary node!
Problem: get the previous sibling and following sibling • Suppose that in your stylesheet you are at an arbitrary node in the XML tree. How would you output the name of its previous sibling and following sibling?
Long Notation • Thus far we have used the abbreviated notation for expressing a path to a node. As we see there are some things for which the abbreviated notation is inadequate • The long notation gives you the ability to express things that can't be expressed using the short notation
Document / PI <?xml version=“1.0”?> Element FitnessCenter Element Member ancestors Element Phone Element Name Element FavoriteColor Element Phone preceding sibling following siblings Text 555-4321 Text Jeff Text lightgrey Text 555-1234 If we are currently at the Member's first phone element then: we can select all ancestors by: all preceding siblings by: all following siblings by: ancestor::* preceding-sibling::* following-sibling::* Which yields: Which yields: Which yields: Member Name Phone FitnessCenter FavoriteColor
Document / PI <?xml version=“1.0”?> Element FitnessCenter descendants Element Member (the other Member elements) Element Phone Element Name Element FavoriteColor Element Phone Text 555-4321 Text Jeff Text lightgrey Text 555-1234 If we are currently at the FitnessCenter element then: we can select all descendants by: descendant::* Which yields: Member Name Phone, etc (see axis-example01)
Axis • ancestor, preceding-sibling, following-sibling, and descendent each provide a different way of navigating the XML tree. • They are each called an axis
List of Axis • ancestor: selects all ancestors • ancestor-or-self: selects the current node plus all its ancestors • attribute: selects all the attributes • child: selects all the children • descendant: selects all the descendants • descendant-or-self: selects the current node plus all its descendants • following: selects everything in the document that follows the current node • following-sibling: selects the siblings that follow • namespace: selects all the namespaces that are in scope • parent: selects the parent • preceding: selects everything in the document that precedes the current node • preceding-sibling: selects the siblings that precede • self: selects the current node (see axis-example02)