Matts++ (like he needs any more xp but it's the thought that counts)
I asked this same question on the mailing list a while ago :)
If you still like the thought of XPath over DOM (as I did/do) but would like to preserve/edit/add... CDATA then I found XML::LibXML a great alternative to XML::XPath. (plus it works well with XML::LibXSLT which I beleive is faster than sablotron)
<edit> added some code </edit>
#!/usr/bin/perl -w
use strict;
use XML::LibXML;
undef $/;
my $XML=<DATA>;
my $parser = XML::LibXML->new();
my $xmlp = $parser->parse_string($XML);
print $xmlp->toString; #before munge
foreach my $node ($xmlp->findnodes('/foo/bar/text')) {
my $data = your_munge_function($node->findvalue('text()'));
$node->findnodes('text()')->get_node(1)->setData($data);
}
print $xmlp->toString; #after munge
# functions
sub your_munge_function
{ return "munged $_[0]"; }
# data
__DATA__
<foo>
<bar>
<text id="text1"><![CDATA[La dee da de da.<br>Foo bar baz]]></text>
<text id="text2">normal text</text>
</bar>
</foo>
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.