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?
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.