开发者

XPath: what can be selected?

Am I correct in thinking an XPath expression can only point to/select either an eleme开发者_运维问答nt or an element attribute?


According to the XPath documentation at W3C:

An expression is evaluated to yield an object, which has one of the following four basic types:

  • node-set (an unordered collection of nodes without duplicates)
  • boolean (true or false)
  • number (a floating-point number)
  • string (a sequence of UCS characters)

The nodes are elements of the document tree, so they can include element nodes, attribute nodes and text nodes. And note that an XPath expression can select a number of nodes - i.e. a number of elements - not just a single one.

Some examples:

  • //monkey - will select all <monkey> elements in the document, a node-set
  • count(//monkey) - will return the count of all <monkey> elements, a number
  • contains("mouse", "cat") - will return false, a boolean
  • (//monkey)[0]/text() - will return the text from the first <monkey> element, a string


Since you're free to use set operators, you can select pretty much anything.

//x | //@x

should give you a list of all x-elements and all x-attributes, if I recall the syntax correctly. (pipe is the union operator)


The currently accepted answer by Dave Webb is only partially correct.

All seven types of nodes can be selected:

  • Root (document node), like in "/"
  • Elements, like in: "/*"
  • Attributes, like in: "//@*"
  • Text nodes, like in: "//text()"
  • Comment nodes, like in: "//comment()"
  • Processing instruction nodes, like in: "//processing-instruction()"
  • Namespace nodes, like in: "//namespace::*"

Also, it is good to know that depending on the host of a given XPath engine, the selected node-set may contain nodes from more than one document. For example, if the host is XSLT, the following XPath expression:

"document($uri1)//* | document($uri2)//*"

selects the union of all elements in the document identified by $uri1 and all elements in the document identified by $uri2.

The order of the nodes in the selected node-set is "document order" (as in depth-first), but this applies only for nodes belonging to the same document. In case nodes belonging to more than one document are selected, the ordering between nodes that belong to different documents is not defined (implementation-dependent).

Finally, in XPath 2.0, the selection may also be a sequence of items of any type (possibly mixed), (and having a predefined order) and there is potentially unlimited set of item types (if user-defined types are used), including all built-in XML Schema types.

In XPath 2.1 (a W3C working draft expected to be published as we speak), there will be also a "function-item" type, as functions will become first-class types of the language and can be passed as parameters, returned by a function, stored in a sequence or dynamically created.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