python 使用 lxml 的 etree 创建文档类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/991864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 21:13:29  来源:igfitidea点击:

Creating a doctype with lxml's etree

pythondoctypelxmlelementtree

提问by Marijn

I want to add doctypes to my XML documents that I'm generating with LXML's etree.

我想将文档类型添加到我使用 LXML 的 etree 生成的 XML 文档中。

However I cannot figure out how to add a doctype. Hardcoding and concating the string is not an option.

但是我不知道如何添加文档类型。硬编码和连接字符串不是一种选择。

I was expecting something along the lines of how PI's are added in etree:

我期待在 etree 中添加 PI 的方法:

pi = etree.PI(...)
doc.addprevious(pi)

But it's not working for me. How to add a to a xml document with lxml?

但这对我不起作用。如何使用 lxml 将 a 添加到 xml 文档?

采纳答案by Marijn

You can create your document with a doctype to begin with:

您可以使用 doctype 开始创建您的文档:

# Adapted from example on http://codespeak.net/lxml/tutorial.html
import lxml.etree as et
import StringIO
s = """<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE root SYSTEM "test" [ <!ENTITY tasty "cheese"> 
<!ENTITY eacute "&#233;"> ]>
<root>
<a>&tasty; souffl&eacute;</a>
</root>
"""
tree = et.parse(StringIO.StringIO(s))
print et.tostring(tree, xml_declaration=True, encoding="utf-8")

prints:

印刷:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE root SYSTEM "test" [
<!ENTITY tasty "cheese">
<!ENTITY eacute "&#233;">
]>
<root>
<a>cheese soufflé</a>
</root>

If you want to add a doctype to some XML that wasn't created with one, you can first create one with the desired doctype (as above), then copy your doctype-less XML into it:

如果你想向一些不是用它创建的 XML 添加一个 doctype,你可以先用所需的 doctype(如上)创建一个,然后将你的无 doctype 的 XML 复制到其中:

xml = et.XML("<root><test/><a>whatever</a><end_test/></root>")
root = tree.getroot()
root[:] = xml
root.text, root.tail = xml.text, xml.tail
print et.tostring(tree, xml_declaration=True, encoding="utf-8")

prints:

印刷:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE root SYSTEM "test" [
<!ENTITY tasty "cheese">
<!ENTITY eacute "&#233;">
]>
<root><test/><a>whatever</a><end_test/></root>

Is that what you're looking for?

这就是你要找的吗?

回答by hgb

This worked for me:

这对我有用:

print etree.tostring(tree, pretty_print=True, xml_declaration=True, encoding="UTF-8", doctype="<!DOCTYPE TEST_FILE>")

回答by Bertrand Mathieu

The PI is actually added as a previous element from "doc". Thus, it's not a child of "doc". You must use "doc.getroottree()"

PI 实际上是作为“doc”中的前一个元素添加的。因此,它不是“doc”的孩子。您必须使用“doc.getroottree()”

Here is an example:

下面是一个例子:

>>> root = etree.Element("root")
>>> a  = etree.SubElement(root, "a")
>>> b = etree.SubElement(root, "b")
>>> root.addprevious(etree.PI('xml-stylesheet', 'type="text/xsl" href="my.xsl"'))
>>> print etree.tostring(root, pretty_print=True, xml_declaration=True, encoding='utf-8')
<?xml version='1.0' encoding='utf-8'?>
<root>
  <a/>
  <b/>
</root>

with getroottree():

使用 getroottree():

>>> print etree.tostring(root.getroottree(), pretty_print=True, xml_declaration=True, encoding='utf-8')
<?xml version='1.0' encoding='utf-8'?>
<?xml-stylesheet type="text/xsl" href="my.xsl"?>
<root>
  <a/>
  <b/>
</root>