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

天涯浪子 提交于 2019-12-01 06:15:40

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>

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

Read the documentation how a PI is created.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!