Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: XML::XPath and preserving CDATA fields

by Matts (Deacon)
on May 30, 2002 at 15:14 UTC ( [id://170411]=note: print w/replies, xml ) Need Help??


in reply to XML::XPath and preserving CDATA fields

This is a design decision. I wanted to base the module as closely as possible on the XPath specification as possible (as this makes implementation easier - and believe me it was no easy task writing that module). If you look at the section on text nodes in the XPath spec, you'll see how CDATA sections are just treated exactly as non-CDATA section nodes. In order to be able to merge adjacent text nodes, the concept of CDATA nodes had to go.

I know that probably doesn't give you the answer you wanted. If you *really* need CDATA information in the output stream, I suggest you look at something with a more feature rich API like XML::LibXML, SAX, or XML::Parser.

  • Comment on Re: XML::XPath and preserving CDATA fields

Replies are listed 'Best First'.
Re: Re: XML::XPath and preserving CDATA fields
by IOrdy (Friar) on May 31, 2002 at 04:57 UTC
    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>

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://170411]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2024-04-18 20:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found