Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:使用lxml添加xml模式属性_Python_Xsd_Lxml - Fatal编程技术网

Python:使用lxml添加xml模式属性

Python:使用lxml添加xml模式属性,python,xsd,lxml,Python,Xsd,Lxml,我编写了一个脚本,以xml格式打印当前目录中的所有.xml文件,但我不知道如何将xmlns属性添加到顶级标记。 我想要得到的输出是: <?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"

我编写了一个脚本,以xml格式打印当前目录中的所有.xml文件,但我不知道如何将xmlns属性添加到顶级标记。 我想要得到的输出是:

<?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>
我在网上找到了一些显式设置名称空间属性的例子,但老实说,在我刚刚开始的时候,它们有点超出了我的理解。是否有其他方法将这些xmlns属性添加到databaseChangeLog标记中

import lxml.etree as ET
import lxml.builder
import glob

dbchangelog = 'http://www.host.org/xml/ns/dbchangelog'
xsi = 'http://www.host.org/2001/XMLSchema-instance'
E = lxml.builder.ElementMaker(
    nsmap={
        None: dbchangelog,
        'xsi': xsi})

ROOT = E.databaseChangeLog
DOC = E.include

# grab all the xml files
files = [DOC(file=f) for f in glob.glob("*.xml")]

the_doc = ROOT(*files)
the_doc.attrib['{{{pre}}}schemaLocation'.format(pre=xsi)] = 'www.host.org/xml/ns/dbchangelog'

print(ET.tostring(the_doc,
                  pretty_print=True, xml_declaration=True, encoding='utf-8'))
屈服

<?xml version='1.0' encoding='utf-8'?>
<databaseChangeLog xmlns:xsi="http://www.host.org/2001/XMLSchema-instance" xmlns="http://www.host.org/xml/ns/dbchangelog" xsi:schemaLocation="www.host.org/xml/ns/dbchangelog">
  <include file="test.xml"/>
</databaseChangeLog>


很有效,非常感谢!您是否知道如何将输出格式化为类似于上面我所希望的输出的格式?我认为pretty_print可能会有帮助,但它似乎没有帮助:(对不起,我不知道有什么可靠的方法可以做到这一点。真的希望你仍然存在,差不多5年后。这帮了我很多,但我不理解构造
'{{{{pre}}}}schemaLocation'.format(pre=xsi)
。我理解格式,但为什么pre周围有三个大括号?我们需要表单的XML限定名。这个字符串有文字大括号,但大括号对
格式
方法有特殊意义,所以我们需要“转义”让他们告诉
format
我们需要文字大括号而不是字符串替换。换句话说,当
format
遇到2个大括号时,它返回一个带有1个文字大括号的字符串。
import lxml.etree as ET
import lxml.builder
import glob

dbchangelog = 'http://www.host.org/xml/ns/dbchangelog'
xsi = 'http://www.host.org/2001/XMLSchema-instance'
E = lxml.builder.ElementMaker(
    nsmap={
        None: dbchangelog,
        'xsi': xsi})

ROOT = E.databaseChangeLog
DOC = E.include

# grab all the xml files
files = [DOC(file=f) for f in glob.glob("*.xml")]

the_doc = ROOT(*files)
the_doc.attrib['{{{pre}}}schemaLocation'.format(pre=xsi)] = 'www.host.org/xml/ns/dbchangelog'

print(ET.tostring(the_doc,
                  pretty_print=True, xml_declaration=True, encoding='utf-8'))
<?xml version='1.0' encoding='utf-8'?>
<databaseChangeLog xmlns:xsi="http://www.host.org/2001/XMLSchema-instance" xmlns="http://www.host.org/xml/ns/dbchangelog" xsi:schemaLocation="www.host.org/xml/ns/dbchangelog">
  <include file="test.xml"/>
</databaseChangeLog>