@kripton3000
Бекенд- и немного фронтенд-разработчик

Почему не получается сгенерировать Python XML?

Нужно создать XML такого вида:
<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>


Делаю
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))


Получаю
<?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>


Как сгенерировать правильное пространство имен?
  • Вопрос задан
  • 3399 просмотров
Решения вопроса 1
@Vader
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))
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы