Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
What I'm doing is getting a nodeset using find(), for example:

my $xp = XML::XPath->new(filename => $file); $plantdata->{nodeset} = $xp->find('//loop_device[@loop_number="1"]');
Which returns an XML::XPath::NodeSet.

Calling $plantdata->{nodeset}->get_nodelist() will return a list of XML::Node (and/or descendant) objects.

What I've done is go back to the beginning like this:

if ($devicetype eq "complex") { $seachstring = sprintf "//loop_device[@devicename=\"%s\"]/subdevice" +, $devicename; $subdevice = $xp->find($searchstring); }
That's unnecessary, using $device (which in your case is an XML::XPath::Node object returned from $nodeset->get_nodelist())as the context is fine.

I'm doing this because $device is a node from a node list not from a nodeset so I can't do this:

$subdevice = $device->find('/subdevice');
Yes, you can :-). The synopsis in the XML::XPath::NodeSet documentation shows that usage pattern.

I don't know the structure of your XML (you've not provided a sample), but that is exactly how this sort of this is usually done.

If I go into the debugger and look at $plantdata->{nodeset} and $device both seem to contain a hell of a lot more than just the node I want. Is there a way to turn a $device node back into a nodeset so that I can dig deeper into it rather than going back to $xp and starting again?
Now, why would you use a debugger to look in the XML::XPath::Node object? ;)

Actually, every node object returned from the initial XPath context has a references back to the main XPath context's XML DOM. That's likely what you're seeing in the debugger.

XML::XPath::Node objects must have the full context, otherwise you couldn't perform XPath queries like

  $node->find('/foo/../../../bar[2]');

because the context wouldn't be there to traverse!

The moral of the story is to use DOM methods to traverse the XML DOM, not a debugger.

-David


In reply to Re: nodelists and nodesets by erroneousBollock
in thread nodelists and nodesets by nonnonymousnonk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others musing on the Monastery: (5)
    As of 2014-09-17 23:03 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (100 votes), past polls