A Python library for querying the linked data web.
This is a library for querying linked data on the web using a simple to learn path language based on the Fresnel Selector Language. A LinkPath is evaluated against the whole web, fetching the data it needs by following links between items.
Install with easy_install:
>sudo easy_install linkpath
If you already have it installed then simply upgrade with:
>sudo easy_install --upgrade linkpath
The basic pattern of usage is as follows:
from linkpath import LinkPathProcessor wp = LinkPathProcessor results = wp.select('http://example.com/res/person1', "foaf:knows/*/foaf:givenName/text()") for res in results: print res
A LinkPath looks like this:
LinkPaths are applied to a starting resource, specified by supplying a URI.
Path steps are separated by slashes. The first step specifies the name of an property to select from the starting resource, the second step specifies criteria for the values of that property. The third step specifies criteria for slecting properties of those values. The last step also specifies what is actually selected by the LinkPath. You can read the LinkPath above as selecting the names of all the people known by the starting URI.
The LinkPath always alternates between property and value (or arc and node):
Every time the LinkPath processor encounters a node it looks up more data about that node. At the moment this is just a simple HTTP GET but in the future I plan to add other mechanisms. Any RDF retrieved by this lookup is added to the pool of data the processor is using to evaluate the LinkPath.
Filters can be applied to each step to further refine it:
See the examples and specification for more details on filters.
Find all the friends of the starting resource:
Find all the friends who are people:
Find the given name of all friends:
Find the given name of friends whose family name is Smith:
Find all friends who are aged 32 (these are equivalent):
The previous two paths use esact text matching so will fail if the foaf:age property contains '032', '+32' or '32.0'. Numeric comparison is better:
The usual numeric comparison operators are available:
foaf:knows/*[foaf:age/text()>32] foaf:knows/*[foaf:age/text()<=32] foaf:knows/*[foaf:age/text()!=32]
Find friends whose given name is the same as their nickname
Find friends who have more than five friend
foaf:knows/*[count(foaf:knows/*) > 5]
Find friends who have a foaf:based_near property:
Find friends who don't have a foaf:based_near property:
Find friends who have both a foaf:givenName and a foaf:familyName:
foaf:knows/*[foaf:givenName and foaf:familyName]
Find friends who have either a foaf:givenName or a foaf:familyName:
foaf:knows/*[foaf:givenName or foaf:familyName]
Find friends whose family name begins with 'S'
Find all the people related to the starting resource by a property that is a subproperty of foaf:knows
Find all the properties of the starting resource in the FOAF namespace:
*[namespace-uri(.) = 'http://xmlns.com/foaf/0.1/']
Find all the properties of the starting resource that have a local name of label:
*[local-name(.) = 'label']
Using linkpath command line
The linkpath package comes with a command line utility. Use it from the command line like this:
linkpath [uri] [path]
Simply pass it a starting URI and a valid LinkPath and it will fetch the necessary data and evaluate the path expression, printing out the results it finds.
Currently it only has mappings for the rdf, rdfs, owl, foaf and geo namespaces. I'll add a command line switch to specify more soon.
As an example, here's how to find who I know that went to Harvard University:
linkpath http://iandavis.com/id/me "foaf:knows/*[foaf:schoolHomepage/*[uri(.)='http://www.harvard.edu/']]/foaf:name/text()"
It takes a minute or so to run because there's no caching yet.
The LinkPath Language Specification
The LinkPath specification is adapted from the Fresnel Selector Language.
This work is hereby released into the Public Domain.
To view a copy of the public domain dedication, visit http://creativecommons.org/licenses/publicdomain or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.