mfriedman has asked for the wisdom of the Perl Monks concerning the following question:
I am currently using XML::XPath to extract and change some nodes in an XML document. I use code similar to the following:
I am then retrieving the modified XML thusly:my $xpath = XML::XPath->new('xml' => $XML); foreach my $node ($xpath->findnodes('/foo/bar/element')->get_nodelist) + { my $data = my_munge_function($node->as_string); my $id = $node->getAttribute('id'); $xpath->setNodeText('/foo/bar/element[@id=' . $id . ']', $data); }
my $newXML = $xpath->findnodes_as_string('/');
NOTE: I'm not sure if all the syntax above is exactly correct - I just wrote it out from memory, since the actual code is not in front of me right now.
This all works fine, except for one thing. When I retrieve the modified XML from the $xpath object, the CDATA fields surrounding certain data have disapeared, and XML::XPath has accidentally escaped some characters that shouldn't be. As an example, the XML
Comes back as:<foo> <bar> <text><![CDATA[La dee da de da.<br>Foo bar baz]]></text> </bar> </foo>
I think I know why the CDATA field disapears; the parser is returning the content of the field to XML::XPath but not the information that it is a CDATA. The problem is that this XML must then go to an XSL transformer (Sablotron, in this case) and it's broken.<foo> <bar> <text>La dee da de da.&lt;br>Foo bar baz</text> </bar> </foo>
I would appreciate dearly any insight into this matter.
Thanks,
-Mike
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: XML::XPath and preserving CDATA fields
by mirod (Canon) on May 30, 2002 at 11:28 UTC | |
by Anonymous Monk on May 30, 2002 at 15:11 UTC | |
by mirod (Canon) on May 30, 2002 at 16:07 UTC | |
by mfriedman (Monk) on May 30, 2002 at 19:36 UTC | |
|
Re: XML::XPath and preserving CDATA fields
by Matts (Deacon) on May 30, 2002 at 15:14 UTC | |
by IOrdy (Friar) on May 31, 2002 at 04:57 UTC | |
|
Re: XML::XPath and preserving CDATA fields
by hackmare (Pilgrim) on May 30, 2002 at 14:16 UTC |