- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有以下 xml -
<draw:image></draw:image>
我想给它添加多个xlink属性并使其 -
<draw:image xlink:href="image" xlink:show="embed"></draw:image>
我尝试使用以下代码,但收到错误“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'
我做错了什么?似乎有一些与命名空间相关的东西,但我不知道是什么。
最佳答案
这是一个工作示例:
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>
或者使用集合:
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 - 如何向在Python中使用lxml中的xpath找到的标签添加属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37714813/
我有以下xml文件 10.10.0.135::3111 10.10.0.130::3111 10.10.0.129::3111 10.
我已经为 PyPy 创建了一个 virtualenv: virtualenv test -p `which pypy` source test/bin/activate 我安装了以下依赖项: sudo
我正在尝试使用 lxml 的 ElementTree etree 在我的 xml 文档中查找特定标签。标签如下所示: 12 我希望使用 etree.find('text:statedAge
在 python 3.5 项目中,我必须读取一些 xml 文件,因此我决定使用 lxml 库。由于我正在阅读文件,因此根据文档执行此操作的最有效方法是使用 lxml.etree.parse(...)
执行 pip install lxml 或 pip install pyquery 会出现此错误: gcc: error trying to exec 'cc1': execvp: No such f
我正在使用下面的代码获取一个部分的所有 html 内容以保存到数据库 el = doc.get_element_by_id('productDescription') lxml.html.tostri
我想以可区分的方式打印出 etree 的树结构(由 html 文档形成)(意味着两个 etree 应该以不同的方式打印出来)。 我所说的结构是指树的“形状”,基本上是指所有标签,但没有属性,也没有文本
谁能解释为什么第一次调用 root.cssselect() 有效,而第二次失败? from lxml.html import fromstring from lxml import etree htm
我收到此错误“ImportError: No module named lxml”,即使确实安装了 LXML。具体来说,它安装在项目的 python Virtualenv 中。最终我正在研究 Pyth
我在 Windows 32 位上使用 Anaconda v4.2 和 Python 3.5,并想使用 lxml etree。我的 Anaconda 发行版包括 lxml 3.6.4,但我的 IDE(P
我有一个脚本,可以从 URL 列表的 XML 文件中提取一些术语。所有 URL 都可以访问 XML 数据。 它在第一次正确打开、解析和提取时工作正常,但随后在过程中被某些 XML 文件中断并出现此错误
我正在使用 mechanize/cookiejar/lxml 来读取页面,它适用于某些页面但不适用于其他页面。我在其中遇到的错误是标题中的错误。我不能在这里发布页面,因为它们不是 SFW,但是有没有办
这是一个基本的问题,我实际上在文档中找不到它:-/ 如下: img = house_tree.xpath('//img[@id="mainphoto"]')[0] 如何获取 的 HTML标记? 我尝
我只想说,这个问题我已经在Pip is already installed: but I am getting no module named lxml看到了并且已经看到关于以非 root 用户身份安
我通过 pip 安装了 lxml 3.3.5。现在我在运行一些 Django 测试时遇到了问题: Traceback (most recent call last): File "manage.p
我有一个 Django 应用程序,其中包含 Tastypie 提供的 RESTFull 服务。我使用 easy_install 安装 lxml 和 defusedxml,这样我就可以使用 xml 而不
我正在使用 lxml etree 创建 xml 或 REST 调用。我有命名空间的问题,因为如果没有正确制定,我会从服务器收到语法错误。 正如您在以下 2 个示例中所看到的,我应该得到例如 ns1、n
我对导航具有 lxml.etree namespace 的 xml 文档感到有些困惑。 .我已经看到了一些关于这个主题的主题( 1 、 2 )以及 lxml docs但仍然没有想出答案。 xml =
由于各种原因,我试图从lxml.html.fromstring()切换到lxml.html.html5parser.document_fromstring()。两者之间的最大区别是,第一个返回lxml
我需要通过向现有元素添加子元素来修改现有的 xml 文件。我使用 lxml 库。 Eric Idle 999-999-999 555-555-555
我是一名优秀的程序员,十分优秀!