Quicksilver has asked for the wisdom of the Perl Monks concerning the following question:

I'm using XML::Writer to convert some text into XML to be transformed using XML::LibXSLT. I'm pretty sure that my XSL is working but I think I'm outputting the XML incorrectly but my attempts to solve this tend to crash the script.
#!c:\perl\bin\perl.exe use strict; use warnings; use XML::Writer; use IO::File; my $write; my $file = "C:\\WebRoot\\dickens\\dicktest.txt"; my @outputxml; my $output = new IO::File(">c:\\generate.xml"); my $count; my $writer = new XML::Writer(OUTPUT => $output); open (IN, $file) || die "$file not found\n"; @outputxml = <IN>; close (IN); chomp @outputxml; $writer->xmlDecl(); $writer ->startTag("text"); foreach $write ( @outputxml) { $count++; $writer ->startTag("lineno"); $writer->characters($count); $writer->endTag("lineno"); $writer ->startTag("line"); $writer->characters($write); $writer->endTag("line"); } $writer->endTag("text"); $writer->end(); $output->close();
The XML I'm getting is:
<?xml version="1.0"?> <text><br /> <lineno>1</lineno><line>STAVE I: MARLEY'S GHOST</line><br /> <lineno>2</lineno><line></line><br /> <lineno>3</lineno><line>MARLEY was dead: to begin with. There is no do +ubt</line>
so the XSL is only transforming the first couple of elements and then ignoring the rest of the file so I can see STAVE I: MARLEY'S GHOST but not the rest of the file. If I put the $writer->startTag("text") and its closing tag inside the for loop, the transformation fails whilst parsing.
On another note but perhaps linked, my next step is to compare the contents of the lineno element with line numbers in another file / db and if the two match, then wrap a div tag to enable some JavaScript code to open a window to display some information. Will LibXSLT allow such a comparison?

Replies are listed 'Best First'.
Re: XML output question
by pc88mxer (Vicar) on Apr 11, 2008 at 17:02 UTC
    I think it would be helpful to see how you are using XML::LibXSLT. Maybe the problem is there, and at the very least it would enable us to reproduce the problem. It looks like there might be an issue when you have entities with no content (i.e. <line></line> or <text></text>) but that's just a guess.