1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
import rdflib
from rdflib import RDF, Graph, Literal
def testPythonRoundtrip():
l1 = Literal("<msg>hello</msg>", datatype=RDF.XMLLiteral)
assert l1.value is not None, "xml must have been parsed"
assert l1.datatype == RDF.XMLLiteral, "literal must have right datatype"
l2 = Literal("<msg>good morning</msg>", datatype=RDF.XMLLiteral)
assert l2.value is not None, "xml must have been parsed"
assert not l1.eq(l2), "literals must NOT be equal"
l3 = Literal(l1.value)
assert l1.eq(l3), "roundtripped literals must be equal"
assert l3.datatype == RDF.XMLLiteral, "literal must have right datatype"
l4 = Literal("<msg >hello</msg>", datatype=RDF.XMLLiteral)
assert l1 == l4
assert l1.eq(l4)
rdflib.NORMALIZE_LITERALS = False
try:
l4 = Literal("<msg >hello</msg>", datatype=RDF.XMLLiteral)
assert l1 != l4
assert l1.eq(l4)
finally:
rdflib.NORMALIZE_LITERALS = True
def testRDFXMLParse():
rdfxml = """\
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:dc="http://purl.org/dc/elements/1.1/"
>
<rdf:Description rdf:about="http://example.org/">
<dc:description rdf:parseType="Literal">
<p xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"></p>
</dc:description>
</rdf:Description>
</rdf:RDF>"""
g = rdflib.Graph()
g.parse(data=rdfxml, format="xml")
l1 = list(g)[0][2]
assert l1.datatype == RDF.XMLLiteral
def graph():
g = rdflib.Graph()
g.add(
(
rdflib.URIRef("http://example.org/a"),
rdflib.URIRef("http://example.org/p"),
rdflib.Literal("<msg>hei</hei>", datatype=RDF.XMLLiteral),
)
)
return g
def roundtrip(fmt):
g1 = graph()
l1 = list(g1)[0][2]
g2 = rdflib.Graph()
g2.parse(data=g1.serialize(format=fmt), format=fmt)
l2 = list(g2)[0][2]
assert l1.eq(l2)
def testRoundtrip():
roundtrip("xml")
roundtrip("n3")
roundtrip("nt")
def testHTML():
l1 = Literal("<msg>hello</msg>", datatype=RDF.XMLLiteral)
assert l1.value is not None, "xml must have been parsed"
assert l1.datatype == RDF.XMLLiteral, "literal must have right datatype"
l2 = Literal("<msg>hello</msg>", datatype=RDF.HTML)
assert l2.value is not None, "xml must have been parsed"
assert l2.datatype == RDF.HTML, "literal must have right datatype"
assert l1 != l2
assert not l1.eq(l2)
|