How to set xml:base attribute with ElementMaker?

Dear all, I have to generate XML files that contain snippets like this one, but I can't find a way to produce a "xml:base" attribute: <markList xmlns:xlink="http://www.w3.org/1999/xlink" type="tok" xml:base="maz-1423.text.xml"> <mark id="sTok1" xlink:href="#xpointer(string-range(//body,'',1,3))" /> <mark id="sTok2" xlink:href="#xpointer(string-range(//body,'',5,10))" /> </markList> I tried to setup the namespaces, but instead of "xml:base", I'll only get "ns0:base". NSMAP={None: 'xml', 'xlink': 'http://www.w3.org/1999/xlink', 'xml': 'xml'} E = ElementMaker(nsmap=NSMAP) etree.tostring(E("markList", {'type': 'tok', '{%s}base' % NSMAP['xml']: 'maz-1423.text.xml'})) '<markList xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="xml" xmlns:ns0="xml" ns0:base="maz-1423.text.xml" type="tok"/>' I'd appreciate any help with this. Best regards, Arne Neumann

Hi,
I have to generate XML files that contain snippets like this one, but I can't find a way to produce a "xml:base" attribute:
<markList xmlns:xlink="http://www.w3.org/1999/xlink" type="tok" xml:base="maz-1423.text.xml"> <mark id="sTok1" xlink:href="#xpointer(string-range (//body,'',1,3))" /> <mark id="sTok2" xlink:href="#xpointer(string-range (//body,'',5,10))" /> </markList>
I tried to setup the namespaces, but instead of "xml:base", I'll only get "ns0:base".
NSMAP={None: 'xml', 'xlink': 'http://www.w3.org/1999/xlink', 'xml': 'xml'}
E = ElementMaker(nsmap=NSMAP) etree.tostring(E("markList", {'type': 'tok', '{%s}base' % NSMAP['xml']: 'maz-1423.text.xml'}))
'<markList xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="xml" xmlns:ns0="xml" ns0:base="maz-1423.text.xml" type="tok"/>'
The prefix 'xml' is special and bound to the namespace http://www.w3.org/XML/1998/namespace by definition (see http://www.w3.org/XML/1998/namespace :)). AFAICT lxml respects that so I guess your NSMAP entry NSMAP = {... 'xml': 'xml'} gets ignored and an additional ns0 prefix is created for your namespace 'xml'. That said, you should be able to just use the predefined prefix + namespace combo:
print etree.tostring(builder.ElementMaker()('foo', {'{http://www.w3.org/XML/1998/namespace}base': '/my/base/URI'})) <foo xml:base="/my/base/URI"/>
If you want a base attribute from another custom namespace you can't use the special prefix 'xml' for this, though. Holger Landesbank Baden-Wuerttemberg Anstalt des oeffentlichen Rechts Hauptsitze: Stuttgart, Karlsruhe, Mannheim, Mainz HRA 12704 Amtsgericht Stuttgart
participants (2)
-
Arne Neumann
-
Holger Joukl