Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hey guys, Here's a simple question, I'm not able to answer. How can I find out, if a Childnode contains specific attributes. My code Looks like this:
<text> <metadata attribute1= ".." Attribute2=".."></metadata> </text> <text> <metadata attribute1= ".." Attribute2=".."></metadata> </text>
I find the text-tag without any Problems. But when I try to catch the metadata to get the attributes of it, then I get Troubles. Here my code:
if($data->nodeName eq 'text'){ if ($data->hasChildNodes()) { my $ch = $data->getChildnodes; } }
This is all I have, because I don't know how to go further. (get always the error message: Can_t locate object or reference "getAttribute" via package XML::LibXML::Nodelist) Hope someone could help me with this beginner-mistake Regards, Sandorado

Replies are listed 'Best First'.
Re: Child contains attribute
by hippo (Archbishop) on Aug 26, 2014 at 12:51 UTC

    You can call the attributes method on a single node and it returns the list of all the attributes on that node which you can then investigate futher.

      @attributelist = $node->attributes();

    as mentioned in the XML::LibXML::Node documentation.

      Hey, thanks for your quick Respons I tried your solution and I need to mention, that I forgot to explain, that the <text>-tag also contains attributes. With your solution, I only get the attributes of the text-tags and not of the metadata-tags. Regards, Sandorado

        Well then, search for "metadata" nodes after you have found "text" node. And, then get the attributes.

Re: Child contains attribute
by AppleFritter (Vicar) on Aug 26, 2014 at 15:32 UTC

    Howdy Sandorado, welcome to the Monastery!

    This is all I have, because I don't know how to go further. (get always the error message: Can_t locate object or reference "getAttribute" via package XML::LibXML::Nodelist)

    That's because XML::LibXML::Nodelist does not have that method. That's natural, because objects of this type represent lists of nodes, not individual nodes; what you'd want to do is call ->foreach() on the Nodelist object, and - for each individual node - call ->attributes() and see if the attributes you're looking for are present.

    That said, you don't have to do it that way. If you're interested in selecting specific nodes of an XML document, use an XPath expression. XML::LibXML has a ->findnodes method for that that you can call on any node (e.g. the document root); alternatively, use a module like XML::XPath.

    The specific XPath expression you'll want depends on what nodes you're looking for, of course, but you might use something like //metadata[@attribute1 and @Attribute2] to select all metadata elements that have both attribute1 and Attribute2 elements (regardless of value).

    I recommend taking a look at the XPath spec for more; section 2.5 has a number of useful examples that are much easier to understand than the formal grammar.

      I've been having the same problem. I originally used xpath expressions to parse values, but find & findvalue seems to be extremely slow.

      Something like this:

      foreach my $query ($xc->findnodes('/xn:query/xn:sequence/xn:transa +ction/xn:step/xn:loginTranHistory')) { my $share = xc->findvalue('./xn:shareSerial',$query); my $cat = $xc->findvalue('./xn:category',$query)||''; my $catop = $xc->findvalue('./xn:category/@option',$qu +ery)||''; # several more findvalues ... }

      Works fine for a 100 records or so, but once I scaled up to a 1000 records or so, it was taking 40-60 seconds to pull the records. Profiling was showing 80-90% of the time was taken up in the find.

      So now I'm converting everything to getChildrenByTagName. That alone made the program run 75% faster, but I'm having trouble getting the attributes.

      I'll look at looping through the Nodelist object and calling attributes(), but I thought I'd share my problems with find to see if anyone has any ideas why it would run so slow.

        XML::Twig can handle big documents better; it doesn't need to load the whole thing into memory at once.
Re: Child contains attribute
by Anonymous Monk on Aug 26, 2014 at 13:23 UTC
Re: Child contains attribute
by Anonymous Monk on Aug 26, 2014 at 15:08 UTC
    Save time and hair. Use an XPath expression to directly query the XML structure for the nodes that contain the specific attribute that you want ... anywhere and everywhere in the entire structure. LibXML can do that. Solve the difficult programming problem by avoiding it entirely.