3

I have the following xml -

<draw:image></draw:image>

I want to add multiple xlink attributes to it and make it -

<draw:image xlink:href="image" xlink:show="embed"></draw:image>

I tried using the following code but got the error "ValueError: Invalid attribute name u'xlink:href'"

root.xpath("//draw:image", namespaces=
{"draw":"urn:oasis:names:tc:opendocument:xmlns:drawing:1.0"})
[0].attrib['xlink:href'] = 'image'

What am I doing wrong? There seems to be something related to namespaces, but I can't figure what.

8
  • Can you add a link to the actual file? Or at least a stripped down usable version with the namespace decs etc,.. Commented Jun 9, 2016 at 0:06
  • @PadraicCunningham Ah. Okay. Here are the namespaces - gist.github.com/shrox/df592e65a8848dd4f0ddab18cc340dd4 Commented Jun 9, 2016 at 0:14
  • Can you add a watered down version of the file? It will be easier to just show you a complete example. Commented Jun 9, 2016 at 0:28
  • @PadraicCunningham I did! The namespaces with the tag in question. gist.github.com/shrox/df592e65a8848dd4f0ddab18cc340dd4 Commented Jun 9, 2016 at 0:30
  • I just need to know how to make .attrib work with attributes with a prefix. Commented Jun 9, 2016 at 0:31

1 Answer 1

2

This is a working example:

from lxml import etree as et

xml = et.parse("your.xml")
root = xml.getroot()
d = root.nsmap

for node in root.xpath("//draw:image", namespaces=d):
    node.attrib["{http://www.w3.org/1999/xlink}href"] = "value"
    node.attrib["{http://www.w3.org/1999/xlink}show"] = "embed"
print(et.tostring(xml))

Which for:

<?xml version="1.0" encoding="utf-8"?>
<office:document xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0"
xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"
xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0"
xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0"
xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0"
xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0"
xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0"
xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0"
xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0"
xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0">
<draw:image></draw:image>

Outputs:

<office:document xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0">
<draw:image xlink:href="value" xlink:show="embed"/>


</office:document>

Or using set:

for node in root.xpath("//draw:image", namespaces=d):
    node.set("{http://www.w3.org/1999/xlink}href", "image")
    node.set("{http://www.w3.org/1999/xlink}show", "embed")
Sign up to request clarification or add additional context in comments.

1 Comment

You have no idea how helpful you've been. I've been having difficulty in understanding how namespaces work since yesterday and this has simplified it for me. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.