W3C

XML Path Language (XPath)
Version 1.0

W3C Working Draft 9 July 1999

This version:
http://www.w3.org/1999/07/WD-xpath-19990709
http://www.w3.org/1999/07/WD-xpath-19990709.xml
http://www.w3.org/1999/07/WD-xpath-19990709.html
Latest version:
http://www.w3.org/TR/xpath
Previous version:
http://www.w3.org/TR/1999/WD-xslt-19990421
Editors:
James Clark <jjc@jclark.com>
Steve DeRose (Inso Corp. and Brown University) <Steven_DeRose@Brown.edu>

Abstract

XPath is a language for addressing parts of an XML document, designed to be used by both XSLT and XPointer.

Status of this document

This is a W3C Working Draft for review by W3C members and other interested parties. It is a draft document and may be updated, replaced, or obsoleted by other documents at any time. This is the first working draft of XPath. Most of the material in this draft was previously part of the XSLT Working Draft. This draft is joint work of the XSL Working Group and the XML Linking Working Group. The XML Linking and XSL Working Groups will not allow early implementation to constrain their ability to make changes to this specification prior to final release. It is inappropriate to use W3C Working Drafts as reference material or to cite them as other than "work in progress". A list of current W3C working drafts can be found at http://www.w3.org/TR.

Comments may be sent to www-xpath-comments@w3.org; archives of the comments are available.

Table of contents

1 Introduction
2 Location Paths
    2.1 Bases
        2.1.1 Axes
        2.1.2 Node Tests
    2.2 Predicates
    2.3 Abbreviated Syntax
3 Expressions
    3.1 Basics
    3.2 Function Calls
    3.3 Node-sets
    3.4 Booleans
    3.5 Numbers
    3.6 Strings
    3.7 Lexical Structure
4 Core Function Library
    4.1 Node Set Functions
    4.2 String Functions
    4.3 Boolean Functions
    4.4 Number Functions
5 Data Model
    5.1 Root Node
    5.2 Element Nodes
        5.2.1 Unique IDs
    5.3 Attribute Nodes
    5.4 Namespace Nodes
    5.5 Processing Instruction Nodes
    5.6 Comment Nodes
    5.7 Text Nodes

Appendices

A References
    A.1 Normative References
    A.2 Other References
B Changes from Previous XSLT Public Working Draft

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 uses a compact, non-XML syntax to facilitate use of XPath within URIs and XML attribute values. XPath operates on the abstract, logical structure of an XML document, rather than its surface syntax; it models an XML document as a tree of nodes as described in [5 Data Model]. XPath gets its name from its use of a path notation as in URLs for navigating through the hierarchical structure of an XML document.

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:

Expression evaluation occurs with respect to a context. XSLT and XPointer specify how the context is determined for XPath expressions used in XSLT and XPointer respectively. The context consists of:

The context node is always a member of the context node list.

The variable bindings consist of a mapping from variable names to variable values. The value of a variable is an object, which can be of any of the types that are possible for the value of an expression, and may also be of additional types not specified here.

The function library consists of a set of named functions. Each function takes zero or more arguments and returns a single result. This document defines a core function library that all XPath implementations must support (see [4 Core Function Library]). For a function in the core function library, arguments and result are of the four basic types. Both XSLT and XPointer extend XPath by defining additional functions; some of these functions operate on the four basic types; others operate on additional data types defined by XSLT and XPointer.

The variable bindings, function library and namespace declarations used to evaluate a subexpression are always the same as those used to evaluate the containing expression. The context node and context node list used to evaluate a subexpression are sometimes different from the context node and context node list used to evaluate the containing expression. When the evaluation of a kind of expression is described, it will always be explicitly stated if the context node and node list change for the evaluation of subexpressions; if nothing is said about the context node and context node list, they remain unchanged for the evaluation of subexpressions of that kind of expression.

XPath expressions often occur in XML attributes. 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 &lt;. 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 (&quot; or &apos;). 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 lists of nodes.

In the following grammar, the non-terminals QName and NCName are defined in [XML Names], and S is defined in [XML].

Expressions are parsed by first dividing the character string to be parsed into tokens and then parsing the resulting sequence of tokens. Whitespace can be freely used between tokens. The tokenization process is described in [3.7 Lexical Structure].

2 Location Paths

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 (see [2.3 Abbreviated Syntax]).

