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
FilterExprnodes at the start of aPathExpr, including leading/and//tokens. These are allowed anywhere in aPathExprin XPath 2.0.
constraint: predicate
XPath 1.0 does not support an
AbbrevReverseSteporContextItemExprfollowed 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 singleGeneralCompwithout anororandexpression.
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 ExprtoExprSingle.
- Added the XPathandExprsymbols from XPath 2.0.
- Inlined the AbbreviatedAbsoluteLocationPathandAbsoluteLocationPathsymbols intoLocationPath.
- Inlined the AbbreviatedRelativeLocationPathsymbol intoRelativeLocationPath.
- Moved Predicate*fromStepinto aPredicateListsymbol.
- Moved FilterExprintoStep, adding afilter-exprextra-grammatical constraint.
- Inlined the LocationPathsymbol intoPathExpr.
- Renamed RelativeLocationPathtoRelativePathExpr.
- Renamed SteptoStepExpr.
Axis Steps
- Split AxisNameintoForwardAxisandReverseAxis, and combine the keywords with the::token.
- Moved NodeTestfromSteptoAxisSpecifier.
- Renamed AbbreviatedAxisSpecifiertoAbbrevForwardStep.
- Added the ForwardStepsymbol from XPath 2.0.
- Moved ReverseAxis NodeTestinto aReverseStepsymbol.
- Moved PredicateListintoAxisSpecifier.
- Renamed AxisSpecifiertoAxisStep.
- Used a predicateextra-grammatical constraint to preventAbbrevReverseStepsymbols followed by aPredicateafter moving..fromAbbreviatedSteptoAbbrevReverseStep.
Node Tests
- Move the nodeNodeTypeinto anAnyKindTestsymbol.
- Move the textNodeTypeinto aTextTestsymbol.
- Move the commentNodeTypeinto aCommentTestsymbol.
- Move the processing-instructionNodeTypeinto aPITestsymbol.
- Move the processing-instructionwith aStringLiteralfromNodeTestinto thePITestsymbol.
- Move AnyKindTest,TextTest,CommentTest, andPITestinto aKindTestsymbol.
- Split out the wildcard syntax from NameTestinto aWildcardsymbol.
Filter Expressions
- Moved Predicate*fromFilterExprinto aPredicateListsymbol.
- Inlined the PredicateExprsymbol intoPredicate.
Primary Expressions
- Renamed VariableReferencetoVarRef.
- Added the LiteralandVarNamesymbols from XPath 2.0.
- Moved the parenthesized primary expression into a ParenthesizedExprsymbol.
- Inlined the Argumentsymbol intoFunctionCall.
- Used a predicateextra-grammatical constraint to preventContextItemExprsymbols followed by aPredicateafter moving.fromAbbreviatedSteptoContextItemExpr.
Terminal Symbols
- Renamed LiteraltoStringLiteral.
- Replaced NumberwithNumericLiteral,IntegerLiteral, andDecimalLiteralfrom XPath 2.0.
- Replaced the ExprWhitespacesymbol with a link to theSsymbol from the XML specification.
- Added links to the NCNameandQNamesymbols from the Namespaces in XML specification.
- Removed the ExprToken,Operator, andOperatorNamesymbols that are not referenced elsewhere in the XPath 1.0 grammar.
- Inlined the MultiplyOperatorsymbol intoMultiplicativeExpr.
- Replaced the FunctionNamesymbol 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, andIfExprin single expressions.
- Support ValueCompandNodeCompcomparisons.
- Support range expressions (RangeExpr).
- Support idivin multiplicative expressions (MultiplicativeExpr).
- Support IntersectExceptExpr,InstanceofExpr,TreatExpr,CastableExpr, andCastExpr.
- Support +in unary expressions (UnaryExpr).
- Support FilterExpranywhere in a path expression, not just at the start.
- Support predicates on ..steps; useparent::*in XPath 1.0.
- Added support for DoubleLiteralinNumericLiteral.
- Made the ExprinParenthesizedExproptional.
- Added DocumentTest,ElementTest,AttributeTest,SchemaElementTest, andSchemaAttributeTest.
- Added support for NCNameinPITest.
- Added support for *:NCNamewildcards.
The following are differences to XPath 1.0 that change how the expression is interpreted:
- Only allow one GeneralCompwithout anor/andexpression.
- Place UnionExprafterMultiplicativeExprinstead 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