
Greetings, Stefan! Thank you for your respond. I have added comments further. On Wed, 15 Jan 2025 08:49:48 +0100 Stefan Behnel via lxml - The Python XML Toolkit <lxml@python.org> wrote:
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.
This is good to know. Thank you for sharing this insight.
("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.
I think that I already conduct serialisation, yet I want to refrain from utilizing it. https://git.xmpp-it.net/sch/Rivista/src/branch/main/rivista/xml/xslt.py#L15 I want an instruction, that is similar to the one which was offered by Mr. Holger, which only appends a stylesheet line after the XML declaration ("<?xml …>"). If it appears that I do not understand what am I stating, then please correct me if I am wrong. I am a lawyer, not an expert engineer. Schimon