如何在Python的lxml中使用xpath找到的标签中添加属性? [英] How to add an attribute to a tag found using xpath in lxml in Python?

查看:313
本文介绍了如何在Python的lxml中使用xpath找到的标签中添加属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下xml-

<draw:image></draw:image>

我想为其添加多个xlink属性,并使其-

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

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

我尝试使用以下代码,但收到错误"ValueError:无效的属性名称u'xlink:href'"

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.

推荐答案

这是一个有效的示例:

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))

其中:

<?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>

输出:

<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>

或使用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")

这篇关于如何在Python的lxml中使用xpath找到的标签中添加属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