/a/b/c\[2]/d\[%]/\%/e\[_]
The JCR 1.0 specification uses the XPath query language because node structures in JCR are very analogous to the structure of an XML document. Thus, XPath provides a useful language for selecting and searching workspace content. And since JCR 1.0 defines a mapping between XML and a workspace view called the "document view", adapting XPath to workspace content is quite natural.
A JCR XPath query specifies the subset of nodes in a workspace that satisfy the constraints defined in the query. Constraints can limit the nodes in the results to be those nodes with a specific (primary or mixin) node type, with properties having particular values, or to be within a specific subtree of the workspace. The query also defines how the nodes are to be returned in the result sets using column specifiers and ordering specifiers.
ModeShape offers a bit more functionality in the "jcr:contains(...)" clauses than required by the specification. In particular, the second parameter specifies the search expression, and for these ModeShape accepts full-text search language expressions, including wildcard support.
As an aside, ModeShape actually implements XPath queries by transforming them into the equivalent JCR-SQL2 representation. And the JCR-SQL2 language, although often more verbose, is much more capable of representing complex queries with multiple combinations of type, property, and path constraints.
JCR 1.0 specifies that support is required only for returning column values based upon single-valued, non-residual properties that are declared on or inherited by the node types specified in the type constraint. ModeShape follows this requirement, and does not specifying residual properties. However, ModeShape does allow multi-valued properties to be specified as result columns. And as per the specification, ModeShape always returns the "jcr:path" and "jcr:score" pseudo-columns.
ModeShape uses the last location step with an attribute axis to specify the properties that are to be returned as result columns. Multiple properties are specified with a union. For example, the following table shows several XPath queries and how they map to JCR-SQL2 queries.
XPath |
JCR-SQL2 |
//* |
SELECT * FROM [nt:base] |
//element(*,my:type) |
SELECT * FROM [my:type] |
//element(*,my:type)/@my:title |
SELECT [my:title] FROM [my:type] |
//element(*,my:type)/(@my:title | @my:text) |
SELECT [my:title], [my:text] FROM [my:type] |
//element(*,my:type)/(@my:title union @my:text) |
SELECT [my:title], [my:text] FROM [my:type] |
Specifying result set columns
JCR 1.0 specifies that support is required only for specifying constraints of one primary type, and it is optional to support specifying constraints on one (or more) mixin types. The specification also defines that the XPath element test be used to test against node types, and that it is optional to support element tests on location steps other than the last one. Type constraints are inherently inheritance-sensitive, in that a constraint against a particular node type 'X' will be satisfied by nodes explicitly declared to be of type 'X' or of subtypes of 'X'.
ModeShape does support using the element test to test against primary or mixin type. ModeShape also only supports using an element test on the last location step. For example, the following table shows several XPath queries and how they map to JCR-SQL2 queries.
XPath |
JCR-SQL2 |
//* |
SELECT * FROM [nt:base] |
//element(*,my:type) |
SELECT * FROM [my:type] |
/jcr:root/nodes/element(*,my:type) |
SELECT * FROM [my:type] |
/jcr:root/nodes//element(*,my:type) |
SELECT * FROM [my:type] |
/jcr:root/nodes//element(ex:nodeName,my:type) |
SELECT * FROM [my:type] |
Specifying type constraints
Note that the JCR-SQL2 language supported by ModeShape is far more capable of joining multiple sets of nodes with different type, property and path constraints.
JCR 1.0 specifies that attribute tests on the last location step is required, but that predicate tests on any other location steps are optional.
ModeShape does support using attribute tests on the last location step to specify property constraints, as well as supporting axis and filter predicates on other location steps. For example, the following table shows several XPath queries and how they map to JCR-SQL2 queries.
XPath |
JCR-SQL2 |
//*[] |
SELECT * FROM [nt:base] |
//element(*,my:type)[@prop1] |
SELECT * FROM [my:type] |
//element(*,my:type)[@prop1=xs:boolean('true')] |
SELECT * FROM [my:type] |
//element(*,my:type)[@id<1 and @name='john'] |
SELECT * FROM [my:type] |
//element(*,my:type)[a/b/@id] |
SELECT * FROM [my:type] |
//element(,my:type)[./{}{}/*/@id] |
SELECT * FROM [my:type] |
//element(*,my:type)[.//@id] |
SELECT * FROM [my:type] |
Specifying property constraints
Section 6.6.3.3 of the JCR 1.0 specification contains an in-depth description of property value constraints using various comparison operators.
JCR 1.0 specifies that exact, child node, and descendants-or-self path constraints be supported on the location steps in an XPath query.
ModeShape does support the four kinds of path constraints. For example, the following table shows several XPath queries and how they map to JCR-SQL2 queries.
XPath |
JCR-SQL2 |
/jcr:root/a[1]/b[2] |
SELECT * FROM [nt:base] |
/jcr:root/a/b[*] |
SELECT * FROM [nt:base] |
/jcr:root/a[1]/b[*] |
SELECT * FROM [nt:base] |
/jcr:root/a[2]/b |
SELECT * FROM [nt:base] |
/jcr:root/a/b[2]//c[4] |
SELECT * FROM [my:type] |
/jcr:root/a/b//c//d |
SELECT * FROM [my:type] |
//element(*,my:type)[@id<1 and @name='john'] |
SELECT * FROM [my:type] |
/jcr:root/a/b//element(*,my:type) |
SELECT * FROM [my:type] |
Specifying path constraints
Note that the JCR-SQL2 language supported by ModeShape is capable of representing a wider combination of path constraints, although the XPath expressions are easier to understand and significantly shorter.
Also, path constraints in XPath do not need to specify wildcards for the same-name-sibling (SNS) indexes, as XPath should naturally find all nodes regardless of the SNS index, unless the SNS index is explicitly specified. In other words, any path segment that does not have an explicit SNS index (or an SNS index of '[%]' or '[]') will match _all SNS index values. However, any segments in the path expression that have an explicit numeric SNS index will require an exact match. Thus this path constraint:
/a/b/c\[2]/d\[%]/\%/e\[_]
will effectively be converted into
/a[%]/b[%]/c\[2]/d\[%]/\%/e\[_]
This behavior is very different than how JCR-SQL and JCR-SQL2 path constraints are handled, since these languages interpret a lack of a SNS index as equating to '[1]'. To achieve the XPath-like matching, a query written in JCR-SQL or JCR-SQL2 would need to explicitly include '[%]' in each path segment where an SNS index literal is not already specified.
JCR 1.0 extends the XPath grammar to add support for ordering the results according to the natural ordering of the values of one or more properties on the nodes.
ModeShape does support zero or more ordering specifiers, including whether each specifier is ascending or descending. If no ordering specifiers are defined, the ordering of the results is not predefined and may vary (though ordering by score may be used by default). For example, the following table shows several XPath queries and how they map to JCR-SQL2 queries.
XPath |
JCR-SQL2 |
//element(,) order by @title |
SELECT nodeSet1.title |
//element(,) order by jcr:score() |
SELECT * |
//element(*,my:type) order by jcr:score(my:type) |
SELECT * |
//element(,) order by @jcr:path |
SELECT jcr:path |
//element(,) order by @title, @jcr:score |
SELECT nodeSet1.title |
Specifying result ordering
Note that the JCR-SQL2 language supported by ModeShape has a far richer ORDER BY clause, allowing the use of any kind of dynamic operand, including ordering upon arithmetic operations of multiple dynamic operands.
JCR 1.0 defines a number of other optional and required features, and these are summarized in this section.
Only abbreviated XPath syntax is supported.
Only the child axis (the default axis, represented by '/' in abbreviated syntax), descendant-or-self axis (represented by '//' in abbreviated syntax), self axis (represented by '.' in abbreviated syntax), and attribute axis (represent by '@' in abbreviated syntax) are supported.
The text() node test is not supported.
The element() node test is supported.
The jcr:like() function is supported.
The jcr:contains() function is supported.
The jcr:score() function is supported.
The jcr:deref() function is not supported.