XPath(5) Use axis, nodetest and predicate to define path expression

来源:互联网 发布:捷西网络官网地址 编辑:程序博客网 时间:2024/06/10 05:34

XPath1.0  uses path expressions to select nodes or node-sets in an XML document.

This is the most commonly used technique for xPath. path expression can appear in theselectattribute ortestattribute in the if statement

Syntax:

axis::nodeTest[optional predicate]

example: 
/bookstore/book[position()<3] 
Selects the first two book elements that are children of the bookstore element

axis

There are 13 types of axis:

  • child axis - the default axis
  • attribute axis - attribute::FName and @FName are equivalent.
  • ancestor axis
  • ancestor-or-self axis
  • descendant axis
  • descendant-or-self axis - descendant-or-self::c and a//c are equivalent
  • following axis
  • following-sibling axis
  • namespace axis (not used in XQuery, and deprecated in XPath 2.0)
  • parent axis - parent:: and .. are equivalent
  • preceding axis
  • preceding-sibling axis
  • self axis - self:: and . are equivalent
The following picture clarify the relationship of axis






Node Test

Specifies the initial set of nodes.

Could be 
Node Name Test
  • * or @*
  • QName (qualified name)
  • NCName (Named Characters Name) or reqularly named node
  • id(id)
Node Type Test
  • comment().
  • text().
  • processing-instruction().
  • processing-instruction("target").
  • node(). Any node except for attribute nodes and the root node.

Predicate

OPTIONAL, specifies conditions that have to be met by the node among the node set.
CAN BE NESTED axis::nodeTest[optional predicate[optional predicate]]
Can have more than one predicate axis::nodeTest[optional predicate][optional predicate]
Predicates can contain operators and functions!

In fact, predicates can be used to filter any kind of sequence.



More on xPath:
http://www.georgehernandez.com/h/xComputers/XML/XSL/XPath.asp




原创粉丝点击