XQuery IntelliJ Plugin 1.9 XPath

This document includes material copied from or derived from the XPath specifications. Copyright © 1999-2017 W3C® (MIT, ERCIM, Keio, Beihang).

Abstract

This document defines the syntax and semantics for vendor and plugin specific functionality that extends XPath and associated W3C extensions. The plugin-specific extensions are provided to support IntelliJ integration.

Table of Contents

1 Introduction

This document defines the syntax and semantics for vendor and plugin specific functionality that extends XPath 2.0. The syntax described here is the syntax that is supported by the XQuery IntelliJ Plugin.

2 Basics

This document uses the following namespace prefixes to represent the namespace URIs with which they are listed. Although these prefixes are used within this specification to refer to the corresponding namespaces, not all of these bindings will necessarily be present in the static context of every expression, and authors are free to use different prefixes for these namespaces, or to bind these prefixes to different namespaces.

In addition to the prefixes in the above list, this document uses the following namespace prefixes to represent the namespace URIs with which they are listed. These namespace prefixes are not predeclared and their use in this document is not normative.

2.1 Types

2.1.1 SequenceType Syntax

Ref Symbol   Expression Options
[5] ItemType ::= KindTest \| AnyItemTest \| FunctionTest \| MapTest \| ArrayTest \| LocalUnionType \| EnumerationType \| RecordTest \| TypeAlias \| AtomicOrUnionType \| ParenthesizedItemType  
[6] AnyItemTest ::= "item" "(" ")"  
[14] SequenceTypeList ::= SequenceType ("," SequenceType)*  
[15] TypedFunctionTest ::= "function" "(" SequenceTypeList? ")" "as" SequenceType  
[16] LocalUnionType ::= "union" "(" ItemType ("," ItemType)* ")"  
[17] TypedMapTest ::= "map" "(" ItemType "," SequenceType ")"  
[18] SingleType ::= (LocalUnionType | SimpleTypeName) "?"?  

Using SequenceTypeList in TypedFunctionTest follows the grammar production pattern of using ParamList in FunctionCall. This is done to make it easier to differentiate the parameter types from the return type.

2.1.2 SequenceType Matching

2.1.2.1 Local Union Types
Ref Symbol   Expression Options
[16] LocalUnionType ::= "union" "(" ItemType ("," ItemType)* ")"  
[17] TypedMapTest ::= "map" "(" ItemType "," SequenceType ")"  
[18] SingleType ::= (LocalUnionType | SimpleTypeName) "?"?  

The LocalUnionType is a new XPath 4.0 Editor’s Draft item type supported by Saxon 9.8.

A LocalUnionType defines a union type whose members are the ItemType types listed in the type definition. These types are restricted to being generalized atomic types (that is, they cannot be list, union, or other complex types).

If a member type has a namespace prefix, the namespace prefix is resolved to a namespace URI using the statically known namespacesXQ31. If the member type has no namespace prefix, it is implicitly qualified by the default type namespaceXQ31.

Example:

xs:numeric can be specified in its expanded form as:

1 instance of union(xs:float, xs:double, xs:decimal)
2.1.2.2 Tuple Type
Ref Symbol   Expression Options
[25] RecordTest ::= ( "tuple" | "record" ) "(" FieldDeclaration ("," FieldDeclaration)* ExtensibleFlag? ")"  
[26] FieldDeclaration ::= FieldName "?"? ( ( ":" | "as" ) (SequenceType | SelfReference) )?  
[33] FieldName ::= NCName | StringLiteral  
[55] ExtensibleFlag ::= "," "*"  
[56] SelfReference ::= ".." OccurrenceIndicator?  

The RecordTest is a new sequence type supported by Saxon 9.8. Saxon 9.8 uses : to specify the tuple item’s sequence type, while Saxon 10.0 uses as.

In Saxon 9.8, a field name can only be an NCName. In Saxon 10, it can also be a StringLiteral.

In Saxon 9.9, a FieldDeclaration can be optional by adding a ? after the field name.

In Saxon 9.8, field declarations are optional by default (that is, they have a default type of item()*). In Saxon 10.0, they are required by default (that is, they have a default type of item()+). To specify an optional field in Saxon 10.0, the sequence type must be optional (i.e. using either the ? or * occurrence indicator for the specified sequence type).

