问题
How to get the list of URIs under a Namespace in RDFlib?
For example, we can do:
from rdflib.namespace import FOAF
But how do we find out what URIs are available in FOAF?
In fact, there are several namespaces (RDF, RDFS, FOAF, ...), and I find it difficult to explore which URIs are within each of them, and which one to use. Say, if I want to express a belonging relationship, should I use RDFS.member or something else? Is there a standard for linked data or it's still ad-hoc?
I'm new to RDFs and sorry if this appears to be an obvious question.
Thanks!
回答1:
IRIs are simply opaque identifiers. While we speak of Linked Data and the Semantic Web, the semantics of rdfs:member
the meaning assigned to a property is defined outside of the Semantic Web (hopefully by good documentation, which ought, of course, to be associated with the property as an rdfs:comment
, or the like), and by usage. For rdfs:member
, the description comes from the RDFS recommendation:
5.1.6 rdfs:member
rdfs:member is an instance of rdf:Property that is a super-property of all the container membership properties i.e. each container membership property has an rdfs:subPropertyOf relationship to the property rdfs:member.
The rdfs:domain of rdfs:member is rdfs:Resource. The rdfs:range of rdfs:member is rdfs:Resource.
The container membership properties are described in the same document:
rdfs:ContainerMembershipProperty
The rdfs:ContainerMembershipProperty class has as instances the properties rdf:_1, rdf:_2, rdf:_3 ... that are used to state that a resource is a member of a container. rdfs:ContainerMembershipProperty is a subclass of rdf:Property. Each instance of rdfs:ContainerMembershipProperty is an rdfs:subPropertyOf the rdfs:member property.
Given a container C, a triple of the form:
C rdf:_nnn O
where nnn is the decimal representation of an integer greater than 0 with no leading zeros, states that O is a member of the container C.
Container membership properties may be applied to resources other than containers.
I was actually a bit surprised to read that last line, but since the membership properties and rdfs:member
can be applied to resources other than containers, it seems like it would be acceptable to use them on your own data structures.
Even so, it might be more appropriate to define your own more specific property and simply assert that it's a subproperty of rdfs:member
. For instance, you might specify that a politics:memberStateOf
property relating a nation state to some multi-national union is an rdfs:subPropertyOf rdfs:member
.
Consulting the documentation of vocabularies is a good idea in other situations too. For instance, the examples on the RDFlib page do include some use of FOAF:
from rdflib import RDF
...
# Create a namespace object for the Friend of a friend namespace.
FOAF = Namespace("http://xmlns.com/foaf/0.1/")
...
# Add triples using store's add method.
store.add((donna, RDF.type, FOAF["Person"]))
store.add((donna, FOAF["nick"], Literal("donna", lang="foo")))
The way that FOAF is being used here (which is a bit different than in your question, perhaps there's a library for it now) with strings being passed in depends on being aware of what URIs should exist, and that information is available in the FOAF specification. Although it appears that the RDF
Namespace has some symbolic constants defined (e.g., RDF.type
), the documentation says that RDF.type
is actually equivalent to RDF['type']
:
Fully qualified URIs in the namespace can be constructed either by attribute or by dictionary access on Namespace instances:
>>> owl.seeAlso rdflib.term.URIRef(u'http://www.w3.org/2002/07/owl#seeAlso') >>> owl['seeAlso'] rdflib.term.URIRef(u'http://www.w3.org/2002/07/owl#seeAlso')
来源:https://stackoverflow.com/questions/18047962/rdflib-get-all-uris-under-a-namespace