XPath

Used to address and select some needed nodes in XSLT

Link

Nodes
element
attribute
text
namespace
processing-instruction
comment
main document node


Selecting Nodes

/ Matches the document node
node() any node
text() Matches text nodes
<xsl:template match="text()">  #text() matches text nodes
<xsl:value-of select="."/>
</xsl:template>
name of an element matches that element
.  current node
.. parent node
* Matches any element
@ matches attributes
@* any attribute



castMember/character: direct child
description//character:  character nested to any level of description


Predicates

[index or attribute]   /bookstore/book[1]  evaluates to the first matched node with that criteria
last()
position()<3         index of the matched elements in the parent's contents must be less than three
book[@lang]          book elements that have a lang attribute
book[@lang="eng"]
book[price>35.00]/title       title elements that are children of book elements that have a price element with a value greater than 35
| or


TAG XPATH="//div[@id='panel_A_1']/*[position()=2] " EXTRACT=TXT          2nd child


Shortcut Keywords

The shortcut selects:
shortcutKey::node[predicate]

AxisName Result
self current node
parent parent of the current node
ancestor all ancestors (parent, grandparent, etc.) of the current node
ancestor-or-self all ancestors of the current node or the current node itself
attribute all attributes of the current node
child all children of the current node
descendant all descendants (children, grandchildren, etc.) of the current node
descendant-or-self all descendants of the current node or the current node itself
following everything in the document after the closing tag of the current node
following-sibling all siblings after the current node
namespace all namespace nodes of the current node
preceding everything in the document that is before the start tag of the current node
preceding-sibling all siblings before the current node

Example Result
child::book Selects all book nodes that are children of the current node
attribute::lang Selects the lang attribute of the current node
child::* Selects all children of the current node
attribute::* Selects all attributes of the current node
child::text() Selects all text child nodes of the current node
child::node() Selects all child nodes of the current node
descendant::book Selects all book descendants of the current node
ancestor::book Selects all book ancestors of the current node
ancestor-or-self::book Selects all book ancestors of the current node - and the current as well if it is a book node
child::*/child::price Selects all price grandchildren of the current node




Operator Description Example Return value
| Computes two node-sets //book | //cd Returns a node-set with all book and cd elements
+ Addition 6 + 4 10
- Subtraction 6 - 4 2
* Multiplication

6 * 4

24
div Division 8 div 4 2
= Equal price=9.80 true if price is 9.80
false if price is 9.90
!= Not equal price!=9.80 true if price is 9.90
false if price is 9.80
< Less than price<9.80 true if price is 9.00
false if price is 9.80
<= Less than or equal to price<=9.80 true if price is 9.00
false if price is 9.90
> Greater than price>9.80 true if price is 9.90
false if price is 9.80
>= Greater than or equal to price>=9.80 true if price is 9.90
false if price is 9.70
or or price=9.80 or price=9.70 true if price is 9.80
false if price is 9.50
and and price>9.00 and price<9.90 true if price is 9.80
false if price is 8.50
mod Modulus (division remainder) 5 mod 2 1




















































Comments