Here are some examples of location paths using the unabbreviated syntax:

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 the second 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 / optionally followed by a relative location path. A / by itself selects the root node of the document containing the context node. If it is followed by a relative location path, then the location path selects the set of nodes that would be selected by the relative location path relative to the root node of the document containing the context node.

A location step starts by specifying an initial list of nodes, which is called the basis of the location step. The location step optionally continues with one of predicates specified by expressions in square brackets. The basis is filtered by the first predicate; the result of that is then filtered by the next predicate and so on. Each predicate selects nodes that satisfy a condition specified by an arbitrary expression. The result of the location step is the set of nodes that are members of the list that results from filtering the initial list by all the predicates. Note that although a location step selects a set of nodes, a basis selects a list of nodes and the predicates operate on a list of nodes.

Issue (node-ordering): The way that the current design handles the ordering of collections of nodes produces some surprises. For example, preceding::foo[1] has a different meaning from (preceding::foo)[1]: the latter returns the first foo element in document order; the former returns the first foo element in reverse document order. Can this design be improved? One possibility is to make all axes be in document order; this would allow a basis to return a node-set; [] would order the positions in this set in document order.

Location Paths
[1]    LocationPath    ::=    RelativeLocationPath
| AbsoluteLocationPath
[2]    AbsoluteLocationPath    ::=    '/' RelativeLocationPath?
| AbbreviatedAbsoluteLocationPath
[3]    RelativeLocationPath    ::=    Step
| RelativeLocationPath '/' Step
| AbbreviatedRelativeLocationPath
[4]    Step    ::=    Basis Predicate*
| AbbreviatedStep

2.1 Bases

The basis has two parts:

The syntax for the basis is the name of the axis followed by a double colon followed by the node test. For example, the basis descendant::para is a list of the para element descendants of the context node: descendant specifies that each node in the basis must be a descendant of the context; para specifies that each node in the basis must be an element named para.

The order of the nodes in the basis is determined by the axis. The general principal is that nodes in an axis are ordered "going away" from the context node. An axis that can never contain a node that is after the context node in document order is ordered in reverse document order; otherwise, an axis is ordered in document order.

Bases
[5]    Basis    ::=    AxisName '::' NodeTest
| AbbreviatedBasis

2.1.1 Axes

The following axes are available:

Note that the ancestor, descendant, following, preceding and self axes partition a document (ignoring attribute and namespace nodes): they do not overlap and together they contain all the nodes in the document.

Axes
[6]    AxisName    ::=    'ancestor'
| 'ancestor-or-self'
| 'attribute'
| 'child'
| 'descendant'
| 'descendant-or-self'
| 'following'
| 'following-sibling'
| 'namespace'
| 'parent'
| 'preceding'
| 'preceding-sibling'
| 'self'

2.1.2 Node Tests

Every axis has a principal node type:

A node test that is a QName tests whether the node is of the principal node type and has the specified name. For example, child::para selects the para element children of the context node; if the context node has no para children, it will select an empty set of nodes. attribute::href selects the href attribute of the context node; if the context node has no href attribute, it will select an empty set of nodes.

A QName in the node test is expanded into a local name and a possibly null URI. This expansion is done using the namespace declarations from the expression context. This is the same way expansion is done for element type names in start and end-tags except that the default namespace declared with xmlns is not used: if the QName does not have a prefix, then the URI is null (this is the same way attribute names are expanded). The expanded names are then compared for equality. Two expanded names are equal if they have the same local part, and either both have no URI or both have the same URI.

A node test * is true for any node of the principal node type. For example, child::* will select all element children of the context node, and attribute::* will select all attributes of the context node.

A node test can have the form NCName:*. In this case, the prefix is expanded in the same way as with a QName using the context namespace declarations. The node test will be true for any node of the principal type whose expanded name has the URI to which the prefix expands, regardless of the local part of the name.

The node test text() is true for any text node. For example, child::text() will select the text node children of the context node. Similarly, the node test comment() is true for any comment node, and the node test processing-instruction() is true for any processing instruction. The processing-instruction() test may have an argument that is Literal; in this case, it is true for any processing instruction that has a name equal to the value of the Literal.

A node test node() is true for any node of any type whatsoever.

[7]    NodeTest    ::=    WildcardName
| NodeType '(' ')'
| 'processing-instruction' '(' Literal ')'

2.2 Predicates

A predicate filters a list of nodes to produce a new list of nodes. For each node in the list to be filtered, the PredicateExpr is evaluated with that node as the context node and with the complete list of nodes to be filtered as the context node list; if PredicateExpr evaluates to true for that node, the node is included in the new list; otherwise, it is not included.

A PredicateExpr is evaluated by evaluating the Expr and converting the result to a boolean. If the result is a number, the result will be converted to true if the number is equal to the position of the context node in the context node list (as returned by the position function) 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].

Issue (bracket-overload): The way that the overloading of [] for both boolean tests and numeric indices is resolved produces some surprises. For example, if the variable x is a number, then foo[$x] means the same as foo[position()=$x]; however, if x is a string or a result tree fragment, then foo[$x] does not mean the same as foo[position()=$x]. What can be done about this?

Predicates
[8]    Predicate    ::=    '[' PredicateExpr ']'
[9]    PredicateExpr    ::=    Expr

2.3 Abbreviated Syntax

Here are some examples of location paths using abbreviated syntax:

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 (even a para element that is a document element will be selected by //para since the document element node is a child of the root node); div//para is short for div/descendant-or-self::node()/child::para and so will select all para descendants of div children.

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.

Abbreviations
[10]    AbbreviatedAbsoluteLocationPath    ::=    '//' RelativeLocationPath
[11]    AbbreviatedRelativeLocationPath    ::=    RelativeLocationPath '//' Step
[12]    AbbreviatedStep    ::=    '.'
| '..'
[13]    AbbreviatedBasis    ::=    NodeTest
| '@' NodeTest

3 Expressions

3.1 Basics

A VariableReference evaluates to the value to which the variable name is bound in the set of variable bindings in the context. It is an error if the variable is not bound to any value in the set of variable bindings in the expression context.

Parentheses may be used for grouping.

[14]    Expr    ::=    OrExpr
[15]    PrimaryExpr    ::=    VariableReference
| '(' Expr ')'
| Literal
| Number
| FunctionCall

3.2 Function Calls

[16]    FunctionCall    ::=    FunctionName '(' ( Argument ( ',' Argument)*)? ')'
[17]    Argument    ::=    Expr

A FunctionCall expression is evaluated by evaluating each of the Arguments, converting each argument to the type required by the function, calling the named function from the function library that is part of the expression evaluation context passing it the converted arguments. The result of the FunctionCall expression is the result returned by the function.

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. It is an error if the number or type of arguments is wrong.

3.3 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.

Square brackets are used to filter expressions in the same way that they are used in location paths. It is an error if the expression to be filtered does not evaluate to a node-set. The context node list used for evaluating the expression in square brackets is the node-set to be filtered listed in document order.

The / operator and // operators combine an arbitrary expression and a relative location path. It is an error if the expression does not evaluate to a node-set. The / operator does composition in the same way as when / is used in a location path. As in location paths, // is short for /descendant-or-self::node()/.

There are no types of objects that can be converted to node-sets.

[18]    UnionExpr    ::=    PathExpr
| UnionExpr '|' PathExpr
[19]    PathExpr    ::=    LocationPath
| FilterExpr
| FilterExpr '/' RelativeLocationPath
| FilterExpr '//' RelativeLocationPath
[20]    FilterExpr    ::=    PrimaryExpr
| FilterExpr Predicate

3.4 Booleans

An object of type boolean can have two values, true and false.

An or expression is evaluated by evaluating each operand and converting its value to a boolean. 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. The result is true if both values are true and false otherwise.

A RelationalExpr or an EqualityExpr is evaluated by comparing the objects that result from evaluating the operands. If both operands are node-sets, then the comparison will be true if and only if there is a node in the first node-set operand and a node in the second node-set operand such that the result of performing the comparison on the string values of the two nodes is true. If one operand 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 operand such that the result of performing the comparison on the number operand and on the result of converting the value of that node to a number using the number function is true. If one operand 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 operand such that the result of performing the comparison on the string value of the node and the other operand is true. If one operand 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 operand and on the result of converting the node-set to a boolean using the boolean function is true.

When neither operand is a node-set, then the operands of an EqualityExpr are converted to a common type as follows and then compared. If at least one operand is a boolean, then each operand is converted to a boolean as if by applying the boolean function. Otherwise, if at least one operand is a number, then each operand is converted to a number as if by applying the number function. Otherwise, both operands are converted to strings as if by applying the string function. The = comparison will be true if and only if both operands are equal; the != comparison will be true if and only if both operands are not equal. Numbers are compared for equality according to IEEE 754. Two booleans are true if either both are true or both are false. Two strings are equal if and only if they both consist of the same sequence of UCS characters.

When neither operand is a node-set, then the operands of a RelationalExpr are each converted to a number and compared according to IEEE 754. 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.

[21]    OrExpr    ::=    AndExpr
| OrExpr 'or' AndExpr
[22]    AndExpr    ::=    EqualityExpr
| AndExpr 'and' EqualityExpr
[23]    EqualityExpr    ::=    RelationalExpr
| EqualityExpr '=' RelationalExpr
| EqualityExpr '!=' RelationalExpr
[24]    RelationalExpr    ::=    AdditiveExpr
| RelationalExpr '<' AdditiveExpr
| RelationalExpr '>' AdditiveExpr
| RelationalExpr '<=' AdditiveExpr
| RelationalExpr '>=' AdditiveExpr

3.5 Numbers

A number represents a floating-point number. A number can have any double-precision 64-bit format IEEE 754 value. These include a special "Not-a-Number" (NaN) value, positive and negative infinity, and positive and negative zero.

The numeric operators convert their operands to numbers as if by calling the number function.

The div operator performs floating-point division according to IEEE 754.

The mod operator returns the remainder from a truncating division. For example,

NOTE: This is the same as the % operator in Java and ECMAScript.
NOTE: This is not the same as the IEEE remainder operation, which returns the remainder from a rounding division.
Numeric Expressions
[25]    AdditiveExpr    ::=    MultiplicativeExpr
| AdditiveExpr '+' MultiplicativeExpr
| AdditiveExpr '-' MultiplicativeExpr
[26]    MultiplicativeExpr    ::=    UnaryExpr
| MultiplicativeExpr MultiplyOperator UnaryExpr
| MultiplicativeExpr 'div' UnaryExpr
| MultiplicativeExpr 'mod' UnaryExpr
[27]    UnaryExpr    ::=    UnionExpr
| '-' UnaryExpr

3.6 Strings

Strings consist of a sequence of zero or more characters, where a character is defined as in the XML Recommendation [XML]. A single character in XPath thus corresponds to a single Unicode 2.0 abstract character with a single corresponding Unicode scalar value; this is not the same thing as a 16-bit Unicode code value: the Unicode coded character representation for an abstract character with Unicode scalar value greater that U+FFFF is a pair of 16-bit Unicode code values (a surrogate pair). In many programming languages, a string is represented by a sequence of 16-bit Unicode code values; implementations of XPath in such languages must take care to ensure that a surrogate pair is correctly treated as a single XPath character.

3.7 Lexical Structure

When tokenizing, the longest possible token is always returned.

For readability, whitespace may be used in patterns even though not explicitly allowed by the grammar: ExprWhitespace may be freely added within patterns before or after any ExprToken.

A NodeType or FunctionName token is recognized only when the following token is (. An AxisName token is recognized only when the following token is ::. An OperatorName token or MultiplyOperator token is recognized as such only when there is a preceding token and the preceding token is not one of @, ::, (, [, , or an Operator.

Expression Lexical Structure
[28]    ExprToken    ::=    '(' | ')' | '[' | ']' | '.' | '..' | '@' | ',' | '::'
| WildcardName
| NodeType
| Operator
| FunctionName
| AxisName
| Literal
| Number
| VariableReference
[29]    Literal    ::=    '"' [^"]* '"'
| "'" [^']* "'"
[30]    Number    ::=    Digits ('.' Digits)?
| '.' Digits
[31]    Digits    ::=    [0-9]+
[32]    Operator    ::=    OperatorName
| MultiplyOperator
| '/' | '//' | '|' | '+' | '-' | '=' | '!=' | '<'| '<=' | '>' | '>='
[33]    OperatorName    ::=    'and' | 'or' | 'mod' | 'div'
[34]    MultiplyOperator    ::=    '*'
[35]    FunctionName    ::=    QName - NodeType
[36]    VariableReference    ::=    '$' QName
[37]    WildcardName    ::=    '*'
| NCName ':' '*'
| QName
[38]    NodeType    ::=    'comment'
| 'text'
| 'processing-instruction'
| 'node'
[39]    ExprWhitespace    ::=    S

4 Core Function Library

This section describes functions that XPath implementations must always include in the function library that is used to evaluate expressions.

4.1 Node Set Functions

Function: number last()

The last function returns the number of nodes in the context node list.

Function: number position()

The position function returns the position of the context node in the context node list. The first position is 1, and so the last position will be equal to last().

Function: number count(node-set)

The count function returns the number of nodes in the argument node-set.

Ed. Note: It would be possible to combine the count and last functions, but this would be inconsistent with other functions which default an optional node-set argument to the current node. Feedback is solicited.

Function: node-set id(object)

The id function selects elements by their unique ID (see [5.2.1 Unique IDs]). When the argument to id is of type node-set, then the result is the union of the result of applying id to the string value of each of the nodes in the argument node-set. When the argument to id is of any other type, the argument is converted to a string as if by a call to the string function; the string is split into a whitespace-separated list of tokens (whitespace is any sequence of characters matching the production S); the result is a node-set containing the elements in the same document as the context node that have a unique ID equal to any of the tokens in the list.

Function: string local-part(node-set?)

The local-part function returns a string containing the local part of the name of the node in the argument node-set that is first in document order. If the node-set is empty or the first node has no name, an empty string is returned. If the argument is omitted, it defaults to a node-set with the context node as its only member.

Function: string namespace(node-set?)

The namespace function returns a string containing the namespace URI of the expanded name of the node in the argument node-set that is first in document order. If the node-set is empty, the first node has no name, or the expanded name has no namespace URI, an empty string is returned. If the argument is omitted, it defaults to a node-set with the context node as its only member.

Issue (namespace-dom-harmonize): The names for the namespace() and local-part() functions need to be coordinated with the DOM Level 2.

Function: string name(node-set?)

The name function returns a string containing a QName representing the name of the node in the argument node-set that is first in document order. The QName must represent the name with respect to the namespace declarations in effect on the node whose name is being represented. Typically, this will be the form in which the name occurred in the XML source. This need not be the case if there are namespace declarations in effect on the node that associate multiple prefixes with the same namespace. However, an implementation may include information about the original prefix in its representation of nodes; in this case, an implementation can ensure that the returned string is always the same as the QName used in the XML source. 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:

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 (see [3.6 Strings]) 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 second argument and, if the third argument is specified, less than the sum of the second and third arguments; the comparisons and addition used for the above follow the standard IEEE 754 rules. Thus:

Function: number string-length(string?)

The string-length returns the number of characters in the string (see [3.6 Strings]). If the argument is omitted, it defaults to the context node converted to a string, in other words the value of the context node.

Function: string normalize(string?)

The normalize function returns the argument string with white space normalized by stripping leading and trailing whitespace and replacing sequences of whitespace characters by a single space. Whitespace characters are the same allowed by the S production in XML. If the argument is omitted, it defaults to the context node converted to a string, in other words the 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 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:

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.

Function: boolean lang(string)

The lang function returns true or false depending on whether the language of the context node as specified by xml:lang attributes is the same as or is a sublanguage of the language specified by the argument string. The language of the context node is determined by the value of the xml:lang attribute on the context node, or, if the context node has no xml:lang attribute, by the value of the xml:lang attribute on the nearest ancestor of the context node that has an xml:lang attribute. If there is no such attribute, then lang returns false. If there is such an attribute, then lang returns true if the attribute value is equal to the argument ignoring case, or if there is some suffix starting with - such that the attribute value is equal to the argument ignoring that suffix of the attribute value and ignoring case. For example, lang("en") would return true if the context node is any of these five elements:

<para xml:lang="en"/>
<div xml:lang="en"><para/></div>
<para xml:lang="EN"/>
<para xml:lang="en-us"/>

4.4 Number Functions

Function: number number(object?)

The number function converts its argument to a number as follows:

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 of the values of the nodes in the argument node-set.

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 even is returned.

Issue (round): Should the round function round .5 upwards for consistency with ECMAScript and Java, instead of rounding to even?

5 Data Model

Ed. Note: This section will be rewritten in terms of the XML Infoset WD.

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.

XML documents operated on by XPath must conform to the XML namespaces specification [XML Names].

The tree contains nodes. There are seven kinds of node:

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 value is part of the node; for other types of node, the value is computed from the value of descendant nodes.

5.1 Root Node

The root node is the root of the tree. It does not occur anywhere else in the tree. The element node for the document element is a child of the root node. The root node also has as children processing instruction and comment nodes for processing instructions and comments that occur in the prolog and after the end of the document element.

The value of the root node is the value of the document element.

5.2 Element Nodes

There is an element node for every element in the document. An element has an expanded name consisting of a local name and a possibly null URI reference (see [XML Names]); the URI reference will be null if the element type name has no prefix and there is no default namespace in scope. A relative URI reference should be resolved into an absolute URI reference during namespace processing.

The children of an element node are the element nodes, comment nodes, processing instruction nodes and text nodes for its content. Entity references to both internal and external entities are expanded. Character references are resolved.

The descendants of an element node are the children of the element node and the descendants of the children that are element nodes.

The value of an element node is the string that results from concatenating all characters that are descendants of the element node in the order in which they occur in the document.

The set of all element nodes in a document can be ordered according to the order of the start-tags of the elements in the document; this is known as document order.

Ed. Note: Need a definition of document order that handles arbitrary node types, including attributes.

5.2.1 Unique IDs

An element object may have a unique identifier (ID). This is the value of the attribute that is declared in the DTD as type ID. No two elements in a document may have the same unique ID. If an XML processor reports two elements in a document as having the same unique ID (which is possible only if the document is invalid) then the second element must be treated as not having a unique ID.

NOTE: If a document does not have a DTD, then no element in the document will have a unique ID.

5.3 Attribute Nodes

Each element node has an associated set of attribute nodes. A defaulted attribute is treated the same as a specified attribute. If an attribute was declared for the element type in the DTD, but the default was declared as #IMPLIED, and the attribute was not specified on the element, then the element's attribute set does not contain a node for the attribute.

An attribute node has an expanded name and has a string value. The expanded name consists of a local name and a possibly null URI reference (see [XML Names]); the URI reference will be null if the specified attribute name did not have a prefix. The value is the normalized value as specified by the XML Recommendation [XML]. An attribute whose normalized value is a zero-length string is not treated specially: it results in an attribute node whose value is a zero-length string.

There are no attribute nodes corresponding to attributes that declare namespaces (see [XML Names]).

Ed. Note: Point out potential pitfalls caused by an external DTD not being read.

5.4 Namespace Nodes

Each element has an associated set of namespace nodes, one for each namespace prefix that is in scope for the element and one for the default namespace if one is in scope for the element. This means that an element will have a namespace node:

A namespace node has a name which is a string giving the prefix. This is empty if the namespace node is for the default namespace. A namespace node also has a value, which is the namespace URI. If the namespace declaration specifies a relative URI, then the resolved absolute URI is used as the value.

5.5 Processing Instruction Nodes

There is a processing instruction node for every processing instruction.

Ed. Note: What about processing instructions in the internal subset or elsewhere in the DTD?

A processing instruction has a name. This is a string equal to the processing instruction's target. It also has a value. This is a string equal to the part of the processing instruction following the target and any whitespace. It does not include the terminating ?>.

NOTE: The XML declaration is not a processing instruction. Therefore, there is no processing instruction node corresponding to the XML declaration.

5.6 Comment Nodes

There is a comment node for every comment.

Ed. Note: What about comments in the internal subset or elsewhere in the DTD?

A comment has a value. This is a string equal to the text of the comment not including the opening <!-- or the closing -->.

5.7 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 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 &lt;. 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 &lt; and &amp; respectively.

NOTE: When a text node that contains a < character is written out as XML, the < character must be escaped by, for example, using &lt;, or including it in a CDATA section.

Characters inside comments or processing instructions are not character data. Line-endings in external entities are normalized to #xA as specified in the XML Recommendation [XML].


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

XPointer
World Wide Web Consortium. XML Pointer Language (XPointer). W3C Working Draft. See http://www.w3.org/TR/WD-xptr
XSLT
World Wide Web Consortium. XSL Transformations (XSLT). W3C Working Draft. See http://www.w3.org/TR/WD-xslt

B Changes from Previous XSLT Public Working Draft

The syntax for axes has changed from, for example, from-children(foo) to child::foo.

A != operator has been added.

The behavior of relational operators with node-set operands has been changed.

The quo operator has been removed.

The pi() node test has been renamed to processing-instruction().

The qname function has been renamed to name.

The substring function has been added.

The string-length function has been added.

The functionality of the idref function has been merged into the id function.

The number function applied to a string that is not a Number returns NaN rather than 0.

Number to string conversion is more fully specified.

The translate function is more fully specified.

The following axis excludes descendants and the preceding axis excludes ancestors.

The argument to the boolean is no longer optional.