RxPath is a language for querying a RDF model. It is syntactically identical to XPath 1.0 and behaves very similarly.
Compared to other query languages for RDF, it is much more expressive than SQL-like query languages such as SPARQL and even if you are not familar with XPath I think it is more intuitive than other graph-traversal query languages like Versa.
Informally, RxPath is a XPath expression evaluated on a RDF model as if it has been transformed into a XML document as follows:
- the document has a root element for every resource in model, with the element name equal to the name of the rdf:type of the resource.
This RxPath expression returns a nodeset of all the resources in the model:
This expression returns all the resource with the rdf:type of class ex:Document:
- each root element has a child element for each statement the resource is the subject of. The name of each child is name of the property in the statement.
Thus this RxPath expression returns a nodeset of all the predicates in the model (and therefore all the statements):
And this expression returns all the statements with the property dc:title:
- Each of these children have child text node if the object of the statement is a literal or a child element if the object is a resource.
Thus these expressions return the objects of all the statements with the property dc:title:
/*/dc:title/node() (Find all objects)
/*/dc:title/* (Find all objects that are resources)
/*/dc:title/text() (Find all objects that are literals)
- Object elements have the same name and children as the equivalent root element for the resource, thus defining a potentially infinitely recursive tree.
Thus this expression finds the full name of all the people that are editors of a resource:
- RDF collections (rdf:List) are represented by collapsing the recursive list into child predicate elements (named rdf:first):
/*/ex:members/rdf:List/rdf:first (Find the first members of each of the membership lists)
/*/ex:members/rdf:List/rdf:first[last()] (Find the last members of each of the membership lists)
- Additionally, RxPath also defines a few attributes that may appear on predicate elements -- rdf:datatype, xml:lang, rdf:ID, uri, and listID -- and one that must appear on all resource elements (rdf:about). It also defines several new XPath functions. See the RxPathSpec for more information.
RxPath modifies the semantics of XPath in the following ways:
- RxPath defines resource elements (subject and object) as having a string value equal to the resource's RDF URI reference (i.e. the value of its rdf:about attribute)
Find all the statements whose subject is the resource with the URI reference "http://example.com/example#foo":
- But note that string value of a predicate element is unchanged from XPath, thus its string value will be the string value of its child node (the object). This implies:
Find all the statements with a dc:title predicate and an object which a literal equal to "A marvelous thing"
/*/dc:title[.="A marvelous thing"]
Find all the statements with that have the resource "http://example.com/example#bar" as its object:
- RxPath redefines the descendant axis to only follow transitive properties:
Find all the decendant files of a folder named "foldername":
Inverse transitive queries can also be performed using this axis:
Find all the ancestor folder(s) of the file named "filename":
- Similarly, the ancestor axis only follows transitive relations.
A more complicated expression that does the same thing as the previous example but returns a nodeset ordered by the ancestor relation.