Appending XSLT to XML
![](https://secure.gravatar.com/avatar/534645cd8693459571ca415bdc3313e5.jpg?s=120&d=mm&r=g)
Greetings! I would want to know whether there is a function to append XSLT stylesheet to XML files. I have resorted to converting XML data to string, in order to append a reference to an XSLT styleshett. See https://git.xmpp-it.net/sch/Rivista/src/branch/main/rivista/xml/xslt.py Please advise. Kind regards, Schimon
![](https://secure.gravatar.com/avatar/a72e34437ff2a048717b7b964940b0b3.jpg?s=120&d=mm&r=g)
Hi Schimon,
For the processing instruction (<?xml-stylesheet ...?>) itself I think you're looking for s.th. like
(see e.g. https://gist.github.com/larsks/de0d32d3796609cd856da7b95e7e4808) That said - and I'm out of depth here since I never needed processing instructions: - This link (German-only, unfortunately) suggests that the type for an XSL stylesheet should rather be text/xml: https://www.data2type.de/xml-xslt-xslfo/ xml/xml-in-a-nutshell/cascading-stylesheets/stylesheets-mit-xml-dokumenten (I haven't checked any specs) - Are you sure you actually produce valid XML? I'd have thought this usage of href implies an external XSL document, not something inlined. https://www.w3.org/TR/xml-stylesheet/ and https://www.w3.org/TR/xslt20/ #embedded seem to imply you'd use a fragment identifier URI as href and a corresponding id field on your inlined stylesheet - but this would have to be part of your embedding XML tree, not simply prepended to it, since an XML doc can only have one single root node. Then again, one of the linked docs is pretty old and the other refers to XSLT 2, which hasn't seen widespread adoption (and isn't supported by libxml2/ lbxslt and thus lxml). Best regards Holger
![](https://secure.gravatar.com/avatar/534645cd8693459571ca415bdc3313e5.jpg?s=120&d=mm&r=g)
Greetings, Holger! Thank you for your instructions and references. Please. Read further messages. On Fri, 10 Jan 2025 17:28:00 +0100 jholg--- via lxml - The Python XML Toolkit <lxml@python.org> wrote:
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. 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.
Danke!
Node stylesheet instruction should not replace node root. Node stylesheet instruction should appear immediately after node root. <?xml version="1.0"?> <?xml-stylesheet type="text/xml" href="whatever.xsl"?> <feed xmlns="http://www.w3.org/2005/Atom"> </feed>
Kindly, Schimon
![](https://secure.gravatar.com/avatar/8b97b5aad24c30e4a1357b38cc39aeaa.jpg?s=120&d=mm&r=g)
Schimon Jehudah via lxml - The Python XML Toolkit schrieb am 12.01.25 um 08:53:
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.)
Just configure the output encoding explicitly or force the XML declaration to be written. https://lxml.de/tutorial.html#serialisation Stefan
![](https://secure.gravatar.com/avatar/534645cd8693459571ca415bdc3313e5.jpg?s=120&d=mm&r=g)
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:
This is good to know. Thank you for sharing this insight.
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
![](https://secure.gravatar.com/avatar/36206c06354fafb116d1196ec7a66d0b.jpg?s=120&d=mm&r=g)
Just configure the output encoding explicitly or force the XML declaration to be written.
![](https://secure.gravatar.com/avatar/a72e34437ff2a048717b7b964940b0b3.jpg?s=120&d=mm&r=g)
Hi Schimon,
For the processing instruction (<?xml-stylesheet ...?>) itself I think you're looking for s.th. like
(see e.g. https://gist.github.com/larsks/de0d32d3796609cd856da7b95e7e4808) That said - and I'm out of depth here since I never needed processing instructions: - This link (German-only, unfortunately) suggests that the type for an XSL stylesheet should rather be text/xml: https://www.data2type.de/xml-xslt-xslfo/ xml/xml-in-a-nutshell/cascading-stylesheets/stylesheets-mit-xml-dokumenten (I haven't checked any specs) - Are you sure you actually produce valid XML? I'd have thought this usage of href implies an external XSL document, not something inlined. https://www.w3.org/TR/xml-stylesheet/ and https://www.w3.org/TR/xslt20/ #embedded seem to imply you'd use a fragment identifier URI as href and a corresponding id field on your inlined stylesheet - but this would have to be part of your embedding XML tree, not simply prepended to it, since an XML doc can only have one single root node. Then again, one of the linked docs is pretty old and the other refers to XSLT 2, which hasn't seen widespread adoption (and isn't supported by libxml2/ lbxslt and thus lxml). Best regards Holger
![](https://secure.gravatar.com/avatar/534645cd8693459571ca415bdc3313e5.jpg?s=120&d=mm&r=g)
Greetings, Holger! Thank you for your instructions and references. Please. Read further messages. On Fri, 10 Jan 2025 17:28:00 +0100 jholg--- via lxml - The Python XML Toolkit <lxml@python.org> wrote:
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. 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.
Danke!
Node stylesheet instruction should not replace node root. Node stylesheet instruction should appear immediately after node root. <?xml version="1.0"?> <?xml-stylesheet type="text/xml" href="whatever.xsl"?> <feed xmlns="http://www.w3.org/2005/Atom"> </feed>
Kindly, Schimon
![](https://secure.gravatar.com/avatar/8b97b5aad24c30e4a1357b38cc39aeaa.jpg?s=120&d=mm&r=g)
Schimon Jehudah via lxml - The Python XML Toolkit schrieb am 12.01.25 um 08:53:
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.)
Just configure the output encoding explicitly or force the XML declaration to be written. https://lxml.de/tutorial.html#serialisation Stefan
![](https://secure.gravatar.com/avatar/534645cd8693459571ca415bdc3313e5.jpg?s=120&d=mm&r=g)
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:
This is good to know. Thank you for sharing this insight.
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
![](https://secure.gravatar.com/avatar/36206c06354fafb116d1196ec7a66d0b.jpg?s=120&d=mm&r=g)
Just configure the output encoding explicitly or force the XML declaration to be written.
participants (4)
-
jholg@gmx.de
-
Neil Donaldson
-
Schimon Jehudah
-
Stefan Behnel