如何向 lxml 中的属性添加命名空间

发布于 2024-08-03 07:43:27 字数 376 浏览 4 评论 0原文

我正在尝试使用 python 和 lxml 创建一个如下所示的 xml 条目:

<resource href="Unit 4.html" adlcp:scormtype="sco">

我正在使用 python 和 lxml。我在使用 adlcp:scormtype 属性时遇到问题。我是 xml 新手,如果我错了,请纠正我。 adlcp 是一个命名空间,scormtype 是在 adlcp 命名空间中定义的属性,对吗?
我什至不确定这是否是正确的问题,但是...我的问题是,如何使用 lxml 将属性添加到非默认命名空间中的元素?如果这是一个微不足道的问题,我提前道歉。

I'm trying to create an xml entry that looks like this using python and lxml:

<resource href="Unit 4.html" adlcp:scormtype="sco">

I'm using python and lxml. I'm having trouble with the adlcp:scormtype attribute. I'm new to xml so please correct me if I'm wrong. adlcp is a namespace and scormtype is an attribute that is defined in the adlcp namespace, right?
I'm not even sure if this is the right question but... My question is, how do I add an attribute to an element from a non-default namespace using lxml? I apologize in advance if this is a trivial question.

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

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

发布评论

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

评论(2

摘星┃星的人 2024-08-10 07:43:27

这不是完整的答复,只是一些提示。

adlcp 不是命名空间,它是命名空间前缀。命名空间在文档中由诸如 xmlns:adlcp="http://xxx/yy/zzz" 之类的属性定义。

在 lxml 中,您始终设置包含命名空间的元素/属性名称,例如
{http://xxx/yy/zzz}scormtype 而不仅仅是 scormtype。然后,lxml 将自动添加名称空间前缀。
然而,lxml 会将前缀设置为 ns0 或类似的前缀,除非您进行更多的修改,但这应该足够了,因为前缀没有任何意义。 (然而,有些人更喜欢控制前缀名称;请参阅 Element 和 SubElement 函数以及 register_namespace 函数上的 nsmap 参数)。

我会查看 关于命名空间的 lxml 教程 以及 深入了解 Python - XML 章节

This is not a full reply but just a few pointers.

adlcp is not the namespace it is a namespace prefix. The namespace is defined in the document by an attribute like xmlns:adlcp="http://xxx/yy/zzz"

In lxml you always set an element/attribute name including the namespace e.g.
{http://xxx/yy/zzz}scormtype instead of just scormtype. lxml will then put in a namespace prefix automatically.
However lxml will set the prefix to ns0 or similar unless you do more fiddling but that should be sufficient as the prefix does not mean anything. (However some people prefer controlling the prefix name; see the nsmap argument on the Element and SubElement functions, and the register_namespace function).

I would look at the lxml tutorial on namespace and also Dive into Python - XML chapter

甜心 2024-08-10 07:43:27

试试这个:

builder = ElementMaker(namespace="http://a.different.url/blah/v.10",
                       nsmap={
                         'adlcp': "http://a.namespace.url/blah/v.10",
                         'anotherns': "http://a.different.url/blah/v.10"
                       })

builder.resource()
builder.attrib['href'] = "Unit 4.html"
builder.attrib['{http://a.namespace.url/blah/v.10}scormtype'] = 'sco'

print(etree.tostring(builder, pretty_print=True))

Try this:

builder = ElementMaker(namespace="http://a.different.url/blah/v.10",
                       nsmap={
                         'adlcp': "http://a.namespace.url/blah/v.10",
                         'anotherns': "http://a.different.url/blah/v.10"
                       })

builder.resource()
builder.attrib['href'] = "Unit 4.html"
builder.attrib['{http://a.namespace.url/blah/v.10}scormtype'] = 'sco'

print(etree.tostring(builder, pretty_print=True))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文