Question

I am trying to canonize a XML fragment to avoid printing of self closing nodes in the resultant XML. ( i want node <test /> to be streamed as <test></test> due to constraints at the consuming system ).

I tried to use toStringC14N() method in the XML::LibXML::Node to achieve the same but I hit the following error on which I don't seems to get any help from google as well.

use XML::LibXML;

my $tagName  = 'testtag';
my $tagValue = '';

my $doc = XML::LibXML->createDocument->new('1.0', 'UTF-8');
my $element = $doc->createElement($tagName);
$element->appendText($tagValue);
print "\nOut=>".$element->toStringC14N();

Output:

Failed to convert doc to string in doc->toStringC14N at /usr/lib/perl5/site_perl/5.8.8/i386-linux-thread-multi/XML/LibXML.pm line 1328.

I could only chase down the error till the following method in the LibXML.c

xmlDocDumpMemory(self, &result, &len);

I am clueless on what I am doing wrong as "toString()" seems to work fine.

I am using perl v5.8.8 & XML::LibXML version 1.88

Was it helpful?

Solution

All the unit tests in XML::LibXML's test script t/24c14n.t get a $doc from $parser->parse_string and call toStringC14N on that. Maybe there's something about createDocument that isn't round-trippable in respect to toStringC14N. You could try asking on the XML::LibXML mailing list (see below).

Meanwhile, this does work:

my $doc = XML::LibXML->createDocument->new('1.0', 'UTF-8');

my $element = $doc->createElement($tagName);
$element->appendText($tagValue);
my $doc2 = XML::LibXML->new->parse_string($element->toString);
print "\nOut=>".$doc2->toStringC14N()."\n";

#prints Out=><testtag></testtag>

For suggestions etc., and other issues related to XML::LibXML you may use the perl XML mailing list (perl-xml@listserv.ActiveState.com), where most XML-related Perl modules are discussed. In case of problems you should check the archives of that list first. Many problems are already discussed there. You can find the list's archives and subscription options at http://aspn.activestate.com/ASPN/Mail/Browse/Threaded/perl-xml.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top