in reply to Finding node with attribute XML::LibXML

Volume[@VolumeCategory="L"]
relative to the root of the document (/) means
/Volume[@VolumeCategory="L"]

They fail to find a match since there's no element named Volume at the root of the document.

/root/Volume[@VolumeCategory="L"]

would find the node in question, and so would its relative equivalent

root/Volume[@VolumeCategory="L"]

It's just like directories.

$ cd / $ ls -d ikegami ls: cannot access ikegami: No such file or directory $ ls -d /ikegami ls: cannot access /ikegami: No such file or directory $ ls -d /home/ikegami /home/ikegami $ ls -d home/ikegami home/ikegami

Replies are listed 'Best First'.
Re^2: Finding node with attribute XML::LibXML
by jjap (Monk) on Sep 30, 2014 at 18:06 UTC
    Thanks for the reply.

    I did muff my example as I had been using getElementsByTagName which spared me the need to qualify the full path if may say.

    Should I understand that that methods does not support attribute?
      If you want to search the whole document, you can use
      /descendant::Volume[@VolumeCategory="L"]
      which can be abbreviated to
      //Volume[@VolumeCategory="L"]

      Should I understand that that methods does not support attribute?

      If you're asking if getElementsByTagName can filter the returned nodes to retain only those with a specific attribute value, then the answer is no. The only argument it takes is a tag name. You could do

      my @matching = grep { my $vc = $_->getAttribute('VolumeCategory'); defined($vc) && $vc eq "L" } $doc->getElementsByTagName('Volume');

      but aforementioned

      my @matching = $doc->findnodes('//Volume[@VolumeCategory="L"]');

      is much simpler.

        I think you meant to type…

        /descendant::Volume[@VolumeCategory="L"]

        With just one colon, you get…

        XPath error : Undefined namespace prefix