在具有命名空间的XML文档中使用lxml.xpath给定rdf:ID的Seach元素

在具有命名空间的XML文档中使用lxml.xpath给定rdf:ID的Seach元素

问题描述:

我有一个cim/xml格式的xml文档。所述文档包括两个命名空间在具有命名空间的XML文档中使用lxml.xpath给定rdf:ID的Seach元素

  • RDF,
  • CIM。

文档的一部分如下所示:

<?xml version='1.0' encoding='UTF-8'?> 
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:cim="http://iec.ch/TC57/2013/CIM-schema-cim16#"> 
    <cim:Terminal rdf:ID="_08d0270e-f753-4812-a1cc-0550d9864a23"> 
    <cim:IdentifiedObject.name>C:Y8CHTT402:ETTR:1</cim:IdentifiedObject.name> 
    <cim:Terminal.ConductingEquipment rdf:resource="#_93030a09-6aac-46b5-bf5b-f75b90841675"/> 
    <cim:ACDCTerminal.sequenceNumber>1</cim:ACDCTerminal.sequenceNumber> 
    </cim:Terminal> 
    <cim:Terminal rdf:ID="_5451fc7e-5d94-4d30-ab58-744ab841334d"> 
    <cim:IdentifiedObject.name>C:Y8CHTT402:ETTR:2</cim:IdentifiedObject.name> 
    <cim:Terminal.ConductingEquipment rdf:resource="#_93030a09-6aac-46b5-bf5b-f75b90841675"/> 
    <cim:ACDCTerminal.sequenceNumber>2</cim:ACDCTerminal.sequenceNumber> 
    </cim:Terminal> 
</rdf:RDF> 

我的目标是找到具有给定的rdf终端etree对象:ID

我能够使用etree.xpath查找给定类型的所有元素。我找到了使用lxml documentation的方法。

from lxml import etree 
root = etree.parse(my_file) 
RDFNS = "http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
CIMNS = "http://iec.ch/TC57/2013/CIM-schema-cim16#" 

all_objts = root.xpath('/y:RDF/x:Terminal' % nodeID, 
         namespaces={'x': CIMNS, 'y': RDFNS}) # This returns a list of all terminal objects 

但我无法获得唯一一个给定的RDF元素:ID:

nodeID = "_08d0270e-f753-4812-a1cc-0550d9864a23" 
tar_obj = root.xpath('/y:RDF/x:Terminal[@ID="%s"]' % nodeID, 
        namespaces={'x': CIMNS, 'y': RDFNS}) # Returns an empty list 

我发现a very similar post但它没有正确回答这个问题。

我想命名空间前缀添加到ID标签(如下图所示)

root.xpath('/y:RDF/x:PowerTransformer[y:@ID="%s"]' % nodeID, 
      namespaces={'x': CIMNS, 'y': RDFNS}) 

,但不起作用

File "lxml.etree.pyx", line 1507, in lxml.etree._Element.xpath (src\lxml\lxml.etree.c:52198) 

    File "xpath.pxi", line 307, in lxml.etree.XPathElementEvaluator.__call__ (src\lxml\lxml.etree.c:152124) 

    File "xpath.pxi", line 227, in lxml.etree._XPathEvaluatorBase._handle_result (src\lxml\lxml.etree.c:151097) 

    File "xpath.pxi", line 213, in lxml.etree._XPathEvaluatorBase._raise_eval_error (src\lxml\lxml.etree.c:150950) 

XPathEvalError: Invalid expression 

有没有一种方法来搜索具有多个名称空间的文档中使用etree.xpath的给定rdf:ID的对象?

有在此表达谓词的错误:

root.xpath('/y:RDF/x:PowerTransformer[y:@ID="%s"]' % nodeID, 
      namespaces={'x': CIMNS, 'y': RDFNS}) 

您需要更改[y:@ID="%s"][@y:ID="%s"]

+0

谢谢,这有帮助。 –