Python libxml2:使用xpath查询xml

时间:2014-02-05 17:42:31

标签: python xml text xpath libxml2

我正在尝试从命令行参数中读取XML文件。我是新手一般使用libxml2和XPath。我想使用XPath进行查询。

XML:

<?xml version="1.0"?>                                                                                                                                     
<xmi:XMI xmlns:cas="http:///text/cas.ecore" xmlns:audioform="http:something" xmlns:xmi="http://blahblah" xmlns:lib="http://blahblah" xmlns:solr="http:blahblah" xmlns:tcas="http:///blah" xmi:version="2.0">                                                
  <cas:NULL xmi:id="0"/>                                                                                                                                     
  <cas:Sofa xmi:id="9" Num="1" ID="First" Type="text" String="play a song"/>    
  <cas:Sofa xmi:id="63" Num="2" ID="Second" Type="text" String="Find a contact"/>     
  <cas:Sofa xmi:id="72" Num="3" ID="Third" Type="text" String="Send a message"/>     
  <lib:Confidence xmi:id="1" sofa="9" begin="0" end="1" key="context" value="" confidence="1.0"/>                                                                          
</xmi:XMI>

代码:

def main(argv):
  try:
     xmlfile=argv[0]
     doc=libxml2.parseFile(xmlfile)
     root2=doc.children

     print root2  # This prints everything but <?xml version="1.0"?> 
     result= root2.xpathEval("//*")

     for node in result:
       print node
       print node.nodePath(), node.name, node.content

我想进一步使用此文件进行某种处理。

  1. 如何使用xpath获取63之类的值?来自xmi:id="63"
  2. 查找xmi:id = "72"所在的字符串。结果应为“发送消息”
  3. 查找xmi:id = 72 and ID= "Third"的字符串。结果应为“发送消息”
  4. 我尝试将node.Path()node.namenode.content用于此节点:

    <cas:Sofa xmi:id="9" Num="1" ID="First" Type="text" String="play a song"/>
    

    结果为:/xmi:XMI/cas:Sofa[1]nodePath()沙发为名称,不打印任何内容

  5. 如何获得1和2和3?

2 个答案:

答案 0 :(得分:1)

关于命名空间:

>>> from lxml import etree
>>> doc = etree.parse('in.html')
>>> names = {'cas':'http:///text/cas.ecore', 'xmi': 'http://blahblah'}
>>> doc.xpath('//cas:Sofa[@xmi:id="63"]', namespaces=names)
[<Element {http:///text/cas.ecore}Sofa at 0x10550a5f0>]
>>> doc.xpath('//cas:Sofa[@xmi:id="63"]/@String', namespaces=names)
['Find a contact']
>>> doc.xpath('//cas:Sofa[@xmi:id="72" and @ID="Third"]/@String', namespaces=names)
['Send a message']

答案 1 :(得分:0)

我不熟悉Python,但以下XPath应该这样做:

1。)//*/@xmi:id

2。)//*[@xmi:id='72']/@String

3。)//*[@xmi:id='72' and @ID='Third']/@String

使用@选择属性,使用括号([])创建条件。

请注意您的XML使用名称空间。您应该考虑更具体的XPath(//*)并使用命名空间管理器,而不是只选择所有内容(/xmi:XMI/cas:Sofa)。