【问题标题】:Python ElementTree NamespacesPython ElementTree 命名空间
【发布时间】:2023-04-01 18:22:01
【问题描述】:

环境:
蟒蛇 2.7
视窗 8.1

示例 XML:

<?xml version="1.0" encoding="UTF-8"?>
<edmx:Edmx xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx" Version="1.0">
    <edmx:DataServices xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" m:MaxDataServiceVersion="3.0" m:DataServiceVersion="3.0">
        <Schema xmlns="http://schemas.microsoft.com/ado/2009/11/edm" Namespace="DataModel">
            <EntityContainer Name="EC1" xmlns:p6="http://schemas.microsoft.com/ado/2009/02/edm/annotation" p6:LazyLoadingEnabled="true" m:IsDefaultEntityContainer="true">
                <EntitySet Name="Attributes" EntityType="Model.Attribute"/>
                <EntitySet Name="AttributeControlTypes" EntityType="Model.AttributeControlType"/>
            </EntityContainer>
        </Schema>
    </edmx:DataServices>
</edmx:Edmx>

测试代码:

import xml.etree.ElementTree as ET
from cStringIO import StringIO

def parse(self):
    events = ("end", "start-ns", "end-ns", "start")
    namespaces = []
    for event, elem in ET.iterparse(StringIO(self.document), events=events):
        if event == "start-ns":
            print "startns: {}".format(elem)
            namespaces.append(elem)
        elif event == "end-ns":
            removed = namespaces.pop()
            print "removedns: {}".format(removed)
        elif event == "start":
            print elem.tag

我的理解是,如果一个标签附加了一个命名空间,那么所有子元素也应该在同一个命名空间中。当我尝试解析上述 XML 时,我希望“entitySet”元素位于“EntityContainer”中定义的命名空间中:

输出:

startns: (u'edmx', 'http://schemas.microsoft.com/ado/2007/06/edmx')
{http://schemas.microsoft.com/ado/2007/06/edmx}Edmx
startns: (u'm', 'http://schemas.microsoft.com/ado/2007/08/dataservices/metadata')
{http://schemas.microsoft.com/ado/2007/06/edmx}DataServices
startns: ('', 'http://schemas.microsoft.com/ado/2009/11/edm')
{http://schemas.microsoft.com/ado/2009/11/edm}Schema
startns: (u'p6', 'http://schemas.microsoft.com/ado/2009/02/edm/annotation')
{http://schemas.microsoft.com/ado/2009/11/edm}EntityContainer
{http://schemas.microsoft.com/ado/2009/11/edm}EntitySet
{http://schemas.microsoft.com/ado/2009/11/edm}EntitySet
removedns: (u'p6', 'http://schemas.microsoft.com/ado/2009/02/edm/annotation')
removedns: ('', 'http://schemas.microsoft.com/ado/2009/11/edm')
removedns: (u'm', 'http://schemas.microsoft.com/ado/2007/08/dataservices/metadata')
removedns: (u'edmx', 'http://schemas.microsoft.com/ado/2007/06/edmx')

ElementTree 看到命名空间的开始(p6),但似乎将“EntityContainer”子元素添加到“Schema”元素的命名空间中。这是否按预期工作?

【问题讨论】:

    标签:
    python
    xml
    elementtree