1

I have an xml file with this as the header

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type='text/xsl' href='\\segotn12805\ppr\PPRData3\StyleSheet\PPRData3.xslt'?>

when I modify the file I use .write (for example)

mytree.write('output.xml')

but the output file does not contain the header info. The first two lines of the output file look like this

<ns0:pprdata xmlns:ns0="http://ManHub.PPRData">
  <ns0:Group name="Models">

any ideas on how I can add the header info to the output file?

Barfield
  • 87
  • 1
  • 7

1 Answers1

1

The first line is the XML declaration. It is optional, and a parser will assume UTF-8 if not specified.

The second line is a processing instruction.

It would be helpful if you provided more code to show what you are doing, but I suspect that you are using ElementTree. The documentation has this note indicating that by default these are skipped:

Note Not all elements of the XML input will end up as elements of the parsed tree. Currently, this module skips over any XML comments, processing instructions, and document type declarations in the input. Nevertheless, trees built using this module’s API rather than parsing from XML text can have comments and processing instructions in them; they will be included when generating XML output. A document type declaration may be accessed by passing a custom TreeBuilder instance to the XMLParser constructor.

As suggested in this answer, you might want to try using lxml

Mads Hansen
  • 63,927
  • 12
  • 112
  • 147