Force rdflib to define a namespace

5xum

I am starting to use the rdflib library for dealing with rdf data in Python. At the moment, I want to create a .n3 file of some rdf graph that looks like this:

@prefix ns1: <http://some.namespace/with/name#> .
@prefix ns2: <http://second.namespace/some/name#>
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

<my_example> ns1:annotated_with [ ns1:annotation
        ns2: annotation_value> ] ;
    ns1:name "myname" .

That is, there is one example in this graph which is called my_example, which has a name "myname". This example is annotated with an object whose annotation value is annotation value. I wanted to construct this example in Python like so:

import rdflib

gg=rdflib.graph.Graph()
ns1 = rdflib.Namespace('http://some.namespace/with/name#')
ns2 = rdflib.Namespace('http://second.namespace/some/name#')
u = rdflib.term.URIRef('my_example')
gg.add((u, ns1.name, rdflib.Literal('myname')))
blank = rdflib.BNode()
gg.add((u, ns1.annotated_with, blank))
gg.add((blank, ns1.annotation, ns2.annotation_value))

print gg.serialize(format='n3')

Which should, in my oppinion, produce the correct result, and, in a way, it does. The result of the code above is a n3 string that looks like this:

@prefix ns1: <http://some.namespace/with/name#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

<my_example> ns1:annotated_with [ ns1:annotation <http://second.namespace/some/name#annotation_value> ] ;
    ns1:name "myname" .

Which is close, but I don't understand why rdflib did not define, in the beginning of the file, the second namespace I am using. Is there a way to force it to do this?

5xum

The method bind is what I was looking for. It was used in the related question and is also useful here.

Adding the lines

gg.bind('myprefix1', ns1)
gg.bind('myprefix2', ns1)

results in what I needed.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

RDFLib: Remove namespace from a URIRef resource

How to define namespace in python?

Define values for each namespace

Define a NameSpace in VB

How to force delete a Kubernetes Namespace?

Rust define new Uuid namespace

Why does "#define A" interfere with "namespace A{}"?

Blazor: Define namespace in Razor File

XSLT- Pre Define NameSpace

How to force autoconf to not define a macro?

Using #define to force call a function

How to define namespace name same as return type of function in that namespace?

How do I define a variable in a namespace with "using namespace"?

Best way to define obj constructor in namespace javascript

How to define a friend operator declared on a template in a namespace?

Is it possible to programmatically define a new function in a given namespace?

How to define a reusable function in vue component namespace

Define C# class outside of any namespace

Define a QObject derived class inside an anonymous namespace?

Define ListenerContainer without the jms namespace in spring jms

How to define/declare xaml namespace in WPF?

How to define data types directly in a namespace

Using define directive to get the namespace name

Force a class implementing the interface to define a constant

Declare variable in namespace, define it in main, make it visible to all other files

How to define custom namespace and tag alias using camel-xstream

Define all external JavaScript in a pre-existing namespace

How to define functions and data from another namespace, not in the global one?

Can you define a class static constant at namespace scope with auto?