RDFLib / rdflib

RDFLib is a Python library for working with RDF, a simple yet powerful language for representing information.
https://rdflib.readthedocs.org
BSD 3-Clause "New" or "Revised" License
2.15k stars 555 forks source link

Wrapping an rdf:List loses information in pretty-xml serialization #160

Open ghost opened 12 years ago

ghost commented 12 years ago

azarot...@gmail.com, 2011-06-22T17:10:07.000Z

What steps will reproduce the problem?

from rdflib import ConjunctiveGraph
from rdflib import Namespace, Literal
from rdflib.collection import Collection

# Need to generate the following:

foo = Namespace('http://www.example.org/foo/ns/')
ex = Namespace('http://www.example.org/example/foo/')
rdf = Namespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#')

# Works:  x a rdf:List, a foo:Other ;
# Fails:  y a foo:Wrapper, foo:wraps x; x a rdf:List, a foo:Other ;

g = ConjunctiveGraph()

bits = [ex['a'], ex['b'], ex['c']]
l = Collection(g, ex['thing'], bits)
g.add((ex['thing'], rdf['type'], foo['Other']))
g.add((ex['thing'], foo['property'], Literal('Some Value')))
for b in bits:
    g.add((b, rdf['type'], foo['Item']))

print "Works:"
print g.serialize(format='pretty-xml')

g.add((ex['wrapper'], rdf['type'], foo['Wrapper']))
g.add((ex['wrapper'], foo['wraps'], ex['thing']))

print ""
print "Fails (info is lost):"
print g.serialize(format='pretty-xml')

What is the expected output? What do you see instead?

Equivalent of n3, or regular xml.

What version of the product are you using? On what operating system?

3.1, MacOSX

ghost commented 12 years ago

It would seem that you have encountered a modelling issue.

Deep in the rdflib.plugins.serializers.rdfxml.PrettyXMLSerializer code are the lines:


if first(store.objects(object, RDF.first)): # may not have type RDF.List
    self.__serialized[object] = 1
    # TODO: warn that any assertions on object other than
    # RDF.first and RDF.rest are ignored... including RDF.List
    writer.attribute(RDF.parseType, "Collection")

Assuming I read it correctly, this seems to be exactly what you are seeing. The assertions made on foo:thing have been ignored.

I have implemented the TODO and added a UserWarning.

I don't know why the assertions are ignored but the intention is explicit.

None of the vanilla XML, Notation3 or NTriples serializations suffer from this limitation.

kouralex commented 1 year ago

Sorry for commenting to an old issue, but neither do I understand why some assertions are ignored, can't they just be taken into account?

Using Apache Jena's rdfdiff I can confirm that models produced by vanilla XML serializer and Turtle serializer are equal but the one generated by PrettyXmlSerializer is not.

aucampia commented 1 year ago

@kouralex best to open an issue, but if something is wrong with RDFLib we are happy to have it fixed, but RDFLib is entirely community-driven.

kouralex commented 1 year ago

@aucampia yes, and as I have maintained an open source software, I understand. I was just wondering if there are some specification that let one to omit some declarations/assertions but that does not seem to be the case - hence the comment. I am not overly into/interested in the (RDF/)XML format(s), in my use case I am basically just offering different serializations of the graph to the user. Therefore, I can use the plain XML version (or use another program to generate RDF/XML-ABBREV) if I want to, so this indeed is not an issue to me.

aucampia commented 1 year ago

@kouralex if a triple is in asserted in the input source it should be in the output, so if it is ignored it is a bug. I actually don't think this should have been closed, as it seems the last communication on this made it clear it is still a problem.