XPath 1.0 as 2.0 EBNF Grammar
This document includes material copied from or derived from the XPath specifications. Copyright © 1999-2017 W3C® (MIT, ERCIM, Keio, Beihang).
Abstract
This document describes the XPath 1.0 EBNF grammar using the XPath 2.0 EBNF symbols. This is to make it easier to implement an XPath 1.0 parser as a subset of an XPath 2.0 parser with minor modifications to account for precedence changes.
Table of Contents
A XPath Grammar
A.1 EBNF
Ref | Symbol | Expression | Options | |
---|---|---|---|---|
[1] | XPath |
::= | Expr |
|
[2] | Expr |
::= | ExprSingle |
|
[3] | ExprSingle |
::= | OrExpr |
|
[4] | OrExpr |
::= | AndExpr ( "or" AndExpr )* |
|
[5] | AndExpr |
::= | EqualityExpr ( "and" EqualityExpr )* |
|
[6] | EqualityExpr |
::= | RelationalExpr ( ("=" \| "!=") RelationalExpr )* |
/* gn: comparison */ |
[7] | RelationalExpr |
::= | AdditiveExpr ( ("<" \| ">" \| "<=" \| ">=") AdditiveExpr )* |
/* gn: comparison */ |
[8] | AdditiveExpr |
::= | MultiplicativeExpr ( ("+" \| "-") MultiplicativeExpr )* |
|
[9] | MultiplicativeExpr |
::= | UnaryExpr ( ( "*" \| "div" \| "mod") UnaryExpr )* |
|
[10] | UnaryExpr |
::= | "-"* UnionExpr |
|
[11] | UnionExpr |
::= | PathExpr ( "|" PathExpr )* |
|
[12] | PathExpr |
::= | ("/" RelativePathExpr?) \| ("//" RelativePathExpr) \| RelativePathExpr |
/* xgs: leading-lone-slash */ |
[13] | RelativePathExpr |
::= | StepExpr (("/" \| "//") StepExpr)* |
|
[14] | StepExpr |
::= | FilterExpr \| AxisStep |
/* xgc: filter-expr */ |
[15] | AxisStep |
::= | (ReverseStep \| ForwardStep) PredicateList |
|
[16] | ForwardStep |
::= | (ForwardAxis NodeTest) \| AbbrevForwardStep |
|
[17] | ForwardAxis |
::= | ("child" "::") \| ("descendant" "::") \| ("attribute" "::") \| ("self" "::") \| ("descendant-or-self" "::") \| ("following-sibling" "::") \| ("following" "::") \| ("namespace" "::") |
|
[18] | AbbrevForwardStep |
::= | '@'? NodeTest |
|
[19] | ReverseStep |
::= | (ReverseAxis NodeTest) \| AbbrevReverseStep |
|
[20] | ReverseAxis |
::= | ("parent" "::") \| ("ancestor" "::") \| ("preceding-sibling" "::") \| ("preceding" "::") \| ("ancestor-or-self" "::") |
|
[21] | AbbrevReverseStep |
::= | ”..” | /* xgc: predicate */ |
[22] | NodeTest |
::= | KindTest \| NameTest |
|
[23] | NameTest |
::= | QName \| Wildcard |
|
[24] | Wildcard |
::= | "*" \| (NCName ":" "*") |
/* ws: explicit */ |
[25] | FilterExpr |
::= | PrimaryExpr PredicateList |
|
[26] | PredicateList |
::= | Predicate* |
|
[27] | Predicate |
::= | "[" Expr "]" |
|
[28] | PrimaryExpr |
::= | Literal \| VarRef \| ParenthesizedExpr \| ContextItemExpr \| FunctionCall |
|
[29] | Literal |
::= | NumericLiteral \| StringLiteral |
|
[30] | NumericLiteral |
::= | IntegerLiteral \| DecimalLiteral |
|
[31] | VarRef |
::= | "$" VarName |
|
[32] | VarName |
::= | QName |
|
[33] | ParenthesizedExpr |
::= | "(" Expr ")" |
|
[34] | ContextItemExpr |
::= | "." |
/* xgc: predicate */ |
[35] | FunctionCall |
::= | QName "(" ( ExprSingle ( "," ExprSingle )* )? ')' |
/* xgs: reserved-function-names */ /* gn: parens */ |
[36] | KindTest |
::= | PITest \| CommentTest \| TextTest \| AnyKindTest |
|
[37] | AnyKindTest |
::= | "node" "(" ")" |
|
[38] | TextTest |
::= | "text" "(" ")" |
|
[39] | CommentTest |
::= | "comment" "(" ")" |
|
[40] | PITest |
::= | "processing-instruction" "(" StringLiteral? ")" |
A.1.1 Extra-grammatical Constraints
This section contains constraints on the EBNF productions in addition to those
defined by XPath 2.0, which are required to parse legal sentences. The notes
below are referenced from the right side of the production, with the notation:
/* xgc: <id> */
.
constraint: filter-expr
XPath 1.0 only supports
FilterExpr
nodes at the start of aPathExpr
, including leading/
and//
tokens. These are allowed anywhere in aPathExpr
in XPath 2.0.
constraint: predicate
XPath 1.0 does not support an
AbbrevReverseStep
orContextItemExpr
followed by aPredicate
.
A.1.2 Grammar Notes
This section contains general notes on the EBNF productions in addition to those
defined by XPath 2.0, which may be helpful in understanding how to interpret and
implement the EBNF. These notes are not normative. The notes below are referenced
from the right side of the production, with the notation: /* gn: <id> */
.
grammar-note: comparison
These XPath 1.0 symbols are retained here as the XPath 2.0 replacement (
ComparisonExpr
) only allows a singleGeneralComp
without anor
orand
expression.
A.2 Terminal Symbols
Ref | Symbol | Expression | Options | |
---|---|---|---|---|
[41] | StringLiteral |
::= | ('"' \[^"\]* '"') \| ("'" \[^'\]* "'") |
/* ws: explicit */ |
[42] | IntegerLiteral |
::= | Digits |
|
[43] | DecimalLiteral |
::= | ("." Digits) \| (Digits "." \[0-9\]*) |
/* ws: explicit */ |
[44] | S |
::= | [http://www.w3.org/TR/REC-xml#NT-S]XML | /* xgc: xml-version */ |
[45] | QName |
::= | [http://www.w3.org/TR/REC-xml-names/#NT-QName]Names | /* xgc: xml-version */ |
[46] | NCName |
::= | [http://www.w3.org/TR/REC-xml-names/#NT-NCName]Names | /* xgc: xml-version */ |
The following symbols are used only in the definition of terminal symbols; they are not terminal symbols in the grammar of A.1 EBNF.
Ref | Symbol | Expression | Options | |
---|---|---|---|---|
[47] | Digits |
::= | [0-9]+ |
A.3 Reserved Function Names
The following names are not allowed as function names in an unprefixed form because expression syntax takes precedence.
comment
node
processing-instruction
text
B References
B.1 W3C References
Core Specifications
- W3C. XML Path Language (XPath) 1.0. W3C Recommendation 16 November 1999. See https://www.w3.org/TR/1999/REC-xpath-19991116/.
- W3C. XML Path Language (XPath) 2.0. W3C Recommendation 14 December 2010. See https://www.w3.org/TR/2010/REC-xpath20-20101214/.
C Change Log
This section documents the changes from the XPath 1.0 to XPath 2.0 EBNF grammar.
Path Expressions
- Renamed
Expr
toExprSingle
. - Added the
XPath
andExpr
symbols from XPath 2.0. - Inlined the
AbbreviatedAbsoluteLocationPath
andAbsoluteLocationPath
symbols intoLocationPath
. - Inlined the
AbbreviatedRelativeLocationPath
symbol intoRelativeLocationPath
. - Moved
Predicate*
fromStep
into aPredicateList
symbol. - Moved
FilterExpr
intoStep
, adding afilter-expr
extra-grammatical constraint. - Inlined the
LocationPath
symbol intoPathExpr
. - Renamed
RelativeLocationPath
toRelativePathExpr
. - Renamed
Step
toStepExpr
.
Axis Steps
- Split
AxisName
intoForwardAxis
andReverseAxis
, and combine the keywords with the::
token. - Moved
NodeTest
fromStep
toAxisSpecifier
. - Renamed
AbbreviatedAxisSpecifier
toAbbrevForwardStep
. - Added the
ForwardStep
symbol from XPath 2.0. - Moved
ReverseAxis NodeTest
into aReverseStep
symbol. - Moved
PredicateList
intoAxisSpecifier
. - Renamed
AxisSpecifier
toAxisStep
. - Used a
predicate
extra-grammatical constraint to preventAbbrevReverseStep
symbols followed by aPredicate
after moving..
fromAbbreviatedStep
toAbbrevReverseStep
.
Node Tests
- Move the
node
NodeType
into anAnyKindTest
symbol. - Move the
text
NodeType
into aTextTest
symbol. - Move the
comment
NodeType
into aCommentTest
symbol. - Move the
processing-instruction
NodeType
into aPITest
symbol. - Move the
processing-instruction
with aStringLiteral
fromNodeTest
into thePITest
symbol. - Move
AnyKindTest
,TextTest
,CommentTest
, andPITest
into aKindTest
symbol. - Split out the wildcard syntax from
NameTest
into aWildcard
symbol.
Filter Expressions
- Moved
Predicate*
fromFilterExpr
into aPredicateList
symbol. - Inlined the
PredicateExpr
symbol intoPredicate
.
Primary Expressions
- Renamed
VariableReference
toVarRef
. - Added the
Literal
andVarName
symbols from XPath 2.0. - Moved the parenthesized primary expression into a
ParenthesizedExpr
symbol. - Inlined the
Argument
symbol intoFunctionCall
. - Used a
predicate
extra-grammatical constraint to preventContextItemExpr
symbols followed by aPredicate
after moving.
fromAbbreviatedStep
toContextItemExpr
.
Terminal Symbols
- Renamed
Literal
toStringLiteral
. - Replaced
Number
withNumericLiteral
,IntegerLiteral
, andDecimalLiteral
from XPath 2.0. - Replaced the
ExprWhitespace
symbol with a link to theS
symbol from the XML specification. - Added links to the
NCName
andQName
symbols from the Namespaces in XML specification. - Removed the
ExprToken
,Operator
, andOperatorName
symbols that are not referenced elsewhere in the XPath 1.0 grammar. - Inlined the
MultiplyOperator
symbol intoMultiplicativeExpr
. - Replaced the
FunctionName
symbol with a reserved function names section.
D Differences Between XPath 1.0 and XPath 2.0
The following is the list of features added to XPath 2.0 that are not present in XPath 1.0:
- Allow multiple comma separated expressions in
Expr
. - Support
ForExpr
,QuantifiedExpr
, andIfExpr
in single expressions. - Support
ValueComp
andNodeComp
comparisons. - Support range expressions (
RangeExpr
). - Support
idiv
in multiplicative expressions (MultiplicativeExpr
). - Support
IntersectExceptExpr
,InstanceofExpr
,TreatExpr
,CastableExpr
, andCastExpr
. - Support
+
in unary expressions (UnaryExpr
). - Support
FilterExpr
anywhere in a path expression, not just at the start. - Support predicates on
..
steps; useparent::*
in XPath 1.0. - Added support for
DoubleLiteral
inNumericLiteral
. - Made the
Expr
inParenthesizedExpr
optional. - Added
DocumentTest
,ElementTest
,AttributeTest
,SchemaElementTest
, andSchemaAttributeTest
. - Added support for
NCName
inPITest
. - Added support for
*:NCName
wildcards.
The following are differences to XPath 1.0 that change how the expression is interpreted:
- Only allow one
GeneralComp
without anor
/and
expression. - Place
UnionExpr
afterMultiplicativeExpr
instead ofUnaryExpr
.
The following keywords have been added to the Reserved Function Names list:
attribute
document-node
element
empty-sequence
if
item
schema-attribute
schema-element
typeswitch