nonnonymousnonk has asked for the wisdom of the Perl Monks concerning the following question:
XML::xpath is making my brain ache, perhaps someone can enlighten me.
It's taken a while but I think I'm starting to understand the differences between a nodeset and a node list. I'm sure I've not got it fully because my code so far is not that nice.
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"]');
I then run though the nodes like this
foreach my $device ($plantdata->{nodeset}->get_nodelist()) { $devicename = find('devicename')->string_value; $devicetype = find('devicetype')->string_value; do more stuff }
Most of the time I'm just getting attributes as in the above code and chucking them out to an Excel spreadsheet but for a few values of $devicetype I need to dig deeper into the XML and this is where I'm stuck.
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); }
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');
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?
Thanks
Nonk
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: nodelists and nodesets
by Cody Pendant (Prior) on Nov 22, 2007 at 23:48 UTC | |
by erroneousBollock (Curate) on Nov 23, 2007 at 03:35 UTC | |
|
Re: nodelists and nodesets
by erroneousBollock (Curate) on Nov 23, 2007 at 03:33 UTC | |
by nonnonymousnonk (Novice) on Nov 23, 2007 at 12:16 UTC |