def testTagToCurie(self): self.assertEquals('dc:title', namespaces.tagToCurie('{http://purl.org/dc/elements/1.1/}title')) self.assertRaises(KeyError, namespaces.tagToCurie, '{unknown}tag') self.assertRaises(ValueError, namespaces.tagToCurie, 'no-uri-in-tag') self.assertEquals('srw:records', namespaces.tagToCurie(namespaces.expandNsTag('srw:records'))) ns2 = namespaces.copyUpdate({'new':'uri:new'}) self.assertEquals('new:tag', ns2.tagToCurie('{uri:new}tag'))
def testTagToCurie(self): self.assertEqual( 'dc:title', namespaces.tagToCurie('{http://purl.org/dc/elements/1.1/}title')) self.assertRaises(KeyError, namespaces.tagToCurie, '{unknown}tag') self.assertRaises(ValueError, namespaces.tagToCurie, 'no-uri-in-tag') self.assertEqual( 'srw:records', namespaces.tagToCurie(namespaces.expandNsTag('srw:records'))) ns2 = namespaces.copyUpdate({'new': 'uri:new'}) self.assertEqual('new:tag', ns2.tagToCurie('{uri:new}tag'))
def add(self, lxmlNode, **kwargs): fieldslist = [] for child in lxmlNode.getroot().getchildren(): if child.tag == curieToTag('meta:repository'): for repokind in child.iterchildren(): fieldname = tagToCurie(repokind.tag) fieldslist.append((fieldname, repokind.text)) yield self.all.add(fieldslist=fieldslist, **kwargs) # <meta xmlns="http://meresco.org/namespace/harvester/meta"> # <upload> # <id>knaw:record:4</id> # </upload> # <record> # <id>record:4</id> # <harvestdate>2016-10-05T10:30:45Z</harvestdate> # <metadataNamespace>http://www.loc.gov/mods/v3</metadataNamespace> # </record> # <repository> # <id>knaw</id> # <set>oa_publications</set> # <baseurl>http://depot.knaw.nl/cgi/oai2</baseurl> # <repositoryGroupId>knaw</repositoryGroupId> # <metadataPrefix>nl_didl</metadataPrefix> # <collection>publication</collection> # </repository> # </meta>
def testReificationWithBlankNodeSubject(self): testNamespaces = namespaces.copyUpdate(dict(test="urn:test#")) xpathFirst = testNamespaces.xpathFirst rdfXml = '''<rdf:RDF %(xmlns_rdf)s %(xmlns_test)s> <rdf:Description> <test:relation rdf:ID="_987">object</test:relation> </rdf:Description> <rdf:Statement rdf:about="#_987"> <test:reificationRelation>reification object</test:reificationRelation> </rdf:Statement> </rdf:RDF>''' % testNamespaces graph = RDFParser().parse(XML(rdfXml)) result = Triples2RdfXml(namespaces=testNamespaces).asRdfXml(graph) self.assertEquals('reification object', xpathFirst(result, '/rdf:RDF/rdf:Statement[@rdf:about="#_987"]/test:reificationRelation/text()')) self.assertEquals(None, xpathFirst(result, '/rdf:RDF/rdf:Statement[@rdf:about="#_987"]/rdf:subject')) # rdf:Statement with bnode subject should not actually refer to it...!! self.assertEquals('object', xpathFirst(result, '/rdf:RDF/rdf:Description/test:relation[@rdf:ID="_987"]/text()')) self.assertEquals(['rdf:Description', 'rdf:Statement'], [tagToCurie(node.tag) for node in xpath(result, '/rdf:RDF/*')])
def testReificationWithRdfID(self): testNamespaces = namespaces.copyUpdate(dict(test="urn:test#")) xpathFirst = testNamespaces.xpathFirst rdfXml = '''<rdf:RDF %(xmlns_rdf)s %(xmlns_test)s> <rdf:Description rdf:about="some:uri"> <test:relation rdf:ID="_987">object</test:relation> </rdf:Description> <rdf:Statement rdf:about="#_987"> <test:reificationRelation>reification object</test:reificationRelation> </rdf:Statement> </rdf:RDF>''' % testNamespaces graph = RDFParser().parse(XML(rdfXml)) result = Triples2RdfXml(namespaces=testNamespaces).asRdfXml(graph) self.assertEquals('object', xpathFirst(result, '/rdf:RDF/rdf:Description[@rdf:about="some:uri"]/test:relation[@rdf:ID="_987"]/text()')) self.assertEquals('reification object', xpathFirst(result, '/rdf:RDF/rdf:Statement[@rdf:about="#_987"]/test:reificationRelation/text()')) self.assertEquals(['rdf:Description', 'rdf:Statement'], [tagToCurie(node.tag) for node in xpath(result, '/rdf:RDF/*')]) self.assertXmlEquals(rdfXml, result)
def add(self, lxmlNode, **kwargs): fieldslist = [] for child in lxmlNode.getroot().getchildren(): fieldname = tagToCurie(child.tag) fieldslist.append((fieldname, child.text)) yield self.all.add(fieldslist=fieldslist, **kwargs)
def _getPrefix(element): prefix, _ = namespaces.tagToCurie(element.tag).split(':', 1) return prefix