【问题标题】:Python libxml2: querying xml using xpathPython libxml2:使用 xpath 查询 xml
【发布时间】:2014-02-05 17:42:30
【问题描述】:

我正在尝试从命令行参数读取 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()沙发为名称且不打印任何内容

如何获得 1 和 2 和 3?

【问题讨论】:

    标签: python xml text xpath libxml2


    【解决方案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']
    

    【讨论】:

    • 嗨,伙计,你能给出程序的前几行吗?我得到 ttributeError: 'ElementTree' object has no attribute 'xpath' 我该如何完成这个:def main(argv): elem_list=[] elem_num=0 尝试:xmlfile=argv[0] doc=ET.parse(xmlfile) root=doc。 getroot() 为 root 中的孩子:`
    【解决方案2】:

    我不熟悉 Python,但应该使用以下 XPath:

    1.) //*/@xmi:id

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

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

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

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

    【讨论】:

    • 谢谢,但你能给我完整的命令吗,只是想知道我是否遗漏了什么。我收到 xmlXpathEval() 失败错误。 :( 我可以在每个节点上使用 xpath 吗?
    猜你喜欢
    • 2012-01-23
    • 2018-10-22
    • 1970-01-01
    • 1970-01-01
    • 2010-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多