RDFLib/rdflib — Issue #166: serialize(format="pretty-xml") fails on cyclic links
Author: gjhiggins
Created: 2012-02-20 08:34:13+00:00
State: open

jman...@gmail.com, 2011-08-13T00:28:24.000Z

When cyclically related nodes are serialized with format="pretty-xml", the nodeID for the outermost node is not included. This leaves the inner node making a dangling (unresolved) reference. See below for details...

--------------
What steps will reproduce the problem?

$ [Fresh Ubuntu 11.04 install]
$ sudo easy_install -U "rdflib>=3.0.0"

$ python

from rdflib import *
g = Graph()
g.bind("j", URIRef("http://example.com#"))

g.add((a, RDF.type, URIRef("http://example.com#a")))
g.add((b, RDF.type, URIRef("http://example.com#b")))

g.add((a, URIRef("htt://example.com#linkedto"), b))
g.add((b, URIRef("htt://example.com#linkedto"), a))

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

--------------
What is the expected output?

--------------
What do you see instead?

The node of type does *not* have a nodeID assigned, meaning that the reference is broken! Exact output below:

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

$ [Fresh Ubuntu 11.04 install]
$ sudo easy_install -U "rdflib>=3.0.0"

Comment 1 by gjhigg...@gmail.com
Good catch. The PrettyXMLSerializer code is explicit about this (mis)behaviour:

if isinstance(subject, BNode):
def subj_as_obj_more_than(ceil):
return more_than(store.triples((None, None, subject)), ceil)
#here we only include BNode labels if they are referenced
#more than once (this reduces the use of redundant BNode identifiers)
if subj_as_obj_more_than(1):
writer.attribute(RDF.nodeID, fix(subject))

and this produces exactly the disconnect that you are seeing.

Just to confirm: this issue is restricted to the output of PrettyXMLSerializer - choose any serialization other than pretty-xml and you will be able to parse it back into a properly-connected graph.

IMO, it's a false economy and could usefully be dispensed with.

Comment 2 by gjhigg...@gmail.com
Fixed in changeset 3ddb26ab8f4f - check for BNode references > 1 commented out.