Appending XSLT to XML
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
Hi Schimon,
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.
For the processing instruction (<?xml-stylesheet ...?>) itself I think you're looking for s.th. like
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/>'
(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
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:
Hi Schimon,
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.
For the processing instruction (<?xml-stylesheet ...?>) itself I think you're looking for s.th. like
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/>'
(see e.g. https://gist.github.com/larsks/de0d32d3796609cd856da7b95e7e4808)
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.
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)
Danke!
- 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.
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>
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
Kindly, Schimon
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
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
Just configure the output encoding explicitly or force the XML declaration to be written.
Greetings. I want to append instructions for XSLT without converting an XML object to string. Is there a solution for doing so, yet? This code: import xml.etree.ElementTree as ET e_feed = ET.Element("feed") e_feed.set("xmlns", "http://www.w3.org/2005/Atom") xslt_reference = ET.ProcessingInstruction( "xml-stylesheet", "type=\"text/xml\" href=\"stylesheet.xsl\"") e_feed.insert(0, xslt_reference) Would yield this result: <?xml version='1.0' encoding='utf-8'?> <feed xmlns="http://www.w3.org/2005/Atom"> <?xml-stylesheet type="text/xml" href="stylesheet.xsl"?> </feed> Yet, the intended resukt is: <?xml version='1.0' encoding='utf-8'?> <?xml-stylesheet type="text/xml" href="stylesheet.xsl"?> <feed xmlns="http://www.w3.org/2005/Atom"> </feed> That is, the instruction should be above element "feed". Please advise. On Fri, 31 Jan 2025 18:02:06 -0000 Neil Donaldson via lxml - The Python XML Toolkit <lxml@python.org> wrote:
Just configure the output encoding explicitly or force the XML declaration to be written.
ET.ElementTree(e_feed).write("chathappy.atom", encoding="unicode", xml_declaration=True) Thank you.
On Sat Jun 14, 2025 at 8:34 PM CEST, Schimon Jehudah via lxml - The Python XML Toolkit wrote:
I want to append instructions for XSLT without converting an XML object to string.
Is there a solution for doing so, yet?
This code:
import xml.etree.ElementTree as ET
As somebody suggested, this is ElementTree from the standard library, not lxml. Your best contact is probably https://discuss.python.org , not this list. Best, Matěj -- http://matej.ceplovi.cz/blog/, @mcepl@en.osm.town GPG Finger: 3C76 A027 CA45 AD70 98B5 BC1D 7920 5802 880B C9D8 Give a man a regular expression and he’ll match a string… teach him to make his own regular expressions and you’ve got a man with problems. -- yakugo in http://regex.info/blog/2006-09-15/247#comment-3022
Matěj. Good afternoon. You are correct. I was utilizing the built-in xml module. Now, I realize my mistake. This list is relevant to me, and from now on, I will post about concerns of lxml, that are not of the built-in module xml. Thank you, Schimon On Thu, 19 Jun 2025 09:03:31 +0200 Matěj Cepl <mcepl@cepl.eu> wrote:
On Sat Jun 14, 2025 at 8:34 PM CEST, Schimon Jehudah via lxml - The Python XML Toolkit wrote:
I want to append instructions for XSLT without converting an XML object to string.
Is there a solution for doing so, yet?
This code:
import xml.etree.ElementTree as ET
As somebody suggested, this is ElementTree from the standard library, not lxml. Your best contact is probably https://discuss.python.org , not this list.
Best,
Matěj
participants (5)
-
jholg@gmx.de -
Matěj Cepl -
Neil Donaldson -
Schimon Jehudah -
Stefan Behnel