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
In reply to XML::XPath and preserving CDATA fields by mfriedman
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |