I wrote a script that outputs all .xml files in the current directory in xml format, but I cannot figure out how to add xmlns attributes to the top level tag. The output I want to get is:
<?xml version='1.0' encoding='utf-8'?>
<databaseChangeLog
xmlns="http://www.host.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.host.org/2001/XMLSchema-instance"
xsi:schemaLocation="www.host.org/xml/ns/dbchangelog">
<include file="cats.xml"/>
<include file="dogs.xml"/>
<include file="fish.xml"/>
<include file="meerkats.xml"/>
</databaseChangLog>
However, here is the result that I get:
<?xml version='1.0' encoding='utf-8'?>
<databaseChangeLog>
<include file="cats.xml"/>
<include file="dogs.xml"/>
<include file="fish.xml"/>
<include file="meerkats.xml"/>
</databaseChangLog>
Here is my script:
import lxml.etree
import lxml.builder
import glob
E = lxml.builder.ElementMaker()
ROOT = E.databaseChangeLog
DOC = E.include
files = [DOC(file=f) for f in glob.glob("*.xml")]
the_doc = ROOT(*files)
str = lxml.etree.tostring(the_doc, pretty_print=True, xml_declaration=True, encoding='utf-8')
print str
I found several examples online where I explicitly set namespace attributes here and here , but to be honest, they turned my head up a bit when I just started. Is there any other way to add these xmlns attributes to the databaseChangeLog tag?
source
share