Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Finding node with attribute XML::LibXML

by ikegami (Patriarch)
on Sep 30, 2014 at 17:51 UTC ( [id://1102452]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1102452]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-25 19:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found