ShExPath
ShExPath is a path language for the elements of a ShEx schema. Elements are shape expressions, triple constraints, node constraints and their value sets, semantic actions and annotations. A path addresses a schema, not a graph. It selects a list of schema elements. An application can use them to report a validation result, to attach information, or to find an element in a large schema.
rudof implements the language as the specification defines it, from the command line, the Rust
library, Python and JavaScript.
The model
A path is evaluated over the ShExJ form of the schema, the JSON document that ShExJ writes down. An item is any node of that document:
- An object: the schema, a
ShapeDecl, a shape expression, a triple expression, aSemAct, anAnnotation, a value set value and others. - An array:
shapes,expressions,extendsand others. An array is an item itself, soextendsselects the array andextends/*selects its members. - an atomic value: an IRI, a string, a number or a boolean. A reference to a shape is an IRI, not the shape it names.
rudof reports each selected item with its location, a
JSON Pointer into the ShExJ document, such as
/shapes/0/shapeExpr/expression/expressions/1, and its value, the item as ShExJ. The items come
in the order the path selects them, with their duplicates.
The language at a glance
The examples use the example schema of the specification:
PREFIX : <http://ex.example/ns#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
BASE <http://ex.example/schema>
<#IssueShape> {
:name xsd:string MINLENGTH 4 ;
:category ["bug" "feature request"] ;
$<#reportedBy> :postedBy @<#UserShape> ;
:processing {
:reproduced [true false] ;
:priority xsd:integer
} ?
}
<#UserShape> IRI /User\?id=[0-9]+/ {
( foaf:name xsd:string
| foaf:givenName xsd:string + ;
foaf:familyName xsd:string
) ;
foaf:mbox IRI
}
<#AgentShape> { foaf:mbox IRI }
<#EmployeeShape> EXTENDS @<#UserShape> EXTENDS @<#AgentShape> {
:employeeId xsd:integer
}
| ShExPath | Selects |
|---|---|
@<#IssueShape> | The shape expression declared as #IssueShape. |
@<#IssueShape>~:category | The :category triple constraint in it. |
$<#reportedBy> | The :postedBy triple constraint, by the label given to it. |
$<#reportedBy>/valueExpr | The reference to #UserShape in it: an IRI. |
@<#UserShape>~foaf:mbox | The foaf:mbox constraint, through the ShapeAnd that IRI /re/ {…} is. |
/shapes/* | The four shape declarations. |
@<#IssueShape>/expression/expressions/*[1] | The second constraint of #IssueShape, :category. |
@<#IssueShape>/expression/expressions/*[predicate = :name] | The :name constraint. |
@<#IssueShape>/expression/expressions/*[assert count() = 4] | The four constraints, and an error if there are not four. |
@<#IssueShape>~:category/ancestor::Shape | The shape that constraint is in. |
@<#EmployeeShape>/extends/*[0] | The reference to #UserShape in the EXTENDS list. |
@<#IssueShape>~:name , @<#IssueShape>~:category | Both constraints. |
The parts of a path:
- Steps are separated by
/and applied left to right, each to every item the previous one selected. A child step names a ShExJ property (shapes,expression,valueExpr, …) or is*, every property value of an object or member of an array. - Axes:
self::,parent::,ancestor::,thisShapeExpr::andthisTripleExpr::.thisShapeExpr::selects the item and the shape expressions that aShapeAnd,ShapeOrorShapeNotcombines.thisTripleExpr::selects the item and the triple expressions that anEachOforOneOfgroups, including inclusions. - Term types keep the items of a ShExJ type:
Shape,TripleConstraint,NodeConstraint, … - Filters in brackets:
[N](the item at positionN, from 0),[index() = N],[count() = N],[path],[path = value]and[assert ...]. - Shortcuts:
@label(a shape expression),~iri(the triple constraints on a predicate in this shape) and$label(a labelled triple expression). - Junctions:
,(every item of each operand, keeping duplicates),union(without duplicates) andintersection.
A path uses the prefixes and base of the schema for its IRIs. In the example above, :category and <#IssueShape> resolve as in the schema.
Using ShExPath
From the command line
The shex-path command prints what a path selects as JSON:
rudof shex-path -s issue.shex -p "@<#IssueShape>~:category"
From Rust
Rudof::evaluate_shex_path evaluates a path against the loaded schema and returns a list of
ShExPathMatch, each with its location and value:
let matches = rudof.evaluate_shex_path("@<#IssueShape>~:category").execute()?;
for m in matches {
println!("{} {}", m.location, m.value);
}
The shape_path crate implements the language, and shex_ast::shexpath re-exports it. ShExPathParser reads a path. ShExJDocument evaluates it over a schema, also from other items, for relative paths. shex_ast::shexpath::shexj_document makes the document from a parsed schema.
From Python
from pyrudof import Rudof
with Rudof() as rudof:
rudof.read_shex(schema)
for match in rudof.evaluate_shex_path("@<#IssueShape>~:category"):
print(match["location"], match["value"]["predicate"])
From JavaScript
const rudof = new Rudof();
rudof.readShex(schema);
for (const { location, value } of rudof.evaluateShexPath("@<#IssueShape>~:category")) {
console.log(location, value.predicate);
}
In every case a path that is not a ShExPath, or an assert that does not hold, is an error
(ShExError in Python and JavaScript).
Points the specification leaves open
The specification marks some points as unsettled. rudof follows the text of the specification
and, where it is silent, the reference implementation,
ShapePath.js:
| Point | What rudof does |
|---|---|
A leading / | Has no effect on where the path begins: /shapes/* from an item that is not the schema selects nothing. |
// | The same as /. |
< and > | Are parsed, and select nothing. |
A parenthesized step, (path) | Is parsed, and evaluating it is an error, as in the reference implementation. |
union and intersection | Compare items by value, as the specification says: two identical constraints in different shapes are one item to them. |
| The members of an object | JSON objects have no order. * over an object gives its members in the order of their keys. |
A blank node label after @, ~ or $ | Is not accepted, as the grammar takes only an IRI. |
The ShExJ document is the ShExJ output of rudof for the schema, with two changes. It has no prefixmap and base keys, which rudof adds. It has "type": "SemAct" on every semantic action.
ShExPath addresses a schema. Evaluating a path against data is not supported.
ShExPaths in validation results
If a node does not conform to a shape, the ShEx validation result uses ShExPaths to name the failing parts of the schema. Each line that names a triple constraint ends with (at PATH). The JSON result has the paths in the path and paths fields of each error. See Reading the results.
The validation writes the paths with the shortcuts, and with the prefixes of the schema:
| Path | Names |
|---|---|
@:Person | The shape declared as :Person. |
@:Person~:age | The triple constraint on :age in that shape. |
@:Product~:code/self::[1] | The second triple constraint on :code, when the shape has more than one. The first one is /self::[0]. |
$:knowsLabel | The triple constraint with the label :knowsLabel. |
@:Order~:item/valueExpr | The anonymous shape in the triple constraint on :item. |
@:Contact/shapeExprs/*[0] | The first operand of the AND or OR that :Contact is. |
@:N/shapeExpr | The shape expression that the NOT of :N negates. |
/start~:p | The triple constraint on :p in the start shape expression. |
Each path selects exactly one item when you evaluate it against the schema:
rudof shex-path -s schema.shex -p "@:Product~:code/self::[1]"
A shape in an imported schema is also named by its label. The shex-path command evaluates a path against the ShExJ form of one schema only, so evaluate the path against the imported schema. A ShExPath cannot name a blank node label. So validation results use no paths for the shapes inside a shape with a blank node label.