-
I have some very specific need to create RDF string Literal being the piece of LISP code.
but when I create a Literal
it is always serialized like below:
Is it possible to somehow tell the rdflib (or probably some turtle parser plugin) to accept, during serialization, the string "as it is" and NOT wrap the whole string with " marks? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Is this any use? ... def test_verbatim_literal():
from rdflib import Graph, Namespace, Literal
g = Graph()
CC = Namespace("cc:")
R = Namespace("r:")
g.bind("cc", CC)
g.bind("r", R)
g.add(
(
CC["register-prefix"],
R.effect,
Literal('( "rdf_register_prefix" ( "atomize" "xxx" ) xxx: )'),
)
)
term = list(g)[0]
res = f"{term[0]}\n{term[1]} {term[2].toPython()}"
assert (
res
== 'cc:register-prefix\nr:effect ( "rdf_register_prefix" ( "atomize" "xxx" ) xxx: )'
) |
Beta Was this translation helpful? Give feedback.
-
@maciaren I believe you actually do not want an RDF Literal, which are just datatyped/language-tagged scalar ("atomic") values. Those who do not have a syntactic shorthand in a certain serialization are always "just" a string, and in Turtle, quotes within those must be escaped. You cannot "break out of syntax" without hacking the serializer in non-standard ways. What you appear to be wanting is to express an S-expression as an RDF List (formally called an RDF Collection). This is definitely possible. Programmatically you can build your desired Turtle example like this: from rdflib import Graph, Namespace, Literal, BNode
CC = Namespace("https://example.org/cc/")
R = Namespace("https://example.org/r/")
IBA = Namespace("https://example.org/iba/")
g = Graph()
g.bind("cc", CC)
g.bind("r", R)
g.bind("iba", IBA)
collid = BNode()
coll = g.collection(collid)
coll.append(Literal("rdf_register_prefix"))
subcollid = BNode()
subcoll = g.collection(subcollid)
subcoll.append(Literal("atomize"))
subcoll.append(Literal("iba"))
coll.append(subcollid)
coll.append(IBA[''])
g.add((CC["register-prefix"], R.effect, collid))
print(g.serialize(format='turtle')) Which gives you: @prefix cc: <https://example.org/cc/> .
@prefix iba: <https://example.org/iba/> .
@prefix r: <https://example.org/r/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
cc:register-prefix r:effect ( "rdf_register_prefix" ( "atomize" "iba" ) iba: ) . If you need to parse an RDF List string representation, that is something not possible in and of itself, you need to parse a full statement with such as an object if so, and pick that out from the resulting graph and add it to another one. That would be more involved than building up collections programmatically as per above, which I hope you'll get by with. (You could also represent this as e.g. a JSON literal of course, which has some special treatment in JSON-LD 1.1. But I doubt that those would help you (also see the warning about them in that spec). RDF lists are S-expressions, after all.) |
Beta Was this translation helpful? Give feedback.
@maciaren I believe you actually do not want an RDF Literal, which are just datatyped/language-tagged scalar ("atomic") values. Those who do not have a syntactic shorthand in a certain serialization are always "just" a string, and in Turtle, quotes within those must be escaped. You cannot "break out of syntax" without hacking the serializer in non-standard ways.
What you appear to be wanting is to express an S-expression as an RDF List (formally called an RDF Collection). This is definitely possible. Programmatically you can build your desired Turtle example like this: