【问题标题】:Python: QName on node's attribute, lxmlPython:节点属性上的 QName,lxml
【发布时间】:2014-08-18 09:35:31
【问题描述】:

在 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"/>

【问题讨论】:

    标签: python xml elementtree


    【解决方案1】:

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

    <!-- 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。那里隐含地指出,属性值中的前缀并不重要,只是实际命名空间的占位符。

    【讨论】:

    • 这是我最终做的,但它不是一个非常干净的解决方案,它很难编码,谢谢你的建议!
    猜你喜欢
    • 2012-12-23
    • 1970-01-01
    • 2014-08-22
    • 1970-01-01
    • 1970-01-01
    • 2014-06-13
    • 1970-01-01
    • 2021-09-07
    • 1970-01-01
    相关资源
    最近更新 更多