[Definition: An extensible tuple is a tuple that has some fields specified, but allows other fields to be included in the map object.] An extensible tuple is specified by having the last tuple field be the * wildcard operator. This is supported by Saxon 9.9.

2.1.2.3 Element Test
Ref Symbol   Expression Options
[12] NillableTypeName ::= TypeName "?"  
[13] ElementTest ::= "element" "(" (NameTest ("," (NillableTypeName | TypeName))?)? ")"  

This is a Saxon 10.0 extension. The element tests have been relaxed to support all wildcard forms, not just *.

Example:

$a instance of element(*:thead) and
$b instance of element(xhtml:*, xs:string)
2.1.2.4 Attribute Test
Ref Symbol   Expression Options
[29] AttributeTest ::= "attribute" "(" (NameTest ("," TypeName)?)? ")"  

This is a Saxon 10.0 extension. The attribute tests have been relaxed to support all wildcard forms, not just *.

Example:

$a instance of attribute(xlink:*) and
$b instance of attribute(*:id, xs:ID)
2.1.2.5 Type Alias
Ref Symbol   Expression Options
[34] TypeAlias ::= ( "~" EQName ) | ( "type" "(" EQName ")" )  

This is a Saxon 9.8 extension. This is used to reference XSLT type aliases declared using saxon:type-alias XSLT elements.

Saxon 9.8 uses the ~type syntax, while Saxon 10.0 uses the type(...) syntax.

3 Expressions

Ref Symbol   Expression Options
[9] ExprSingle ::= ForExpr \| LetExpr \| QuantifiedExpr \| IfExpr \| TernaryConditionalExpr  

3.1 FLWOR Expressions

3.1.1 For Member Expressions

Ref Symbol   Expression Options
[7] ForExpr ::= SimpleForClause ReturnClause  
[41] SimpleForClause ::= "for" "member"? SimpleForBinding ("," SimpleForBinding)*  
[8] ReturnClause ::= "return" ExprSingle  

This is a new XPath 4.0 Editor’s Draft extension to for expressions that is supported as a Saxon 10.0 vendor extension. It makes it easier to iterate over the members in an array().

The expression:

for member $m in E return R

is equivalent to:

let $a as array(*) := E
for $i in 1 to (array:size($a) + 1)
let $m := array:get($a, $i)
return R

Note:

This means that the for member expression will produce an XPTY0004 error if E is not a single array.

The expression:

for member $m_1 in E_1, $m_2 in E_2, ..., $m_n in E_n
return R

is equivalent to:

       for member $m_1 in E_1
return for member $m_2 in E_2
...
return for member $m_n in E_n
return R

3.2 Logical Expressions

Ref Symbol   Expression Options
[19] OrExpr ::= AndExpr (("or" \| "orElse") AndExpr)*  
[20] AndExpr ::= ComparisonExpr (("and" \| "andAlso") ComparisonExpr)*  

The orElse and andAlso expressions are new logical expression supported by Saxon 9.9.

The orElse expression evaluates the left hand side (lhs) first, and only evaluates the right hand side (rhs) if the left hand side is false. This is equivalent to:

if (lhs) then fn:true() else xs:boolean(rhs)

The andAlso expression evaluates the left hand side (lhs) first, and only evaluates the right hand side (rhs) if the left hand side is true. This is equivalent to:

if (lhs) then xs:boolean(rhs) else fn:false()

3.3 Conditional Expressions

Ref Symbol   Expression Options
[10] TernaryConditionalExpr ::= OrExpr "??" TernaryConditionalExpr "!!" TernaryConditionalExpr  

The TernaryConditionalExpr expression is a new expressions defined in proposal 2 of the EXPath syntax extensions for XPath and XQuery.

Given the TernaryConditionalExpr:

C ?? A !! B

the equivalent IfExpr is:

if (C) then A else B

3.3.1 Otherwise Expressions

Ref Symbol   Expression Options
[31] MultiplicativeExpr ::= OtherwiseExpr ( ("*" | "div" | "idiv" | "mod") OtherwiseExpr )*  
[32] OtherwiseExpr ::= UnionExpr ( "otherwise" UnionExpr )*  

The OtherwiseExpr expression is a new XPath 4.0 Editor’s Draft expression supported as a Saxon 10.0 vendor extension that returns the first non-empty sequence in the otherwise expression.

For two items or empty sequences A and B, the expression A otherwise B is equivalent to:

(A, B)[1]

Otherwise, if either A or B have more than one item, the expression A otherwise B is equivalent to:

let $a := A
return if (exists($a)) then $a else B

Note:

For sequences with more than one item (A, B)[1] will only return the first item in the non-empty sequence, not the entire sequence. This is why the more complicated expression is needed for that case.

3.4 Primary Expressions

Ref Symbol   Expression Options
[36] PrimaryExpr ::= Literal \| VarRef \| ParamRef \| ParenthesizedExpr \| ContextItemExpr \| FunctionCall \| FunctionItemExpr \| MapConstructor \| ArrayConstructor \| UnaryLookup  
[23] FunctionItemExpr ::= NamedFunctionRef \| InlineFunctionExpr \| ContextItemFunctionExpr \| LambdaFunctionExpr  

3.4.1 Inline Function Expressions

Ref Symbol   Expression Options
[22] ParamList ::= ParamList ::= Param ("," Param)* "..."?  

[Definition: Variadic function arguments match zero or more arguments at the end of the non-variadic arguments.] Variadic function arguments are supported in proposal 1, version 2 of the EXPath syntax extensions for XPath and XQuery.

When ... is added after the last parameter in a parameter list, that parameter contains the arguments passed after the previous parameter as an array. If the variadic parameter has a type, the elements in that array have that type.

3.4.1.1 Context Item Function Expressions
Ref Symbol   Expression Options
[24] ContextItemFunctionExpr ::= (( "fn" "{" ) | ".{" ) Expr "}"  

This is a Saxon 9.8 extension. It is a syntax variant of the focus function alternative for inline functions in proposal 5 of the EXPath syntax extensions for XPath and XQuery.

The expressions fn{E} (from Saxon 9.8) and .{E} (from Saxon 10.0) are equivalent to:

function ($arg as item()) as item()* { $arg ! (E) }
3.4.1.2 Lambda Function Expressions
Ref Symbol   Expression Options
[35] LambdaFunctionExpr ::= "_" "{" Expr "}"  
[37] ParamRef ::= "$" Digits  

This is a Saxon 10.0 extension. This is a simplified syntax for inline function expressions.

The parameters of a lambda function expression are not defined at the function signature. Instead, they are referenced in the expression itself using the $number syntax.

Example:

The lambda function _{ $1 + $2 } is equivalent to the inline function expression:

function ($arg1 as item()*, $arg2 as item()*) as item()* {
  $arg1 + $arg2
}

The arity of the lambda function is the maximum value of the ParamRef primary expressions in the lambda function body expression.

Example:

The lambda function _{ $2 mod 2 = 0 } has an arity of 2.

Note:

If there are no ParamRef primary expressions in the lambda function body expression, the lambda function has an arity of 0.

3.5 Arrow Operator (=>)

Ref Symbol   Expression Options
[27] ArrowExpr ::= UnaryExpr ( FatArrowTarget | ThinArrowTarget )*  
[40] FatArrowTarget ::= "=>" ( ArrowFunctionCall | ArrowDynamicFunctionCall )  
[53] ThinArrowTarget ::= "->" ( ArrowFunctionCall | ArrowDynamicFunctionCall | ArrowInlineFunctionCall )  
[28] ArrowFunctionCall ::= EQName ArgumentList  
[38] ArrowDynamicFunctionCall ::= ( VarRef \| ParamRef \| ParenthesizedExpr ) PositionalArgumentList  
[54] ArrowInlineFunctionCall ::= EnclosedExpr  
[11] PositionalArgumentList ::= "(" PositionalArguments? ")"  
[59] PositionalArguments ::= Argument ("," Argument)*  

The ParamRef is for Lambda Function Expressions support in Saxon 10.0.

The ThinArrowTarget is a new XPath 4.0 Editor’s Draft extension that applies the function to each item in UnaryExpr.

A XQuery IntelliJ Plugin Grammar

A.1 EBNF for XPath 3.1 with Vendor Extensions

This EBNF grammar includes and modifies the EBNF grammar for the following specifications:

  1. XPath 3.1 (W3C Recommendation 21 March 2017).

When an EBNF symbol is modified in an extension, that modified symbol is used in preference to the base symbol definition. When the symbol is modified below, that modified symbol is used in preference to the base or extension symbol definition. When the EBNF symbol is a list such as PrimaryExpr, the result used is the combination of the definitions of that symbol across all specifications, including this document.

The EBNF symbols below only include new and modified symbols.

These changes include support for:

  1. XPath 4.0 Editor’s Draft 12 December 2020 syntax;
  2. Saxon Vendor Extensions.
Ref Symbol   Expression Options
[1] QuantifiedExpr ::= ("some" \| "every") QuantifierBinding ("," QuantifierBinding)* "satisfies" ExprSingle  
[2] QuantifierBinding ::= "$" VarName "in" ExprSingle  
[3] Wildcard ::= WildcardIndicator \| (NCName ":" WildcardIndicator) \| (WildcardIndicator ":" NCName) \| (BracedURILiteral WildcardIndicator) /* ws: explicit */
[4] WildcardIndicator ::= "*"  
[5] ItemType ::= KindTest \| AnyItemTest \| FunctionTest \| MapTest \| ArrayTest \| LocalUnionType \| EnumerationType \| RecordTest \| TypeAlias \| AtomicOrUnionType \| ParenthesizedItemType  
[6] AnyItemTest ::= "item" "(" ")"  
[7] ForExpr ::= SimpleForClause ReturnClause  
[8] ReturnClause ::= "return" ExprSingle  
[9] ExprSingle ::= ForExpr \| LetExpr \| QuantifiedExpr \| IfExpr \| TernaryConditionalExpr  
[10] TernaryConditionalExpr ::= OrExpr "??" TernaryConditionalExpr "!!" TernaryConditionalExpr  
[11] PositionalArgumentList ::= "(" PositionalArguments? ")"  
[12] NillableTypeName ::= TypeName "?"  
[13] ElementTest ::= "element" "(" (NameTest ("," (NillableTypeName | TypeName))?)? ")"  
[14] SequenceTypeList ::= SequenceType ("," SequenceType)*  
[15] TypedFunctionTest ::= "function" "(" SequenceTypeList? ")" "as" SequenceType  
[16] LocalUnionType ::= "union" "(" ItemType ("," ItemType)* ")"  
[17] TypedMapTest ::= "map" "(" ItemType "," SequenceType ")"  
[18] SingleType ::= (LocalUnionType | SimpleTypeName) "?"?  
[19] OrExpr ::= AndExpr (("or" \| "orElse") AndExpr)*  
[20] AndExpr ::= ComparisonExpr (("and" \| "andAlso") ComparisonExpr)*  
[21] FunctionSignature ::= "(" ParamList? ")" TypeDeclaration?  
[22] ParamList ::= ParamList ::= Param ("," Param)* "..."?  
[23] FunctionItemExpr ::= NamedFunctionRef \| InlineFunctionExpr \| ContextItemFunctionExpr \| LambdaFunctionExpr  
[24] ContextItemFunctionExpr ::= (( "fn" "{" ) | ".{" ) Expr "}"  
[25] RecordTest ::= ( "tuple" | "record" ) "(" FieldDeclaration ("," FieldDeclaration)* ExtensibleFlag? ")"  
[26] FieldDeclaration ::= FieldName "?"? ( ( ":" | "as" ) (SequenceType | SelfReference) )?  
[27] ArrowExpr ::= UnaryExpr ( FatArrowTarget | ThinArrowTarget )*  
[28] ArrowFunctionCall ::= EQName ArgumentList  
[29] AttributeTest ::= "attribute" "(" (NameTest ("," TypeName)?)? ")"  
[30] InlineFunctionExpr ::= (("function" FunctionSignature) | ("->" FunctionSignature?)) FunctionBody  
[31] MultiplicativeExpr ::= OtherwiseExpr ( ("*" | "div" | "idiv" | "mod") OtherwiseExpr )*  
[32] OtherwiseExpr ::= UnionExpr ( "otherwise" UnionExpr )*  
[33] FieldName ::= NCName | StringLiteral  
[34] TypeAlias ::= ( "~" EQName ) | ( "type" "(" EQName ")" )  
[35] LambdaFunctionExpr ::= "_{" Expr "}"  
[36] PrimaryExpr ::= Literal \| VarRef \| ParamRef \| ParenthesizedExpr \| ContextItemExpr \| FunctionCall \| FunctionItemExpr \| MapConstructor \| ArrayConstructor \| UnaryLookup  
[37] ParamRef ::= "$" Digits  
[38] ArrowDynamicFunctionCall ::= ( VarRef \| ParamRef \| ParenthesizedExpr ) PositionalArgumentList  
[39] LetExpr ::= SimpleLetClause ReturnClause  
[40] FatArrowTarget ::= "=>" ( ArrowFunctionCall | ArrowDynamicFunctionCall )  
[41] SimpleForClause ::= "for" "member"? SimpleForBinding ("," SimpleForBinding)*  
[42] PathExpr ::= ("/" RelativePathExpr?) \| (AbbrevDescendantOrSelfStep RelativePathExpr) \| RelativePathExpr /* xgc: leading-lone-slash */
[43] RelativePathExpr ::= StepExpr (("/" \| AbbrevDescendantOrSelfStep) StepExpr)*  
[44] AbbrevDescendantOrSelfStep ::= "//"  
[45] PostfixExpr ::= FilterExpr \| DynamicFunctionCall \| PostfixLookup \| PrimaryExpr  
[46] FilterExpr ::= PostfixExpr Predicate  
[47] DynamicFunctionCall ::= PostfixExpr PositionalArgumentList  
[48] PostfixLookup ::= PostfixExpr Lookup  
[49] AxisStep ::= FilterStep \| ReverseStep \| ForwardStep  
[50] FilterStep ::= AxisStep Predicate  
[51] ParenthesizedExpr ::= EmptyExpr | ( "(" Expr ")" )  
[52] EmptyExpr ::= "(" ")"  
[53] ThinArrowTarget ::= "->" ( ArrowFunctionCall | ArrowDynamicFunctionCall | ArrowInlineFunctionCall )  
[54] ArrowInlineFunctionCall ::= EnclosedExpr  
[55] ExtensibleFlag ::= "," "*"  
[56] SelfReference ::= ".." OccurrenceIndicator?  
[57] KeySpecifier ::= NCName | IntegerLiteral | StringLiteral | VarRef | ParenthesizedExpr | "*"  
[58] EnumerationType ::= "enum" "(" StringLiteral ("," StringLiteral)* ")"  
[59] PositionalArguments ::= Argument ("," Argument)*  
[60] ArgumentList ::= "(" ((PositionalArguments ("," KeywordArguments)?) | KeywordArguments)? ")"  
[61] KeywordArguments ::= KeywordArgument ("," KeywordArgument)*  
[62] KeywordArgument ::= NCName ":" ExprSingle  
[63] WithExpr ::= "with" NamespaceDeclaration ("," NamespaceDeclaration)* EnclosedExpr  
[64] NamespaceDeclaration ::= QName "=" URILiteral  

