V
V
Vladislav Shepilov2014-02-04 13:03:44
Python
Vladislav Shepilov, 2014-02-04 13:03:44

Why can't I generate Python XML?

You need to create XML like this:

<feed xmlns="http://www.ixtens.com/xml/mp/override/R1.1“ xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance“ xsi:schemaLocation=”http://www.ixtens.com/xml/mp/override/R1.1 http://support.ixtens.com/mp/R1.1/product/override.xsd">
…
Данные
…
</feed>

I do
import lxml.etree
xmlns = "http://www.ixtens.com/xml/mp/override/R1.1"
xmlns_xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi_schemaLocation = "http://www.ixtens.com/xml/mp/override/R1.1 http://support.ixtens.com/mp/R1.1/product/override.xsd"
NSMAP = {None: xmlns}
new_feed = lxml.etree.Element('feed', nsmap=NSMAP)
new_feed = lxml.etree.Element('feed')
new_feed.set('xmlns', xmlns)
head = lxml.etree.SubElement(new_feed, 'data')
new_feed.set('{http://www.ixtens.com/xml/mp/override/R1.1}xsi', xmlns_xsi)
new_feed.set('{http://www.w3.org/2001/XMLSchema-instance}schemaLocation', xsi_schemaLocation)
print(lxml.etree.tostring(new_feed, pretty_print=True, encoding='utf-8', xml_declaration=True))

I receive
<?xml version='1.0' encoding='utf-8'?>
<feed xmlns:ns0="http://www.ixtens.com/xml/mp/override/R1.1“ xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance“ xmlns=”http://www.ixtens.com/xml/mp/override/R1.1“ ns0:xsi=”http://www.w3.org/2001/XMLSchema-instance“ xsi:schemaLocation=”http://www.ixtens.com/xml/mp/override/R1.1 http://support.ixtens.com/mp/R1.1/product/override.xsd">
<data/>
</feed>

How to generate the correct namespace?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vader, 2014-02-04
@kripton3000

import lxml.etree
xmlns = "http://www.ixtens.com/xml/mp/override/R1.1"
xmlns_xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi_schemaLocation = "http://www.ixtens.com/xml/mp/override/R1.1 http://support.ixtens.com/mp/R1.1/product/override.xsd"
NSMAP = {None: xmlns, 'xsi': xmlns_xsi}
new_feed = lxml.etree.Element('feed', nsmap=NSMAP)
new_feed = lxml.etree.Element('feed')
new_feed.set('xmlns', xmlns)
head = lxml.etree.SubElement(new_feed, 'data')
new_feed.set('{http://www.w3.org/2001/XMLSchema-instance}schemaLocation', xsi_schemaLocation)
print(lxml.etree.tostring(new_feed, pretty_print=True, encoding='utf-8', xml_declaration=True))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question