How to add xml-stylesheet node processing instruction with Python 2.6 and mini-mini?

I am creating an XML document using minidom - how can I ensure that my resulting XML document contains a link to a stylesheet, for example:

<?xml-stylesheet type="text/xsl" href="mystyle.xslt"?> 

Thanks!

+4
source share
2 answers

Use something like this:

 from xml.dom import minidom xml = """ <root> <x>text</x> </root>""" dom = minidom.parseString(xml) pi = dom.createProcessingInstruction('xml-stylesheet', 'type="text/xsl" href="mystyle.xslt"') root = dom.firstChild dom.insertBefore(pi, root) print dom.toprettyxml() 

=>

 <?xml version="1.0" ?> <?xml-stylesheet type="text/xsl" href="mystyle.xslt"?> <root> <x> text </x> </root> 
+7
source

I am not familiar with the minid, but you should create a node (PI) processing instruction with the name: "xml-stylesheet" and text: "type = 'text / xsl' href = 'mystyle.xslt'"

Read the documentation for creating PI.

+2
source

Source: https://habr.com/ru/post/1347895/


All Articles