A.2 Reserved Function Names

keyword XPath Version
attribute XPath 2.0
comment XPath 1.0
document-node XPath 2.0
element XPath 2.0
empty-sequence XPath 2.0
if XPath 2.0
item XPath 2.0
node XPath 1.0
processing-instruction XPath 1.0
schema-attribute XPath 2.0
schema-element XPath 2.0
text XPath 1.0
typeswitch XPath 2.0

B References

B.1 W3C References

Core Specifications

W3C Language Extensions

XML Schema

Working Drafts

B.2 MarkLogic References

B.3 EXPath References

XPath NG

B.4 Saxon References

Saxon Documentation

Papers:

C Vendor Extensions

C.1 Saxon Vendor Extensions

The Saxon XQuery Processor supports the following vendor extensions described in this document:

  1. Tuple Type [Saxon 9.8]
  2. Type Alias [Saxon 9.8]
  3. Logical Expressions [Saxon 9.9] – orElse and andAlso
  4. Otherwise Expressions [Saxon 10.0]
  5. For Member Expressions [Saxon 10.0]

Saxon implements the following EXPath Syntax Extensions:

  1. Local Union Types [Saxon 9.8]
  2. Context Item Function Expressions [Saxon 9.8]
  3. Lambda Function Expressions [Saxon 10.0]
  4. Element Test and Attribute Test [Saxon 10.0] – wildcard names