2016-07-26 88 views
1

我有相當複雜的XML這樣的工作的工作:LXML與命名空間

<?xml version="1.0" encoding="UTF-8"?> 
<!-- ***** Configuration Data exported at 20160623T110335 ***** --> 
<impex:ExportData xmlns:impex="urn:swift:saa:xsd:impex"> 

<!-- *** Exported Data for Operator *** --> 
<OperatorData xmlns="urn:swift:saa:xsd:impex:operator"> 

<ns2:OperatorDefinition xmlns="urn:swift:saa:xsd:operatorprofile" xmlns:ns2="urn:swift:saa:xsd:impex:operator" xmlns:ns3="urn:swift:saa:xsd:unit" xmlns:ns4="urn:swift:saa:xsd:licenseddestination" xmlns:ns5="urn:swift:saa:xsd:operator" xmlns:ns6="urn:swift:saa:xsd:authenticationservergroup"> 
    <ns2:Operator> 
     <ns5:Identifier> 
      <ns5:Name>jdoe</ns5:Name> 
     </ns5:Identifier> 
     <ns5:Description>John Doe</ns5:Description> 
     <ns5:OperatorType>HUMAN</ns5:OperatorType> 
     <ns5:AuthenticationType>LDAP</ns5:AuthenticationType> 
     <ns5:AuthenticationServerGroup> 
      <ns6:Type>LDAP</ns6:Type> 
      <ns6:Name>LDAP_GROUP1</ns6:Name> 
     </ns5:AuthenticationServerGroup> 
     <ns5:LdapUserId>jdoe</ns5:LdapUserId> 
     <ns5:Profile> 
      <Name>DEV Users</Name> 
     </ns5:Profile> 
     <ns5:Unit> 
      <ns3:Name>None</ns3:Name> 
     </ns5:Unit> 
    </ns2:Operator> 
</ns2:OperatorDefinition> 

</OperatorData> 

</impex:ExportData> 

在這個XML有許多<ns2:OperatorDefinition>元素像我包括在內。我很難理解如何使用lxml提取類似<ns5:Description>的東西。所有我找到的命名空間的例子都不是這個複雜的。

我想只需找到做這樣的事情的標籤 -

from lxml import etree 
doc = etree.parse('c:/robin/Operators_out.xml') 

r = doc.xpath('/x:OperatorData/ns2:OperatorDefinition', namespaces={'x': 'urn:swift:saa:xsd:impex:operator'}) 
print len(r) 
print r[0].text 
print r[0].tag 

我得到Undefined namespace prefix

+1

由於您尚未在'namespaces'字典中包含'ns2'前綴的定義,因此您會得到「未定義的命名空間前綴」。 – mzjn

回答

1

您可能不需要爲您的使用情況,remove them命名空間,使分析更容易:

from lxml import etree, objectify 

tree = etree.parse("input.xml") 
root = tree.getroot() 

# remove namespaces ---- 
for elem in root.getiterator(): 
    if not hasattr(elem.tag, 'find'): continue 
    i = elem.tag.find('}') 
    if i >= 0: 
     elem.tag = elem.tag[i+1:] 

objectify.deannotate(root, cleanup_namespaces=True) 
# ---- 

name = root.findtext(".//OperatorDefinition/Operator/Identifier/Name") 
print(name) 

打印jdoe

+0

這適用於單一用戶我試圖找出如何循環它,如果有多個'OperatorDefinitions'與他們在相同的標籤?即。我想返回'jdoe','asmith'等。 – whoisearth

+0

@whoisearth沒問題,在'.//運算符定義/運算符/標識符/名稱/文本()'。 – alecxe

+0

@alecse我最終這樣做,它的工作原理 - '對於elem in tree.iterfind('.// OperatorDefinition'): print elem.findtext(「.// Operator/Identifier/Name」)' – whoisearth