Skip to content

[7.x] fix namespace prefixes in longturtle serialization #3134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions rdflib/plugins/serializers/longturtle.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class LongTurtleSerializer(RecursiveSerializer):

def __init__(self, store):
self._ns_rewrite = {}
namespace_manager = store.namespace_manager
store = to_canonical_graph(store)
content = store.serialize(format="application/n-triples")
lines = content.split("\n")
Expand All @@ -53,6 +54,7 @@ def __init__(self, store):
data="\n".join(lines), format="application/n-triples", skolemize=True
)
graph = graph.de_skolemize()
graph.namespace_manager = namespace_manager
super(LongTurtleSerializer, self).__init__(graph)
self.keywords = {RDF.type: "a"}
self.reset()
Expand Down
64 changes: 33 additions & 31 deletions test/data/longturtle/longturtle-target.ttl
Original file line number Diff line number Diff line change
@@ -1,72 +1,74 @@
PREFIX cn: <https://linked.data.gov.au/def/cn/>
PREFIX ex: <http://example.com/>
PREFIX geo: <http://www.opengis.net/ont/geosparql#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX schema: <https://schema.org/>
PREFIX sdo: <https://schema.org/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>

<http://example.com/nicholas>
a schema:Person ;
schema:age 41 ;
schema:alternateName
ex:nicholas
a sdo:Person ;
sdo:age 41 ;
sdo:alternateName
[
schema:name "Dr N.J. Car" ;
sdo:name "Dr N.J. Car" ;
] ,
"N.J. Car" ,
"Nick Car" ;
schema:name
sdo:name
[
a <https://linked.data.gov.au/def/cn/CompoundName> ;
schema:hasPart
a cn:CompoundName ;
sdo:hasPart
[
a <https://linked.data.gov.au/def/cn/CompoundName> ;
schema:hasPart
a cn:CompoundName ;
sdo:hasPart
[
a <https://linked.data.gov.au/def/cn/CompoundName> ;
a cn:CompoundName ;
rdf:value "Car" ;
] ,
[
a <https://linked.data.gov.au/def/cn/CompoundName> ;
a cn:CompoundName ;
rdf:value "Maxov" ;
] ;
] ,
[
a <https://linked.data.gov.au/def/cn/CompoundName> ;
a cn:CompoundName ;
rdf:value "Nicholas" ;
] ,
[
a <https://linked.data.gov.au/def/cn/CompoundName> ;
a cn:CompoundName ;
rdf:value "John" ;
] ;
] ;
schema:worksFor <https://kurrawong.ai> ;
sdo:worksFor <https://kurrawong.ai> ;
.

<https://kurrawong.ai>
a schema:Organization ;
schema:location <https://kurrawong.ai/hq> ;
a sdo:Organization ;
sdo:location <https://kurrawong.ai/hq> ;
.

<https://kurrawong.ai/hq>
a schema:Place ;
schema:address
a sdo:Place ;
sdo:address
[
a schema:PostalAddress ;
schema:addressCountry
a sdo:PostalAddress ;
sdo:addressCountry
[
schema:identifier "au" ;
schema:name "Australia" ;
sdo:identifier "au" ;
sdo:name "Australia" ;
] ;
schema:addressLocality "Shorncliffe" ;
schema:addressRegion "QLD" ;
schema:postalCode 4017 ;
schema:streetAddress (
sdo:addressLocality "Shorncliffe" ;
sdo:addressRegion "QLD" ;
sdo:postalCode 4017 ;
sdo:streetAddress (
72
"Yundah"
"Street"
) ;
] ;
schema:geo
sdo:geo
[
schema:polygon "POLYGON((153.082403 -27.325801, 153.08241 -27.32582, 153.082943 -27.325612, 153.083010 -27.325742, 153.083543 -27.325521, 153.083456 -27.325365, 153.082403 -27.325801))"^^geo:wktLiteral ;
sdo:polygon "POLYGON((153.082403 -27.325801, 153.08241 -27.32582, 153.082943 -27.325612, 153.083010 -27.325742, 153.083543 -27.325521, 153.083456 -27.325365, 153.082403 -27.325801))"^^geo:wktLiteral ;
] ;
schema:name "KurrawongAI HQ" ;
sdo:name "KurrawongAI HQ" ;
.
28 changes: 14 additions & 14 deletions test/test_serializers/test_serializer_longturtle_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,48 +69,48 @@ def test_sort_semiblank_graph() -> None:
serialization_counter[graph_text] += 1

expected_serialization = """\
PREFIX ns1: <http://example.org/ex/>
PREFIX ex: <http://example.org/ex/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

ns1:A
ex:A
rdfs:comment "Thing A" ;
.

ns1:C
ex:C
rdfs:comment "Thing C" ;
.

ns1:B
ex:B
rdfs:comment "Thing B" ;
.

[] ns1:has
[] ex:has
[
rdfs:seeAlso ns1:A ;
rdfs:seeAlso ex:A ;
] ;
.

[] rdfs:seeAlso ns1:B ;
[] rdfs:seeAlso ex:B ;
.

[] ns1:has
[] ex:has
[
rdfs:seeAlso ns1:C ;
rdfs:seeAlso ex:C ;
] ;
.

[] rdfs:seeAlso ns1:A ;
[] rdfs:seeAlso ex:A ;
.

[] rdfs:seeAlso ns1:C ;
[] rdfs:seeAlso ex:C ;
.

[] rdfs:seeAlso ns1:B ;
[] rdfs:seeAlso ex:B ;
.

[] ns1:has
[] ex:has
[
rdfs:seeAlso ns1:B ;
rdfs:seeAlso ex:B ;
] ;
.

Expand Down
Loading