Python:lxml中节点属性的QName
在Python的lxml.etree库中,我想给一个节点的属性添加命名空间,像这样:
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"/>
1 个回答
2
据我所知,虽然一个属性本身可以有命名空间,但这个属性的值并不考虑命名空间。
<!-- 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. 处理考虑命名空间的属性值。在这里,隐含地说明了属性值中的前缀并不重要,它只是实际命名空间的占位符。