You may have more of a dilemma than you realize here: You're saying (I think) that you have existing XML that's non-conforming because it hasn't already used entities for the 3 required characters in the data: <, >, and &. < and > are difficult because you (obviously) want to leave them in the tags. That would mean to get it right, you'd have to parse the XML. However most parsers will, I think, get confused by the unescaped < and > characters. i.e., most parsers expect conforming XML. At any rate, if you can't simply regenerate the XML and do the entity conversion, I think you have to start by trying to parse it. Something like XML::Parser would be where I'd start, but I just tried feeding it some XML without escaped < and > characters and it flagged those as errors. Maybe there are settings that let you get around that.

For entity conversion (to or from) I like HTML::Entities. XML::Writer is nice in other respects, but it's entity conversion could be better. It only does the three above, it always does them named, and it's not smart enough to realize when there are & characters introducing entities that are already in the data.


In reply to Re: Re: Re: Escaping XML Reserved characters by steves
in thread Escaping XML Reserved characters by skinnymofo

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.