简体   繁体   English

如何使用XPath通过同级属性发现XML节点?

[英]How to spot an XML-node by attributes of its sibling using XPath?

Suppose I have the following XML-file (dispensable parts are marked with '...'): 假设我有以下XML文件(可有可无的部分标有“ ...”):

<?xml version="1.0" encoding="ISO-8859-1"?>
<PARAMETERS version="1.6.2" xsi:noNamespaceSchemaLocation="http://open-ms.sourceforge.net/schemas/Param_1_6_2.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <NODE name="info" description="">
    <ITEM name="version" value="2.0.0" type="string" description="" required="false" advanced="false" />
    <ITEM name="num_vertices" value="5" type="int" description="" required="false" advanced="false" />
    <ITEM name="num_edges" value="4" type="int" description="" required="false" advanced="false" />
    <ITEM name="description" value="&lt;![CDATA[]]&gt;" type="string" description="" required="false" advanced="false" />
  </NODE>
  <NODE name="vertices" description="">
    <NODE name="0" description="">
      <ITEM name="recycle_output" value="false" type="string" description="" required="false" advanced="false" />
      <ITEM name="toppas_type" value="input file list" type="string" description="" required="false" advanced="false" />
      <ITEMLIST name="file_names" type="string" description="" required="false" advanced="false">
        <LISTITEM value="input_data/STD_MIX_1_25_neg.mzML"/>
      </ITEMLIST>
      <ITEM name="x_pos" value="-1680" type="double" description="" required="false" advanced="false" />
      <ITEM name="y_pos" value="-620" type="double" description="" required="false" advanced="false" />
    </NODE>
    <NODE name="1" description="">
    ...
    </NODE>
    ...
  </NODE>
</PARAMETERS>

My aim is to make an XPath query that returns the ITEMLIST node having an attribute name="file_names" and a sibling ITEM node that has attributes name="toppas_type", value="input file list". 我的目标是进行一个XPath查询,该查询返回具有属性name =“ file_names”的ITEMLIST节点和具有属性name =“ toppas_type”,value =“ input file list”的同级ITEM节点。 I tried the following one: 我尝试了以下方法:

'./NODE/NODE[ITEM[@name="toppas_type"][@value="input file list"]]/ITEMLIST[@name="file_names"]'

with xml.etree.ElementTree in Python 3.4, but I get an error 'invalid predicate'. 在Python 3.4中使用xml.etree.ElementTree,但出现错误“无效谓词”。 I think my query contains a silly mistake, but I can't find it. 我认为我的查询包含一个愚蠢的错误,但找不到。

xml.etree.ElementTree has a limited XPath support : xml.etree.ElementTree具有有限的XPath支持

This module provides limited support for XPath expressions for locating elements in a tree. 该模块对XPath表达式提供了有限的支持,用于在树中定位元素。 The goal is to support a small subset of the abbreviated syntax; 目标是支持缩写语法的一小部分。 a full XPath engine is outside the scope of the module. 完整的XPath引擎不在模块范围内。

If you are okay with switching to lxml , it can be solved by using following-sibling axis: 如果可以切换到lxml可以,可以使用following-sibling轴解决:

//ITEM[@name = 'toppas_type' and @value = 'input file list']/following-sibling::ITEMLIST[@name = 'file_names']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM