Note that there are some explanatory texts on larger screens.

plurals
  1. POPython, lxml - get a sibling tag's (grand)child's text
    primarykey
    data
    text
    <p>I have an XML to parse which is proving really tricky for me. </p> <pre><code>&lt;bundles&gt; &lt;bundle&gt; &lt;bitstreams&gt; &lt;bitstream&gt; &lt;id&gt;1234&lt;/id&gt; &lt;/bitstream&gt; &lt;/bitstream&gt; &lt;name&gt;FOO&lt;/name&gt; &lt;/bundle&gt; &lt;bundle&gt; ... &lt;/bundle&gt; &lt;/bundles&gt; </code></pre> <p>I would like to iterate through this XML and locate all the <strong>id</strong> values inside of the <strong>bitstreams</strong> for a <strong>bundle</strong> where the <strong>name</strong> element's value is 'FOO'. I'm not interested in any bundles not named 'FOO', and there may be any number of bundles and any number of bitstreams in the bundles.</p> <p>I have been using <code>tree.findall('./bundle/name')</code> to find the FOO bundle but this just returns a list that I can't step through for the <strong>id</strong> values:</p> <pre><code>for node in tree.findall('./bundle/name'): if node.text == 'FOO': id_values = tree.findall('./bundle/bitstreams/bitstream/id') for value in id_values: print value.text </code></pre> <p>This prints out <em>all</em> the id values, not those of the bundle 'FOO'. </p> <p>How can I iterate through this tree, locate the <strong>bundle</strong> with the <strong>name</strong> FOO, take this <strong>bundle</strong> node and collect the <strong>id</strong> values nested in it? Is the XPath argument incorrect here?</p> <p>I'm working in Python, with <code>lxml</code> bindings - but any XML parser I believe would be alright; these aren't large XML trees.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload