Dear Monks,
I want to convert one xml file to another. I am using XML::Twig, version 3.23 and perl 5.8.3.
The input file contains HTML-Entities (ie & in the example below) in some text elements.
I do not want them to be changed, but on flushing the parent element, they are converted to the representing character (&), yielding an invalid xml file.
So the question is how to prevent XML::Twig from converting the entities.
Any hints?
Thanks in advance,
Martin

The script:
#!/usr/bin/perl use strict; use File::Basename; use XML::Twig; binmode(STDOUT, ":utf8"); my ($inFile)=@ARGV; unless (open ("INFILE", "<:utf8", "$inFile")){ die "$inFile: No such file or directory."; } my $t= XML::Twig->new( twig_handlers => { "identifier" => \&section, "record" => sub { $_[0]->flush; } }, ); $t->set_pretty_print("nice"); $t->set_keep_encoding; $t->parsefile($inFile); exit 0; sub section { my( $t, $elt)= @_; my $elt_txt = $elt->text; my $new_elt; if ($elt_txt =~ /^http:\/\/arxiv.org/) { $new_elt = XML::Twig::Elt->new( $elt->tag . ".url" => $elt_txt +); } elsif ( $elt_txt =~ /^doi:/ ) { $new_elt = XML::Twig::Elt->new( $elt->tag . '.doi' => $elt_txt +); } $elt->replace_with($new_elt) if $new_elt; }
and an example input file:
<?xml version="1.0" encoding="UTF-8"?> <harvest> <record> <header> <datestamp>2005-09-18</datestamp> <setSpec>cs</setSpec> </header> <metadata> <title>Memory-Based Lexical Acquisition and Processing</title> <creator>Daelemans, Walter</creator> <subject>Computation &amp; Language</subject> <subject>Computer Science - Computation &amp; Language</subject> <description>Comment: 18 pages</description> <date>1994-05-16</date> <type>text</type> <identifier>http://arxiv.org/abs/cmp-lg/9405018</identifier> <identifier>Steffens (ed.) Machine Translation &amp; Lexion. Springer +, 1995</identifier> </metadata> </record> </harvest>

In reply to XML::Twig::flush() and html/xml entities by mandarin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.