如何在lxml中为属性添加命名空间

时间:2023-01-14 08:53:58

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

我正在尝试使用python和lxml创建一个看起来像这样的xml条目:

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

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

2 个解决方案

#1


16  

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"

adlcp不是名称空间,它是名称空间前缀。命名空间在文档中由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).

在lxml中,您始终设置包含命名空间的元素/属性名称,例如{http:// xxx / yy / zzz} scormtype而不仅仅是scormtype。然后lxml将自动放入名称空间前缀。但是lxml会将前缀设置为ns0或类似,除非你做更多的小提琴,但这应该足够,因为前缀并不意味着什么。 (但有些人更喜欢控制前缀名称;请参阅Element和SubElement函数的nsmap参数以及register_namespace函数)。

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

我会看一下关于命名空间的lxml教程,还会看到Python - XML章节

#2


5  

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

#1


16  

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"

adlcp不是名称空间,它是名称空间前缀。命名空间在文档中由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).

在lxml中,您始终设置包含命名空间的元素/属性名称,例如{http:// xxx / yy / zzz} scormtype而不仅仅是scormtype。然后lxml将自动放入名称空间前缀。但是lxml会将前缀设置为ns0或类似,除非你做更多的小提琴,但这应该足够,因为前缀并不意味着什么。 (但有些人更喜欢控制前缀名称;请参阅Element和SubElement函数的nsmap参数以及register_namespace函数)。

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

我会看一下关于命名空间的lxml教程,还会看到Python - XML章节

#2


5  

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