gpt4 book ai didi

Python:节点属性 lxml 上的 QName

转载 作者:太空宇宙 更新时间:2023-11-04 03:43:04 25 4
gpt4 key购买 nike

在 Python 的 lxml.etree 中,如何将命名空间添加到节点的属性中,如下所示: namespace 是:

XS_NS = 'http://www.w3.org/2001/XMLSchema'

寻找的结果是:

<xs:element name="label" type="xs:string"></xs:element> 

我试过这个,但结果不是我想要的:

element = ET.SubElement(
sequence,
ET.QName(XS_NS, "element"),
name="label",
type=str(ET.QName(XS_NS, "string")),
)

给我:

<xs:element name="label" type="{http://www.w3.org/2001/XMLSchema}string"/>

最佳答案

据我所知,虽然属性本身可能有命名空间,但属性的值不考虑命名空间:

<!-- this attribute has namespace prefix -->
<dummy xs:foo="bar"/>
<!-- this attribute has value of string containing colon -->
<dummy foo="xs:bar"/>

所以你可以简单地将“前缀”和“值”作为一个字符串放在一起:

type="xs:string"

相关讨论:XML Schema. Processing namespace-aware attribute values .那里隐含地指出属性值中的前缀并不重要,只是实际命名空间的占位符。

关于Python:节点属性 lxml 上的 QName,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25360191/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com