Note that there are some explanatory texts on larger screens.

plurals
  1. POXML::LibXML's notion of a text node's parent
    primarykey
    data
    text
    <p>Something seems odd here.</p> <p>In the example below, I'm accessing text nodes via an XPath query ( <code>//book/isbn/text()</code> ). The <code>text()</code> is necessary to coerce <code>XML::LibXML</code> into allowing me to use the <code>XML::LibXML::Text</code> methods.</p> <p>To get to the parent node though, I have to invoke the <code>parentNode</code> method twice to get the true parent node (<code>&lt;book&gt;</code> in this case):</p> <pre><code>use strict; use warnings; use XML::LibXML; my $xml = XML::LibXML-&gt;new-&gt;parse_string( &lt;&lt; 'MAIN' ); &lt;library&gt; &lt;book&gt; &lt;title&gt;Perl Best Practices&lt;/title&gt; &lt;author&gt;Damian Conway&lt;/author&gt; &lt;isbn&gt;0596001738&lt;/isbn&gt; &lt;pages&gt;542&lt;/pages&gt; &lt;image src="http://www.oreilly.com/catalog/covers/perlbp.s.gif" width="145" height="190" /&gt; &lt;/book&gt; &lt;book&gt; &lt;title&gt;Perl Cookbook, Second Edition&lt;/title&gt; &lt;author&gt;Tom Christiansen&lt;/author&gt; &lt;author&gt;Nathan Torkington&lt;/author&gt; &lt;isbn&gt;0596003137&lt;/isbn&gt; &lt;pages&gt;964&lt;/pages&gt; &lt;image src="http://www.oreilly.com/catalog/covers/perlckbk2.s.gif" width="145" height="190" /&gt; &lt;/book&gt; &lt;/library&gt; MAIN foreach my $isbn ( $xml-&gt;findnodes( '//book/isbn/text()' ) ) { # Do something with $isbn-&gt;setData() my $book = $isbn-&gt;parentNode-&gt;parentNode; # My daddy's daddy is my daddy? print $book-&gt;toString; } </code></pre> <h3>Output</h3> <pre><code>&lt;book&gt; &lt;title&gt;Perl Best Practices&lt;/title&gt; &lt;author&gt;Damian Conway&lt;/author&gt; &lt;isbn&gt;0596001738&lt;/isbn&gt; &lt;pages&gt;542&lt;/pages&gt; &lt;image src="http://www.oreilly.com/catalog/covers/perlbp.s.gif" width="145" height="190"/&gt; &lt;/book&gt;&lt;book&gt; &lt;title&gt;Perl Cookbook, Second Edition&lt;/title&gt; &lt;author&gt;Tom Christiansen&lt;/author&gt; &lt;author&gt;Nathan Torkington&lt;/author&gt; &lt;isbn&gt;0596003137&lt;/isbn&gt; &lt;pages&gt;964&lt;/pages&gt; &lt;image src="http://www.oreilly.com/catalog/covers/perlckbk2.s.gif" width="145" height="190"/&gt; &lt;/book&gt; </code></pre> <hr> <p>So:</p> <ul> <li>is my understanding of XML nodes incorrect in assuming that <code>//isbn</code> and <code>//isbn/text()</code> are the same node, or</li> <li>is this a bug in <a href="http://search.cpan.org/perldoc?XML%3a%3aLibXML" rel="nofollow"><code>XML::LibXML</code></a>'s <code>parentNode</code> method?</li> </ul>
    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.
 

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