简体   繁体   中英

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 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". 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'. I think my query contains a silly mistake, but I can't find it.

xml.etree.ElementTree has a limited XPath support :

This module provides limited support for XPath expressions for locating elements in a tree. The goal is to support a small subset of the abbreviated syntax; a full XPath engine is outside the scope of the module.

If you are okay with switching to lxml , it can be solved by using following-sibling axis:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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