La creación de un tipo de documento con etree de lxml
-
13-09-2019 - |
Pregunta
Quiero añadir doctypes a mis documentos XML que estoy generando con etree de LXML.
Sin embargo no puedo encontrar la manera de añadir un tipo de documento. Hardcoding y concating la cadena no es una opción.
Me esperaba algo a lo largo de las líneas de la forma de PI se añaden en etree:
pi = etree.PI(...)
doc.addprevious(pi)
Pero no funciona para mí. Cómo agregar una a un documento XML con lxml?
Solución
Puede crear el documento con un tipo de documento, para empezar:
# 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 "é"> ]>
<root>
<a>&tasty; soufflé</a>
</root>
"""
tree = et.parse(StringIO.StringIO(s))
print et.tostring(tree, xml_declaration=True, encoding="utf-8")
impresiones:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE root SYSTEM "test" [
<!ENTITY tasty "cheese">
<!ENTITY eacute "é">
]>
<root>
<a>cheese soufflé</a>
</root>
Si desea agregar un tipo de documento XML a algunos que no fue creada con uno, primero puede crear una con el tipo de documento que desee (como el anterior), y luego copiar el código XML DOCTYPE-menos en él:
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")
impresiones:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE root SYSTEM "test" [
<!ENTITY tasty "cheese">
<!ENTITY eacute "é">
]>
<root><test/><a>whatever</a><end_test/></root>
Es eso lo que está buscando?
Otros consejos
Esto funcionó para mí:
print etree.tostring(tree, pretty_print=True, xml_declaration=True, encoding="UTF-8", doctype="<!DOCTYPE TEST_FILE>")
El PI es en realidad añade como un elemento anterior de "doc". Por lo tanto, no es un hijo de "doc". Debe utilizar "doc.getroottree ()"
Aquí es un ejemplo:
>>> 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>
con 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>