|

CLaRK System Online User Manual
XML Path Language (XPath)
Back to Contents
XPath is a language for addressing parts of an XML document.
This document is based on the W3C Recommendation 16 November 1999 (http://www.w3.org/TR/1999/REC-xpath-19991116). It describes the XPath language according to the implementation used in the Clark System. The implementation covers almost the whole language. Because of the general purpose of XPath, in the implementation there are some insignificant exclusions, which are not needed for the system. On the other hand, there are new things which will be described in this document. The implementation also covers an abbreviated syntax.
1 Introduction
XPath is the result of an effort to provide a common syntax and semantics for functionality shared between
XSL Transformations [XSLT] and XPointer [XPointer]. The primary purpose of XPath is to address parts of an
XML [XML] document. In support of this primary purpose, it also provides basic
facilities for manipulation of strings, numbers and booleans. XPath operates on the abstract, logical structure of
an XML document, rather than its surface syntax. XPath gets its name from its use of a path notation as in URLs
for navigating through the hierarchical structure of an XML document.
In addition to its use for addressing, XPath is also designed so that it has a natural subset that can be used for matching (testing whether or not a node matches a pattern); this use of XPath is described in XSLT.
XPath models an XML document as a tree of nodes. There are different types of nodes, including element nodes, attribute nodes and text nodes. XPath defines a way to compute a string-value for each type of node. Some types of nodes also have names.
The primary syntactic construct in XPath is the expression. 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)
Expression evaluation occurs with respect to a context. The context consists of:
- a node (the context node)
- a pair of non-zero positive integers (the context position and the context size)
- a function library
The context position is always less than or equal to the context size.
The function library consists of a mapping from function names to functions. Each function takes zero or more arguments and returns a single result. This document defines a core function library that thel XPath implementation supports. For a function in the core function library, arguments and result are of the four basic types.
The context node, context position, and context size used to evaluate a subexpression are sometimes different from those used to evaluate the containing expression. Several kinds of expressions change the context node; only predicates change the context position and context size. When the evaluation of a kind of expression is described, it will always be explicitly stated if the context node, context position, and context size change for the evaluation of subexpressions; if nothing is said about the context node, context position, and context size, they remain unchanged for the evaluation of subexpressions of that kind of expression.
The grammar specified in this section applies to the attribute value after XML 1.0 normalization. So, for example, if the grammar uses the character <, this must not appear in the XML source as < but must be quoted according to XML 1.0 rules by, for example, entering it as <. Within expressions, literal strings are delimited by single or double quotation marks, which are also used to delimit XML attributes. To avoid a quotation mark in an expression being interpreted by the XML processor as terminating the attribute value the quotation mark can be entered as a character reference (" or '). Alternatively, the expression can use single quotation marks if the XML attribute is delimited with double quotation marks or vice-versa.
One important kind of expression is a location path. A location path selects a set of nodes relative to the context node. The result of evaluating an expression that is a location path is the node-set containing the nodes selected by the location path. Location paths can recursively contain expressions that are used to filter sets of nodes.
2 Location Paths
Although location paths are not the most general grammatical construct in the language, they are the most important construct and will therefore be described first.
Every location path can be expressed using a straightforward but rather verbose syntax. There are also a number of syntactic abbreviations that allow common cases to be expressed concisely. This section will explain the semantics of location paths using the unabbreviated syntax. The abbreviated syntax will then be explained by showing how it expands into the unabbreviated syntax .
Here are some examples of location paths using the unabbreviated syntax:
- child::para
selects the para element children of the context node
- child::text()
selects all text node children of the context node
- child::node() or child::*
selects all the children of the context node, whatever their node type
- attribute::name
selects the name attribute of the context node
- attribute::*
selects all the attributes of the context node
- descendant::para
selects the para element descendants of the context node
- ancestor::div
selects all div ancestors of the context node
- ancestor-or-self::div
selects the div ancestors of the context node and, if the context node is a div element, the context node as well
- descendant-or-self::para
selects the para element descendants of the context node and, if the context node is a para element, the context node as well
- self::para
selects the context node if it is a para element, and otherwise selects nothing
- child::chapter/descendant::para
selects the para element descendants of the chapter element children of the context node
- child::*/child::para
selects all para grandchildren of the context node
- /
selects the document root (which is always the parent of the document element)
- /descendant::para
selects all the para elements in the same document as the context node
- /descendant::olist/child::item
selects all the item elements that have an olist parent and that are in the same document as the context node
- child::para[position()=1]
selects the first para child of the context node
- child::para[position()=last()]
selects the last para child of the context node
- child::para[position()=last()-1]
selects the last but one para child of the context node
- child::para[position()>1]
selects all the para children of the context node other than the first para child of the context node
- following-sibling::chapter[position()=1]
selects the next chapter sibling of the context node
- preceding-sibling::chapter[position()=1]
selects the previous chapter sibling of the context node
- /descendant::figure[position()=42]
selects the forty-second figure element in the document
- /child::doc/child::chapter[position()=5]/child::section[position()=2]
selects the second section of the fifth chapter of the doc document element
- child::para[attribute::type="warning"]
selects all para children of the context node that have a type attribute with value warning
- child::para[attribute::type='warning'][position()=5]
selects the fifth para child of the context node that has a type attribute with value warning
- child::para[position()=5][attribute::type="warning"]
selects the fifth para child of the context node if that child has a type attribute with value warning
- child::chapter[child::title='Introduction']
selects the chapter children of the context node that have one or more title children with string-value equal to Introduction
- child::chapter[child::title]
selects the chapter children of the context node that have one or more title children
- child::*[self::chapter or self::appendix]
selects the chapter and appendix children of the context node
- child::*[self::chapter or self::appendix][position()=last()]
selects the last chapter or appendix child of the context node
There are two kinds of location path: relative location paths and absolute location paths.
A relative location path consists of a sequence of one or more location steps separated by /. The steps in a relative location path are composed together from left to right. Each step in turn selects a set of nodes relative to a context node. An initial sequence of steps is composed together with a following step as follows. The initial sequence of steps selects a set of nodes relative to a context node. Each node in that set is used as a context node for the following step. The sets of nodes identified by that step are unioned together. The set of nodes identified by the composition of the steps is this union. For example, child::div/child::para selects the para element children of the div element children of the context node, or, in other words, the para element grandchildren that have div parents.
An absolute location path consists of / followed by a relative location path. The / selects the root node of the document as a context node for the next relative location path. A / itself does not select the root node. This can be done by /self::*
2.1 Location Steps
A location step has three parts:
- an axis, which specifies the tree relationship between the nodes selected by the location step and the context node,
- a node test, which specifies the node type or the name of the nodes selected by the location step, and
- zero or more predicates, which use arbitrary expressions to further refine the set of nodes selected by the location step.
The syntax for a location step is the axis name and node test separated by a double colon, followed by zero or more expressions each in square brackets. For example, in child::para[position()=1], child is the name of the axis, para is the node test and [position()=1] is a predicate.
The node-set selected by the location step is the node-set that results from generating an initial node-set from the axis and node-test, and then filtering that node-set by each of the predicates in turn.
The initial node-set consists of the nodes having the relationship to the context node specified by the axis, and having the node type or name specified by the node test. For example, a location step descendant::para selects the para element descendants of the context node: descendant specifies that each node in the initial node-set must be a descendant of the context; para specifies that each node in the initial node-set must be an element named para. The available axes are described in Axes. The available node tests are described in Node Tests.
The initial node-set is filtered by the first predicate to generate a new node-set; this new node-set is then filtered using the second predicate, and so on. The final node-set is the node-set selected by the location step. The axis affects how the expression in each predicate is evaluated and so the semantics of a predicate is defined with respect to an axis.
2.2 Axes
The following axes are available:
- the
child axis contains the children of the context node
- the
descendant axis contains the descendants of the context node; a descendant is a child or a child of a child and so on; thus the descendant axis never contains attribute nodes
- the
parent axis contains the parent of the context node, if there is one
- the
ancestor axis contains the ancestors of the context node; the ancestors of the context node consist of the parent of context node and the parent's parent and so on; thus, the ancestor axis will always include the root node, unless the context node is the root node
- the
following-sibling axis contains all the following siblings of the context node; if the context node is an attribute node node, the following-sibling axis is empty
- the
preceding-sibling axis contains all the preceding siblings of the context node; if the context node is an attribute node, the preceding-sibling axis is empty
- the
following axis contains all nodes in the same document as the context node that are after the context node in document order, excluding any descendants and excluding attribute nodes.
- the
preceding axis contains all nodes in the same document as the context node that are before the context node in document order, excluding any ancestors and excluding attribute nodes.
- the
attribute axis contains the attributes of the context node; the axis will be empty unless the context node is an element
- the
self axis contains just the context node itself
- the
descendant-or-self axis contains the context node and the descendants of the context node
- the
ancestor-or-self axis contains the context node and the ancestors of the context node; thus, the ancestor axis will always include the root node
NOTE: The ancestor, descendant, following, preceding and self axes partition a document (ignoring attributes): they do not overlap and together they contain all the nodes in the document.
2.3 Node Tests
The node tests are divided into two categories: node type tests and node name tests.
The node type tests are a finite number. They are:
From the initial node-set preserves only the text nodes.
From the initial node-set preserves only the text nodes, which contain the <text> as a substring of their text content. For example child::text(play) will return all text nodes which are children of the context node and contain the token play in them.
- This node test do not filter the initial node-set. This is used when all the nodes selected from the axis are needed for further evaluation. For short * can be used as instead, i.e. child::* is the same as child::node().
- From the initial node-set only the element nodes remain.
- From the initial node-set only the attribute nodes remain. It is possible the initianl nodes to contain not only attributes.
- attribute(<attributeName>)
Filters only for element nodes which have an attribute named <attributeName>. Example child::attribute(id) will take only the element nodes, children of the context node, which have an attribute id.
- attribute(<attributeName> = <arrtibuteValue>)
- This node test is almost the same as the attribute(<attributeName>) node test, but in addition it has also a restriction on the value of the attribute. Example: child::attribute(id=243) will take only these element nodes which have an attribute id set to value 243.
- From the initial node-set only comment nodes remain.
- From the initial node-set only processing-instruction nodes remain.
The name node tests are used to filter the initial node-set for element nodes with a given name. All other non-element nodes and element nodes with other name are removed from the set. Example child::para takes only the para element nodes, children of the context node.
2.4 Predicates
An axis is either a forward axis or a reverse axis. An axis that only ever contains the context node or nodes that are after the context node in document order is a forward axis. An axis that only ever contains the context node or nodes that are before the context node in document order is a reverse axis. Thus, the ancestor, ancestor-or-self, preceding, and preceding-sibling axes are reverse axes; all other axes are forward axes. Since the self axis always contains at most one node, it makes no difference whether it is a forward or reverse axis. The proximity position of a member of a node-set with respect to an axis is defined to be the position of the node in the node-set ordered in document order if the axis is a forward axis and ordered in reverse document order if the axis is a reverse axis. The first position is 1.
A predicate filters a node-set with respect to an axis to produce a new node-set. For each node in the node-set to be filtered, the predicate is evaluated with that node as the context node, with the number of nodes in the node-set as the context size, and with the proximity position of the node in the node-set with respect to the axis as the context position; if the predicate evaluates to true for that node, the node is included in the new node-set; otherwise, it is not included.
A predicate is evaluated as an expression and the result is converted to a boolean. If the result is a number, the result will be converted to true if the number is equal to the context position and will be converted to false otherwise; if the result is not a number, then the result will be converted as if by a call to the boolean() function. Thus a location path para[3] is equivalent to para[position()=3]. In other words if the context node has 4 child nodes para, then only for the third para child the predicate [3] (or [position()=3]) will be true. So the new node-set will contain only the third para child.
2.5 Abbreviated Syntax
Here are some examples of location paths using abbreviated syntax:
- para
selects the para element children of the context node
- *
selects all children of the context node
- text()
selects all text node children of the context node
- @name
selects the name attribute of the context node
- @*
selects all the attributes of the context node
- para[1]
selects the first para child of the context node
- para[last()]
selects the last para child of the context node
- */para
selects all para grandchildren of the context node
- /doc/chapter[5]/section[2]
selects the second section of the fifth chapter of the doc
- chapter//para
selects the para element descendants of the chapter element children of the context node
- //para
selects all the para descendants of the document root and thus selects all para elements in the same document as the context node
- //olist/item
selects all the item elements in the same document as the context node that have an olist parent
- .
selects the context node
- .//para
selects the para element descendants of the context node
- ..
selects the parent of the context node
- ../@lang
selects the lang attribute of the parent of the context node
- para[@type="warning"]
selects all para children of the context node that have a type attribute with value warning
- para[@type="warning"][5]
selects the fifth para child of the context node that has a type attribute with value warning
- para[5][@type="warning"]
selects the fifth para child of the context node if that child has a type attribute with value warning
- chapter[title="Introduction"]
selects the chapter children of the context node that have one or more title children with string-value equal to Introduction
- chapter[title]
selects the chapter children of the context node that have one or more title children
- employee[@secretary and @assistant]
selects all the employee children of the context node that have both a secretary attribute and an assistant attribute
The most important abbreviation is that child:: can be omitted from a location step. In effect, child is the default axis. For example, a location path div/para is short for child::div/child::para.
There is also an abbreviation for attributes: attribute:: can be abbreviated to @. For example, a location path para[@type="warning"] is short for child::para[attribute::type="warning"] and so selects para children with a type attribute with value equal to warning.
// is short for /descendant-or-self::node()/. For example, //para is short for /descendant-or-self::node()/child::para and so will select any para element in the document; div//para is short for div/descendant-or-self::node()/child::para and so will select all para descendants of div children.
NOTE: The location path //para[1] does not mean the same as the location path /descendant::para[1]. The latter selects the first descendant para element; the former selects all descendant para elements that are the first para children of their parents.
A location step of . is short for self::node(). This is particularly useful in conjunction with //. For example, the location path .//para is short for self::node()/descendant-or-self::node()/child::para and so will select all para descendant elements of the context node.
Similarly, a location step of .. is short for parent::node(). For example, ../title is short for parent::node()/child::title and so will select the title children of the parent of the context node.
3 Expressions
3.1 Function Calls
A function call is evaluated by evaluating each of the arguments, converting each argument to the type required by the function, and finally calling the function, passing it the converted arguments. It is an error if the number of arguments is wrong or if an argument cannot be converted to the required type.
An argument is converted to type string as if by calling the string() function. An argument is converted to type number as if by calling the number() function. An argument is converted to type boolean as if by calling the boolean() function. An argument that is not of type node-set cannot be converted to a node-set.
Examples:
child::para[count(child::*) > 3] returns all para child elements of the context node which have more then 3 children. The evaluation will be in the following sequence: getting all child nodes of the context; applying the name node test and keeping only the para elements; for each para element as a context evaluating the count() function. First evaluating the location path child::* with a context each of the para elements. The location path every time returns a node-sets (different in general) which is passed as an agrument for the count() function. The function returns a number which later will be used for further evaluation of the predicate.
3.2 Node-sets
A location path can be used as an expression. The expression returns the set of nodes selected by the path.
The | operator computes the union of its operands, which must be node-sets.
Example: /descendant-or-self::para | /descendant-or-self::head will return an union of all para and head element nodes in a document. The two location paths are evaluated independantly and the two results are unified.
3.3 Booleans
An object of type boolean can have one of two values, true and false.
An or expression is evaluated by evaluating each operand and converting its value to a boolean as if by a call to the boolean() function. The result is true if either value is true and false otherwise.
An and expression is evaluated by evaluating each operand and converting its value to a boolean as if by a call to the boolean() function. The result is true if both values are true and false otherwise.
An equality(A = B) or a relational(A < B) expression is evaluated by comparing the objects that result from evaluating the two operands. Comparison of the resulting objects is defined in the following three paragraphs. First, comparisons that involve node-sets are defined in terms of comparisons that do not involve node-sets; this is defined uniformly for =, !=, <=, <, >= and >. Second, comparisons that do not involve node-sets are defined for = and !=. Third, comparisons that do not involve node-sets are defined for <=, <, >= and >.
If both objects to be compared are node-sets, then the comparison will be true if and only if there is a node in the first node-set and a node in the second node-set such that the result of performing the comparison on the string-values of the two nodes is true. If one object to be compared is a node-set and the other is a number, then the comparison will be true if and only if there is a node in the node-set such that the result of performing the comparison on the number to be compared and on the result of converting the string-value of that node to a number using the number() function is true. If one object to be compared is a node-set and the other is a string, then the comparison will be true if and only if there is a node in the node-set such that the result of performing the comparison on the string-value of the node and the other string is true. If one object to be compared is a node-set and the other is a boolean, then the comparison will be true if and only if the result of performing the comparison on the boolean and on the result of converting the node-set to a boolean using the boolean() function is true.
When neither object to be compared is a node-set and the operator is = or !=, then the objects are compared by converting them to a common type as follows and then comparing them. If at least one object to be compared is a boolean, then each object to be compared is converted to a boolean as if by applying the boolean() function. Otherwise, if at least one object to be compared is a number, then each object to be compared is converted to a number as if by applying the boolean() function. Otherwise, both objects to be compared are converted to strings as if by applying the string() function. The = comparison will be true if and only if the objects are equal; the != comparison will be true if and only if the objects are not equal. Two booleans are equal if either both are true or both are false. Two strings are equal if and only if they consist of the same sequence of UCS characters.
When neither object to be compared is a node-set and the operator is <=, <, >= or >, then the objects are compared by converting both objects to numbers and comparing the numbers. The < comparison will be true if and only if the first number is less than the second number. The <= comparison will be true if and only if the first number is less than or equal to the second number. The > comparison will be true if and only if the first number is greater than the second number. The >= comparison will be true if and only if the first number is greater than or equal to the second number.
3.4 Numbers
The numeric operators convert their operands to numbers as if by calling the number() function.
The + operator performs addition.
The - operator performs subtraction.
NOTE: Since XML allows - in names, the - operator typically needs to be preceded by whitespace. For example, foo-bar evaluates to a node-set containing the child elements named foo-bar; foo - bar evaluates to the difference of the result of converting the string-value of the first foo child element to a number and the result of converting the string-value of the first bar child to a number.
The div operator performs floating-point division.
The mod operator returns the remainder from a truncating division. For example,
- 5 mod 2
returns 1
- 5 mod -2
returns 1
- -5 mod 2
returns -1
- -5 mod -2
returns -1
4 Core Function Library
This section describes functions that the XPath implementation include in the function library that is used to evaluate expressions.
Each function in the function library is specified using a function prototype, which gives the return type, the name of the function, and the type of the arguments. If an argument type is followed by a question mark, then the argument is optional; otherwise, the argument is required.
4.1 Node Set Functions
Function: number last()
The
last function returns a number equal to the context size from the expression evaluation context.
Function: number position()
The position function returns a number equal to the context position from the expression evaluation context.
Function: number count(node-set)
The count function returns the number of nodes in the argument node-set.
Function: string name(node-set?)
The name function returns a string containing a name of the node in the argument node-set that is first in document order. If the argument node-set is empty or the first node has no name, an empty string is returned. If the argument it omitted, it defaults to a node-set with the context node as its only member.
4.2 String Functions
Function: string string(object?)
The string function converts an object to a string as follows:
- A node-set is converted to a string by returning the string-value of the node in the node-set that is first in document order. If the node-set is empty, an empty string is returned.
- A number is converted to a string as follows
- NaN is converted to the string
NaN
- positive zero is converted to the string
0
- negative zero is converted to the string
0
- positive infinity is converted to the string
Infinity
- negative infinity is converted to the string
-Infinity
- if the number is an integer, the number is represented in decimal form as a number with no decimal point and no leading zeros, preceded by a minus sign (
-) if the number is negative
- otherwise, the number is represented in decimal form as a number including a decimal point with at least one digit before the decimal point and at least one digit after the decimal point, preceded by a minus sign (
-) if the number is negative;
- The boolean false value is converted to the string
false. The boolean true value is converted to the string true.
- An object of a type other than the four basic types will cause an exception (error).
If the argument is omitted, it defaults to a node-set with the context node as its only member.
Function: string concat(string, string, string*)
The concat function returns the concatenation of its arguments.
Function: boolean starts-with(string, string)
The starts-with function returns true if the first argument string starts with the second argument string, and otherwise returns false.
Function: boolean contains(string, string)
The contains function returns true if the first argument string contains the second argument string, and otherwise returns false.
Function: string substring-before(string, string)
The substring-before function returns the substring of the first argument string that precedes the first occurrence of the second argument string in the first argument string, or the empty string if the first argument string does not contain the second argument string. For example, substring-before("1999/04/01","/") returns 1999.
Function: string substring-after(string, string)
The substring-after function returns the substring of the first argument string that follows the first occurrence of the second argument string in the first argument string, or the empty string if the first argument string does not contain the second argument string. For example, substring-after("1999/04/01","/") returns 04/01, and substring-after("1999/04/01","19") returns 99/04/01.
Function: string substring(string, number, number?)
The substring function returns the substring of the first argument starting at the position specified in the second argument with length specified in the third argument. For example, substring("12345",2,3) returns "234". If the third argument is not specified, it returns the substring starting at the position specified in the second argument and continuing to the end of the string. For example, substring("12345",2) returns "2345".
More precisely, each character in the string is considered to have a numeric position: the position of the first character is 1, the position of the second character is 2 and so on.
The returned substring contains those characters for which the position of the character is greater than or equal to the rounded value of the second argument and, if the third argument is specified, less than the sum of the rounded value of the second argument and the rounded value of the third argument; rounding is done as if by a call to the round function. The following examples illustrate various unusual cases:
- substring("12345", 1.5, 2.6)
returns "234"
- substring("12345", 0, 3)
returns "12"
- substring("12345", 0 div 0, 3)
returns ""
- substring("12345", 1, 0 div 0)
returns ""
- substring("12345", -42, 1 div 0)
returns "12345"
- substring("12345", -1 div 0, 1 div 0)
returns ""
Function: number string-length(string?)
The string-length returns the number of characters in the string. If the argument is omitted, it defaults to the context node converted to a string, in other words the string-value of the context node.
Function: string normalize-space(string?)
The normalize-space function returns the argument string with whitespace normalized by stripping leading and trailing whitespace and replacing sequences of whitespace characters by a single space. Whitespace characters are: SPACEs, TABs, new lines. If the argument is omitted, it defaults to the context node converted to a string, in other words the string-value of the context node.
Function: string translate(string, string, string)
The translate function returns the first argument string with occurrences of characters in the second argument string replaced by the character at the corresponding position in the third argument string. For example, translate("bar","abc","ABC") returns the string BAr. If there is a character in the second argument string with no character at a corresponding position in the third argument string (because the second argument string is longer than the third argument string), then occurrences of that character in the first argument string are removed. For example, translate("--aaa--","abc-","ABC") returns "AAA". If a character occurs more than once in the second argument string, then the first occurrence determines the replacement character. If the third argument string is longer than the second argument string, then excess characters are ignored.
4.3 Boolean Functions
Function: boolean boolean(object)
The boolean function converts its argument to a boolean as follows:
- a number is true if and only if it is neither positive or negative zero nor NaN
- a node-set is true if and only if it is non-empty
- a string is true if and only if its length is non-zero
- An object of a type other than the four basic types will cause an exception (error).
Function: boolean not(boolean)
The not function returns true if its argument is false, and false otherwise.
Function: boolean true()
The true function returns true.
Function: boolean false()
The false function returns false.
4.4 Number Functions
Function: number number(object?)
The number function converts its argument to a number as follows:
- a string that consists of optional whitespace followed by an optional minus sign followed by a number followed by whitespace is converted to the number that is nearest to the mathematical value represented by the string; any other string is converted to NaN
- boolean true is converted to 1; boolean false is converted to 0
- a node-set is first converted to a string as if by a call to the string function and then converted in the same way as a string argument
- an object of a type other than the four basic types will cause an exception (error).
If the argument is omitted, it defaults to a node-set with the context node as its only member.
Function: number sum(node-set)
The sum function returns the sum, for each node in the argument node-set, of the result of converting the string-values of the node to a number.
Function: number floor(number)
The floor function returns the largest (closest to positive infinity) number that is not greater than the argument and that is an integer.
Function: number ceiling(number)
The ceiling function returns the smallest (closest to negative infinity) number that is not less than the argument and that is an integer.
Function: number round(number)
The round function returns the number that is closest to the argument and that is an integer. If there are two such numbers, then the one that is closest to positive infinity is returned. If the argument is NaN, then NaN is returned. If the argument is positive infinity, then positive infinity is returned. If the argument is negative infinity, then negative infinity is returned. If the argument is positive zero, then positive zero is returned. If the argument is negative zero, then negative zero is returned. If the argument is less than zero, but greater than or equal to -0.5, then negative zero is returned.
5 Data Model
XPath operates on an XML document as a tree. This section describes how XPath models an XML document as a tree. This model is conceptual only and does not mandate any particular implementation.
The tree contains nodes. There are several supported types of node:
- element nodes
- text nodes
- attribute nodes
- processing instruction nodes
- comment nodes
For every type of node, there is a way of determining a string-value for a node of that type. For some types of node, the string-value is part of the node; for other types of node, the string-value is computed from the string-value of descendant nodes.
For element nodes, the string-value is a concatenation of the string-values of their child nodes in document order. If an element node has no child nodes then an empty string is returned.
For attribute, comment, text and processing-instruction nodes the string-value is the text content of each of the nodes.
There is an ordering, document order, defined on all the nodes in the document corresponding to the order in which the first character of the XML representation of each node occurs in the XML representation of the document after expansion of general entities. Thus, the root node will be the first node. Element nodes occur before their children. Thus, document order orders element nodes in order of the occurrence of their start-tag in the XML (after expansion of entities). Reverse document order is the reverse of document order.
Element nodes have an ordered list of child nodes. Nodes never share children: if one node is not the same node as another node, then none of the children of the one node will be the same node as any of the children of another node. Every node other than the root node has exactly one parent, which is an element node. The descendants of a node are the children of the node and the descendants of the children of the node.
5.1 Element Nodes
There is an element node for every element in the document. An element node has a name.
The children of an element node are the element nodes, comment nodes, processing instruction nodes and text nodes for its content.
5.2 Attribute Nodes
Each element node has an associated set of attribute nodes; the element is the parent of each of these attribute nodes; however, an attribute node is not a child of its parent element.
Elements never share attribute nodes: if one element node is not the same node as another element node, then none of the attribute nodes of the one element node will be the same node as the attribute nodes of another element node.
5.3 Processing Instruction Nodes
There is a processing instruction node for every processing instruction, except for any processing instruction that occurs within the document type declaration.
5.4 Comment Nodes
There is a comment node for every comment, except for any comment that occurs within the document type declaration.
5.5 Text Nodes
Character data is grouped into text nodes. As much character data as possible is grouped into each text node: a text node never has an immediately following or preceding sibling that is a text node. The string-value of a text node is the character data. A text node always has at least one character of data.
Each character within a CDATA section is treated as character data. Thus, <![CDATA[<]]> in the source document will treated the same as <. Both will result in a single < character in a text node in the tree. Thus, a CDATA section is treated as if the <![CDATA[ and ]]> were removed and every occurrence of < and & were replaced by < and & respectively.
A text node does not have a name.
A References
A.1 Normative References
- XML
- World Wide Web Consortium. Extensible Markup Language (XML) 1.0. W3C Recommendation. See http://www.w3.org/TR/1998/REC-xml-19980210
- XML Names
- World Wide Web Consortium. Namespaces in XML. W3C Recommendation. See http://www.w3.org/TR/REC-xml-names
A.2 Other References
- Character Model
- World Wide Web Consortium. Character Model for the World Wide Web. W3C Working Draft. See http://www.w3.org/TR/WD-charmod
- DOM
- World Wide Web Consortium. Document Object Model (DOM) Level 1 Specification. W3C Recommendation. See http://www.w3.org/TR/REC-DOM-Level-1
- TEI
- C.M. Sperberg-McQueen, L. Burnard Guidelines for Electronic Text Encoding and Interchange. See http://etext.virginia.edu/TEI.html.
- Unicode
- Unicode Consortium. The Unicode Standard. See http://www.unicode.org/unicode/standard/standard.html.
Back to Contents
|