Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

getAttribute() of XML::XPath::Node

by mohamedrabeek (Initiate)
on Jan 22, 2009 at 17:24 UTC ( #738232=perlquestion: print w/ replies, xml ) Need Help??
mohamedrabeek has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, I am using the getAttribute() method on Node object like given below:
my $nodeset = $xp->find('//group'); foreach my $node ($nodeset->get_nodelist) { my $attrName = $node->getAttribute('name'); print "name is $attrName\n"; }
My question is that I did not find getAttribute() method defined in XML::XPath::Node module. But it is defined in XML::XPath::Node::Element module. How it is possible to invoke the getAttribute on the Node object instead of Element object. I am getting the results for the above code snippet. My question is why getAttribute() of Element.pm is called when I invoke the method on the Node object. In other words what is the relationship between Node and Element.I don't think inheritance kicks in here because Element is the child of Node and not the Node is the child of Element. Thanks in advance for your replies. Mohamed

Comment on getAttribute() of XML::XPath::Node
Download Code
Re: getAttribute() of XML::XPath::Node
by runrig (Abbot) on Jan 22, 2009 at 17:35 UTC
    An element is a node (though a node is not necessarily an element). Your query returns a list of nodes which are also elements. So getAttribute() works. Different methods are valid depending on what type of node it is (element, attribute, etc.). If your query returned a list of attributes, you might call nodeName() and getValue() on them.
Re: getAttribute() of XML::XPath::Node
by holli (Monsignor) on Jan 22, 2009 at 17:38 UTC
    I did not find getAttribute() method defined in XML::XPath::Node module.
    Oh, but it is there.
    sub getAttribute { return; }
    That is a stub, because the node class is not intended to be used directly. It is the motherclass (or superclass or parent class) for all the different element types. You will always get an instance of XML::XPath::Node::Element, XML::XPath::Node::Comment etc. Try print ref($node), "\n"; and see what it says.


    holli

    When you're up to your ass in alligators, it's difficult to remember that your original purpose was to drain the swamp.

      I have found the documentation of XML::XPath to be spotty so you generally have to dig around the code from time to time to verify that the thing you are requesting is what you expect.

      Using the above example, I wish they had used a die statement rather than a return statement because it implies something that is not there. I normally use an empty return as a sign of failure in my code, but you cannot rely upon that assumption in cpan or other peoples code.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://738232]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2014-08-31 02:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (294 votes), past polls