in reply to Child contains attribute

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.

Replies are listed 'Best First'.
Re^2: Child contains attribute
by Anonymous Monk on Aug 27, 2014 at 13:45 UTC
    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.