Thanks for the reply! I've taken the time between posting and your response to dive much deeper into XML than I have before. What I think I want can be accomplished with XPath. I believe your solution does something similar.
Here's what I came up with so far. Please chime in on my approach. First, though, I have to clarify that my original XML file example was not 100% representative of my structure. It looks more like this
<a>
<b id='id1' type="1"/>
<b id='id2' type="1.5"/>
<b id='id3' type="2"/>
<c id='id4'/>
<b id='id5' type="3"/>
</a>
Then, I can use XML::XPath to get what I want using the following
use warnings;
use strict;
use XML::XPath;
use XML::XPath::XMLParser;
my $xmlFile = "try.xml";
# create an object to parse the file and field XPath queries
my $xpath = XML::XPath->new(filename => $xmlFile) || die "Problem: $!\
+n";
# look for the preceding sibling node to the one in question
my $nodeset = $xpath->find('//*[@id=' . "'id4']/preceding-sibling::*[1
+]");
foreach my $node ( $nodeset->get_nodelist ) {
print XML::XPath::XMLParser::as_string( $node ) . "\n";
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.