in reply to Re: Finding node with attribute XML::LibXML
in thread Finding node with attribute XML::LibXML

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?

Replies are listed 'Best First'.
Re^3: Finding node with attribute XML::LibXML
by ikegami (Patriarch) on Sep 30, 2014 at 19:49 UTC
    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
        Thanks, fixed