Schimon Jehudah via lxml - The Python XML Toolkit schrieb am 12.01.25 um 08:53:
On Fri, 10 Jan 2025 17:28:00 +0100
jholg--- via lxml - The Python XML Toolkit <lxml@python.org> wrote:

>>> from lxml import etree
>>> elem = etree.fromstring('<data/>')
>>> tree = elem.getroottree()
>>> tree.getroot().addprevious(etree.ProcessingInstruction('xml-stylesheet',
>>> 'type="text/xml" href="whatever.xsl"'))
>>> etree.tostring(tree)
b'<?xml-stylesheet type="text/xml" href="whatever.xsl"?><data/>'
>>>

This is almost what I need.

Sample
------
<?xml version="1.0"?><feed xmlns="http://www.w3.org/2005/Atom";></feed>

Result
------
>>> etree.tostring(tree)
b'<?xml-stylesheet type="text/xml" href="whatever.xsl"?><feed 
xmlns="http://www.w3.org/2005/Atom";>\n</feed>'

Node root is missing. XML is not valid.

It is well-formed XML. The XML declaration ("<?xml …>") is not mandatory for UTF-8 encoded XML.

("valid" means that it adheres to a schema. There is no XML validation involved here.)


Desired result
--------------
b'<?xml version="1.0"?><?xml-stylesheet type="text/xml" href="whatever.xsl"?><feed 
xmlns="http://www.w3.org/2005/Atom";>\n</feed>'

Node stylesheet appears after node root.

I suppose, that I would have to copy node root and concatenate it to
the, node stylesheet instruction.

Just configure the output encoding explicitly or force the XML declaration to be written.

https://lxml.de/tutorial.html#serialisation

Stefan

_______________________________________________
lxml - The Python XML Toolkit mailing list -- lxml@python.org
To unsubscribe send an email to lxml-le...@python.org
https://mail.python.org/mailman3/lists/lxml.python.org/
Member address: arch...@mail-archive.com

Reply via email to