Создание типа документа с etree lxml

Я хочу добавить типы документов в свои XML-документы, которые я создаю с помощью etree LXML.

Однако я не могу понять, как добавить тип документа. Жесткое кодирование и объединение строки не вариант.

Я ожидал чего-то вроде того, как PI добавляются в etree:

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

Но это не работает для меня. Как добавить в XML-документ с помощью lxml?


person Marijn    schedule 14.06.2009    source источник


Ответы (3)


Вы можете создать свой документ с типом документа для начала:

# 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")

печатает:

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

Если вы хотите добавить тип документа в какой-либо XML, который не был создан с ним, вы можете сначала создать его с желаемым типом документа (как указано выше), а затем скопировать в него свой 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")

печатает:

<?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>

Это то, что вы ищете?

person Community    schedule 08.07.2009
comment
Ссылка устарела. - person Antoine Pelisse; 30.10.2014

Это сработало для меня:

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

PI фактически добавляется как предыдущий элемент из "doc". Таким образом, это не дочерний элемент "doc". Вы должны использовать "doc.getroottree()"

Вот пример:

>>> 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>

с помощью 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>
person Bertrand Mathieu    schedule 18.05.2010