Python – Create XML files with prefix tags (python and lxml)

Create XML files with prefix tags (python and lxml)… here is a solution to the problem.

Create XML files with prefix tags (python and lxml)

I tried to create an XML file like this:

<pico:record xsi:schemaLocation="http://purl.org/pico/1.0/ http://www.culturaitalia.it/pico/schemas/1.0/pico.xsd>
    <dc:identifier>work_3117</dc:identifier>
</pico:record>

I use this code:

from lxml import etree 
xsi="http://www.w3.org/2001/XMLSchema-instance"
schemaLocation="http://purl.org/pico/1.0/ http://www.culturaitalia.it/pico/schemas/1.0/pico.xsd"
ns = "{xsi}"
root=etree. Element("pico:record", attrib={"{" + xsi + "}schemaLocation" : schemaLocation})
etree. SubElement(root, "dc:identifier").text = "work_3117"

print(etree.tostring(root, pretty_print=True))

It didn’t work, python told me:

ValueError: Invalid tag name u’pico:record’

If I change “pico:recors” to “record”, the error is:

ValueError: Invalid tag name u’dc:identifier’

Solution

Well, this question is a bit old, but I had the same problem today.

You need to provide the “dc” namespace for the build, and so does “pico”. And you have to let lxml know about this namespace. You can do this using the namespace mapping provided when you created the root element:

from lxml import etree
xsi="http://www.w3.org/2001/XMLSchema-instance"
schemaLocation="http://purl.org/pico/1.0/ http://www.culturaitalia.it/pico/schemas/1.0/pico.xsd"
pico = "http://purl.org/pico/1.0/"
dc = "http://purl.org/dc/elements/1.1/"
ns = {"xsi": xsi, "dc": dc, "pico": schemalocation}
root=etree. Element("{" + pico + "}record", attrib={"{" + xsi + "}schemaLocation" : schemaLocation}, nsmap=ns)
etree. SubElement(root, "{" + dc + "}" + "identifier").text = "work_3117"
print etree.tostring(root, pretty_print=True)

The result is:

<pico:record xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:pico="http://purl.org/pico/1.0/" xsi: schemaLocation="http://purl.org/pico/1.0/ http://www.culturaitalia.it/pico/schemas/1.0/pico.xsd">
  <dc:identifier>work_3117</dc:identifier>
</pico:record>

For more information, see: http://lxml.de/tutorial.html#namespaces

Related Problems and Solutions