Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在具有名称空间的XML文档中使用lxml.xpath搜索具有给定rdf:ID的元素_Python_Xml_Xpath_Xml Parsing_Lxml - Fatal编程技术网

Python 在具有名称空间的XML文档中使用lxml.xpath搜索具有给定rdf:ID的元素

Python 在具有名称空间的XML文档中使用lxml.xpath搜索具有给定rdf:ID的元素,python,xml,xpath,xml-parsing,lxml,Python,Xml,Xpath,Xml Parsing,Lxml,我有一个cim/xml格式的xml文档。该文档包含两个名称空间 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

我有一个cim/xml格式的xml文档。该文档包含两个名称空间

  • 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: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
我已经找到了,但它没有正确回答这个问题

我想将名称空间前缀添加到ID标记中(如下所示)

,但这不起作用

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”]

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
root.xpath('/y:RDF/x:PowerTransformer[y:@ID="%s"]' % nodeID,
           namespaces={'x': CIMNS, 'y': RDFNS})