-
Hello I have a simple XML file with the following content: <?xml version="1.0" encoding="UTF-8"?>
<root>
<child>OKOK</child>
</root> When checking the content with the w3 Validation Tool https://www.w3.org/RDF/Validator/ I get an error: Do you know why rdflib does not throw an error message? And if rdflib is somehow able to recognize that the RDF tags are missing in the file? Kind Regards |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments
-
That's a reasonable question. RDFLib adopts the widely-accepted stance that a library is more useful to programmers if it is tolerant about what it accepts but strict about what it produces. In this instance, RDFLib is being useful, allowing us to work with the input. Here's how it might go. Let's start by parsing the data: >>> data = """<?xml version="1.0" encoding="UTF-8"?><root><child>OKOK</child></root>"""
>>> g = Graph()
>>> g.bind("ex", Namespace("urn:example:"))
>>> g.parse(data=data, format="xml") As you observed, this does not produce an error and this is because the result of the parse is now something we can work with ... >>> for triple in g.triples((None, None, None)):
... print(triple)
(rdflib.term.BNode('N44cccf29cf14487fa1833fb865af4531'), rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), rdflib.term.URIRef('root'))
(rdflib.term.BNode('N44cccf29cf14487fa1833fb865af4531'), rdflib.term.URIRef('child'), rdflib.term.Literal('OKOK')) Taking it a wee bit further with a rough'n'ready function to add a namespace where appropriate: >>> def addns(x):
>>> return URIRef('urn:example:' + x) if isinstance(x, URIRef) and not x.startswith('http') else x Gives us the opportunity to do this: >>> for (s, p, o) in g.triples((None, None, None)):
... g.remove((s, p, o))
.... g.add((s, addns(p), addns(o))) Bringing us to the point where we can copy'n'paste the serialized graph into the W3C validator for a successful validation: print(g.serialize(format="xml")) <?xml version="1.0" encoding="utf-8"?>
<rdf:RDF
xmlns:ex="urn:example:"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
>
<rdf:Description rdf:nodeID="N44cccf29cf14487fa1833fb865af4531">
<rdf:type rdf:resource="urn:example:root"/>
<ex:child>OKOK</ex:child>
</rdf:Description>
</rdf:RDF>
It has done ... and has tried to fix things up a bit with the intention that the result is usefully computationally tractable. HTH |
Beta Was this translation helpful? Give feedback.
-
Thank you @gjhiggins for the detailed explanation. I also think that flexibility is an advantage. Only I do not understand why it is not possible to catch a warning. Attached is an example from Jena (java implementation) the first two examples give no error but a warning the third example gives an error and also a warning. Would such a warning also be useful with rdflib or have I overlooked this feature? Greetings Example 1 (Valid but has warnings) <?xml version="1.0" encoding="UTF-8"?>
<root>
<child>OK</child>
</root> Log 1
Example 2 (Valid but has warnings) <?xml version="1.0" encoding="UTF-8"?>
<root>
<child>OK</child>
<child>OK</child>
</root> Log 2
Example 3 (Invalid and has warnings) <?xml version="1.0" encoding="UTF-8"?>
<root>
<child>
<subchild>NOT OK</subchild>
<subchild>NOT OK</subchild>
</child>
</root> Log 3
Kind Regards |
Beta Was this translation helpful? Give feedback.
-
It's certainly possible, the code for the rdfxml parser is here and anyone who is sufficiently both motivated and skilled could modify the parser to emit such a warning and submit a PR. However, given the venerability of that code (“Copyright (c) 2002, Daniel Krech, http://eikeon.com/”), the lack of a warning has not been perceived either as a pressing issue or as a critical feature so is unlikely to figure on anyone's to-do list - which is not surprising if you understand it as an issue of allocating scarce resources. |
Beta Was this translation helpful? Give feedback.
-
Understandable, the library is already in amazing condition, these are just cosmetic details. I wish you all the best and thank you for your efforts. |
Beta Was this translation helpful? Give feedback.
-
Thanks for your understanding, I'll migrate this to the Discussions Q&A section where it'll remain more accessible than a closed issue. |
Beta Was this translation helpful? Give feedback.
That's a reasonable question. RDFLib adopts the widely-accepted stance that a library is more useful to programmers if it is tolerant about what it accepts but strict about what it produces. In this instance, RDFLib is being useful, allowing us to work with the input.
Here's how it might go. Let's start by parsing the data:
As you observed, this does not produce an error and this is because the result of the parse is now something we can work with ...