我目前正在使用Amara2进行XML工作,但是为了寻找一个可以与Py3和Py2一起工作的解决方案,我在lxml.objectify上取得了一些进展,但是在如何生成它方面有一个问题。
<?xml version="1.0" encoding="UTF-8"?>
<query xmlns="http://www.vinoxml.org/XMLschema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
<querycreator>
<name>The Wine Cellar Book - version 15.1</name>
</querycreator>使用lxml和下面的代码片段,我可以得到以下结果。
doc_header = """<query></query>"""
doc = etree.ElementTree(etree.fromstring(doc_header))
docO = objectify.fromstring(doc_header)
objectify.SubElement(docO, "querycreator")
docO.querycreator.name = objectify.DataElement(u"The Wine Cellar Book - version %s"
% 15.1)
<query>
<querycreator>
<name>The Wine Cellar Book - version 15.1</name>
</querycreator>
</query>在amara中,我可以使用这样的doc_header:
doc_header = """<query xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.vinoxml.org/XMLschema" xmlns:xsd="http://www.w3.org/2001/XMLSchema"></query>"""但是在docO.querycreator上使用I和lxml.objectify时,我得到了属性错误。
我也尝试过objectify.Elementmaker,但也不能让它工作。
发布于 2015-02-09 18:43:40
通过使用ElementMaker,我得到了名称空间。
myE = objectify.ElementMaker(namespace="http://www.vinoxml.org/XMLschema",
nsmap={None : "http://www.vinoxml.org/XMLschema"})
docO = myE.query(myE.querycreator())
docO.querycreator.name = objectify.DataElement(u"The Wine Cellar Book - version %s"
% 15.1)
objectify.deannotate(docO)
etree.cleanup_namespaces(docO)
print(etree.tostring(docO, pretty_print=True,
encoding="UTF-8", xml_declaration=True))我得到了vinoXML名称空间,但不确定是否需要:xsi和:xsd。
<?xml version='1.0' encoding='UTF-8'?>
<query xmlns="http://www.vinoxml.org/XMLschema">
<querycreator>
<name>The Wine Cellar Book - version 15.1</name>
</querycreator>
</query>https://stackoverflow.com/questions/28406675
复制相似问题