如何使用 LXML 编写命名空间元素属性?

发布于 2024-12-09 01:23:16 字数 957 浏览 2 评论 0原文

我正在使用 lxml (2.2.8) 创建和写出一些 XML(特别是 XGMML)。将读取它的应用程序显然相当挑剔 并希望查看顶级元素:

<graph label="Test" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:xlink="h
ttp://www.w3.org/1999/xlink" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-
ns#" xmlns:cy="http://www.cytoscape.org" xmlns="http://www.cs.rpi.edu/XGMML"  di
rected="1">

如何使用 lxml 设置这些 xmlns: 属性?如果我尝试明显的

root.attrib['xmlns:dc']='http://purl.org/dc/elements/1.1/'
root.attrib['xmlns:xlink']='http://www.w3.org/1999/xlink'
root.attrib['xmlns:rdf']='http://www.w3.org/1999/02/22-rdf-syntax-ns#'
root.attrib['xmlns:cy']='http://www.cytoscape.org'
root.attrib['xmlns']='http://www.cs.rpi.edu/XGMML'

lxml 抛出 ValueError: Invalid attribute name u'xmlns:dc'

我过去曾大量使用 XML 和 lxml 来完成简单的事情,但设法避免需要知道到目前为止有关命名空间的任何内容。

I'm using lxml (2.2.8) to create and write out some XML (specifically XGMML). The app which will be reading it is apparently fairly fussy and wants to see a top level element with:

<graph label="Test" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:xlink="h
ttp://www.w3.org/1999/xlink" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-
ns#" xmlns:cy="http://www.cytoscape.org" xmlns="http://www.cs.rpi.edu/XGMML"  di
rected="1">

How do I setup those xmlns: attributes with lxml ? If I try the obvious

root.attrib['xmlns:dc']='http://purl.org/dc/elements/1.1/'
root.attrib['xmlns:xlink']='http://www.w3.org/1999/xlink'
root.attrib['xmlns:rdf']='http://www.w3.org/1999/02/22-rdf-syntax-ns#'
root.attrib['xmlns:cy']='http://www.cytoscape.org'
root.attrib['xmlns']='http://www.cs.rpi.edu/XGMML'

lxml throws a ValueError: Invalid attribute name u'xmlns:dc'

I've used XML and lxml a fair amount in the past for simple stuff, but managed to avoid needing to know anything about namespaces so far.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝海似她心 2024-12-16 01:23:16

与 ElementTree 或其他允许这样做的序列化程序不同,lxml 需要您事先设置这些命名空间:(

NSMAP = {"dc" : 'http://purl.org/dc/elements/1.1',
         "xlink" : 'http://www.w3.org/1999/xlink'}

root = Element("graph", nsmap = NSMAP)

对于其余声明,依此类推)

然后您可以使用这些命名空间正确的声明:

n = SubElement(root, "{http://purl.org/dc/elements/1.1}foo")

当然,这对于输入来说很烦人,因此将路径分配给短常量名称通常是有益的:

DCNS = "http://purl.org/dc/elements/1.1"

然后在 NSMAPSubElement 中使用该变量> 声明:

n = SubElement(root, "{%s}foo" % (DCNS))

Unlike ElementTree or other serializers that would allow this, lxml needs you to set up these namespaces beforehand:

NSMAP = {"dc" : 'http://purl.org/dc/elements/1.1',
         "xlink" : 'http://www.w3.org/1999/xlink'}

root = Element("graph", nsmap = NSMAP)

(and so on and so forth for the rest of the declarations)

And then you can use the namespaces using their proper declarations:

n = SubElement(root, "{http://purl.org/dc/elements/1.1}foo")

Of course this gets annoying to type, so it is generally beneficial to assign the paths to short constant names:

DCNS = "http://purl.org/dc/elements/1.1"

And then use that variable in both the NSMAP and the SubElement declarations:

n = SubElement(root, "{%s}foo" % (DCNS))
不知所踪 2024-12-16 01:23:16

使用 ElementMaker

import lxml.etree as ET
import lxml.builder as builder
E = builder.ElementMaker(namespace='http://www.cs.rpi.edu/XGMML',
                         nsmap={None: 'http://www.cs.rpi.edu/XGMML',
                         'dc': 'http://purl.org/dc/elements/1.1/',
                         'xlink': 'http://www.w3.org/1999/xlink',
                         'rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
                         'cy': 'http://www.cytoscape.org', })
graph = E.graph(label="Test", directed="1")
print(ET.tostring(graph, pretty_print=True))

产量

<graph xmlns:cy="http://www.cytoscape.org" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.cs.rpi.edu/XGMML" directed="1" label="Test"/>

Using ElementMaker:

import lxml.etree as ET
import lxml.builder as builder
E = builder.ElementMaker(namespace='http://www.cs.rpi.edu/XGMML',
                         nsmap={None: 'http://www.cs.rpi.edu/XGMML',
                         'dc': 'http://purl.org/dc/elements/1.1/',
                         'xlink': 'http://www.w3.org/1999/xlink',
                         'rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
                         'cy': 'http://www.cytoscape.org', })
graph = E.graph(label="Test", directed="1")
print(ET.tostring(graph, pretty_print=True))

yields

<graph xmlns:cy="http://www.cytoscape.org" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.cs.rpi.edu/XGMML" directed="1" label="Test"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文