How To Write Namespaced Element Attributes With Lxml?
I'm using lxml (2.2.8) to create and write out some XML (specifically XGMML). The app which will be reading it is apparently fairly fussy and wants to see a top level element with
Solution 1:
Unlike ElementTree or other serializers that would allow this, lxml
needs you to set up these namespaces beforehand:
NSMAP = {"dc" : 'http://purl.org/dc/elements/1.1',
"xlink" : 'http://www.w3.org/1999/xlink'}
root = Element("graph", nsmap = NSMAP)
(and so on and so forth for the rest of the declarations)
And then you can use the namespaces using their proper declarations:
n = SubElement(root, "{http://purl.org/dc/elements/1.1}foo")
Of course this gets annoying to type, so it is generally beneficial to assign the paths to short constant names:
DCNS = "http://purl.org/dc/elements/1.1"
And then use that variable in both the NSMAP
and the SubElement
declarations:
n = SubElement(root, "{%s}foo" % (DCNS))
Solution 2:
Using ElementMaker:
import lxml.etree as ET
import lxml.builder as builder
E = builder.ElementMaker(namespace='http://www.cs.rpi.edu/XGMML',
nsmap={None: 'http://www.cs.rpi.edu/XGMML','dc': 'http://purl.org/dc/elements/1.1/','xlink': 'http://www.w3.org/1999/xlink','rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#','cy': 'http://www.cytoscape.org', })
graph = E.graph(label="Test", directed="1")
print(ET.tostring(graph, pretty_print=True))
yields
<graphxmlns:cy="http://www.cytoscape.org"xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"xmlns:dc="http://purl.org/dc/elements/1.1/"xmlns:xlink="http://www.w3.org/1999/xlink"xmlns="http://www.cs.rpi.edu/XGMML"directed="1"label="Test"/>
Post a Comment for "How To Write Namespaced Element Attributes With Lxml?"