
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