0

I am trying to create a xml whose first element is:

<speak version="1.0"
xmlns="http://www.w3.org/2001/10/synthesis" xml:lang="en-US">
</speak>

I am able to add the first attributes with...

from lxml.etree import Element, SubElement, QName, tostring 
root = Element('speak', version="1.0",
               xmlns="http://www.w3.org/2001/10/synthesis")

...but not the namespace xml:lang="en-US". Based on several tuto/question like this and this I tried many solutions but none worked.

For example, I tried this :

class XMLNamespaces:
    xml = 'http://www.w3.org/2001/10/synthesis'

root.attrib[QName(XMLNamespaces.xml, 'lang')] = "en-US"

But the ouput is

<speak xmlns:ns0="http://www.w3.org/2001/10/synthesis" version="1.0" xmlns="http://www.w3.org/2001/10/synthesis" ns0:lang="en-US">

How can I create the xml:lang="en-US" of my first xml element?

MagTun
  • 5,619
  • 5
  • 63
  • 104
  • 1
    The URI for the `xml` prefix is `http://www.w3.org/XML/1998/namespace`. See https://stackoverflow.com/a/17477289/407651 – mzjn Mar 30 '21 at 16:26
  • It's working, thanks so much! Please post it as an answer so I can upvote you! – MagTun Mar 30 '21 at 17:34

1 Answers1

1

The special xml: prefix is associated with the http://www.w3.org/XML/1998/namespace URI.

The following code adds xml:lang="en-US" to the root element:

root.attrib[QName("http://www.w3.org/XML/1998/namespace", "lang")] = "en-US"
mzjn
  • 48,958
  • 13
  • 128
  • 248