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 _______________________________________________ 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