You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# use dict notation for things that are not valid Python identifiers, e.g.:
17
+
n['first%20name'] # as rdflib.term.URIRef("http://example.org/first%20name")
19
18
20
-
Note that if a name string is valid for use in an RDF namespace but not valid as a Python identifier, such as '1234', it must be addressed with the "item" syntax (using the "attribute" syntax will raise a Syntax Error).
19
+
These two styles of namespace creation - object attribute and dict - are equivalent and are made available just to allow for valid
20
+
RDF namespaces and URIs that are not valid Python identifiers. This isn't just for syntactic things like spaces, as per
21
+
the example of ``first%20name`` above, but also for Python reserved words like ``class`` or ``while``, so for the URI
22
+
``http://example.org/class``, create it with ``EX['class']``, not ``EX.class``.
21
23
22
-
The ``namespace`` module also defines many common namespaces such as RDF, RDFS, OWL, FOAF, SKOS, PROF, etc.
24
+
Common Namespaces
25
+
-----------------
23
26
24
-
Namespaces can also be associated with prefixes, in a :class:`rdflib.namespace.NamespaceManager`, i.e. using ``foaf`` for ``http://xmlns.com/foaf/0.1/``. Each RDFLib graph has a :attr:`~rdflib.graph.Graph.namespace_manager` that keeps a list of namespace to prefix mappings. The namespace manager is populated when reading in RDF, and these prefixes are used when serialising RDF, or when parsing SPARQL queries. Additional prefixes can be bound with the :meth:`rdflib.graph.bind` method.
27
+
The ``namespace`` module defines many common namespaces such as RDF, RDFS, OWL, FOAF, SKOS, PROF, etc. The list of the
28
+
namespaces provided grows with user contributions to RDFLib.
29
+
30
+
These Namespaces, and any others that users define, can also be associated with prefixes using the :class:`rdflib.namespace.NamespaceManager`, e.g. using ``foaf`` for ``http://xmlns.com/foaf/0.1/``.
31
+
32
+
Each RDFLib graph has a :attr:`~rdflib.graph.Graph.namespace_manager` that keeps a list of namespace to prefix mappings. The namespace manager is populated when reading in RDF, and these prefixes are used when serialising RDF, or when parsing SPARQL queries. Prefixes can be bound with the :meth:`rdflib.graph.bind` method::
33
+
34
+
from rdflib import Graph, Namespace
35
+
from rdflib.namespace import FOAF
36
+
37
+
EX = Namespace("http://example.org/")
38
+
39
+
g = Graph()
40
+
g.bind("foaf", FOAF) # bind an RDFLib-provided namespace to a prefix
41
+
g.bind("ex", EX) # bind a user-declared namespace to a prefix
42
+
43
+
44
+
The :meth:`rdflib.graph.bind` method is actually supplied by the :class:`rdflib.namespace.NamespaceManager` class - see next.
25
45
26
46
NamespaceManager
27
47
----------------
28
48
49
+
Each RDFLib graph comes with a :class:`rdflib.namespace.NamespaceManager` instance in the `namespace_manager` field; you can use the `bind` method of this instance to bind a prefix to a namespace URI,
50
+
as above, however note that the `NamespaceManager` automatically performs some bindings according to a selected strategy.
51
+
52
+
Namespace binding strategies are indicated with the `bind_namespaces` input parameter to `NamespaceManager` instances
53
+
and may be set via ``Graph`` also::
54
+
55
+
from rdflib import Graph
56
+
from rdflib.namespace import NamespaceManager
57
+
58
+
g = Graph(bind_namespaces="rdflib") # bind via Graph
59
+
60
+
g2 = Graph()
61
+
nm = NamespaceManager(g2, bind_namespaces="rdflib") # bind via NamespaceManager
62
+
29
63
30
-
Each graph comes with a `NamespaceManager`__ instance in the `namespace_manager` field; you can use the `bind` method of this instance to bind a prefix to a namespace URI::
0 commit comments