Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>From <a href="http://docs.python.org/2/library/xml.etree.elementtree.html" rel="nofollow noreferrer">the documentation page</a>, the XML declaration can be added like this:</p> <pre><code>tree.write('/home/AlAhAb65/Desktop/output1.xml', xml_declaration=True) </code></pre> <p>You should also add the encoding because the default one is us-ascii:</p> <pre><code>tree.write('/home/AlAhAb65/Desktop/output1.xml', encoding='utf-8', xml_declaration=True) </code></pre> <p>Or you can retrieve the encoding from the original file, but in any case you will get a different XML declaration, probably something like this:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; </code></pre> <p>Or you can manually add the XML declaration. Anyway a slight declaration mismatch should not be a problem for any robust XML parser as long as the declared encoding is coherent with the real encoding.</p> <hr> <p>Attribute order is not significant in XML, so the information is probably lost when the file is parsed within the API. There is probably <a href="https://stackoverflow.com/questions/2741480/can-elementtree-be-told-to-preserve-the-order-of-attributes">no simple way</a> to make this work when processing the file through the standard ElementTree API. You would probably better have to go with <a href="http://lxml.de/api.html#write-c14n-on-elementtree" rel="nofollow noreferrer">lxml C14N</a> support if you want to do minor changes to the file.</p> <hr> <p>The namespace prefixes are changed by default in ElementTree. To prevent this behavior, you can switch to <a href="http://lxml.de/" rel="nofollow noreferrer">lxml</a> which seems to preserve namespace prefixes by default:</p> <blockquote> <p>Because etree is built on top of libxml2, which is namespace prefix aware, etree preserves namespaces declarations and prefixes while ElementTree tends to come up with its own prefixes (ns0, ns1, etc). When no namespace prefix is given, however, etree creates ElementTree style prefixes as well.</p> </blockquote> <p>Switching to lxml is a good idea in any case, but the changes you observe should not be a problem if the program reading the file at the other end is XML compliant enough. Unfortunately a lot of XPath processors have issues with namespace prefixes changes...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload