in reply to regex on XML

I gave it another thought and went to read some books on XML.

Valid XML only needs & and < to be turned into &amp; and &lt; respectively. You don't have to encode ' " or > at all.

Replacing all & by &amp; is trivial. This still leaves you with the troublesome <, but I don't think there is any possibility (other than validating your XML against a DTD) to let the script differentiate between <FIELDS> and <do_what>: they both look like a "valid" XML tag.

Oh yes, just remembered: ]]> must be encoded as ]]&gt; if you want to use it as text.

CountZero

"If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Replies are listed 'Best First'.
Re: Re: regex on XML
by diotalevi (Canon) on Feb 17, 2004 at 21:09 UTC
    That's not true. You do have to encode ' and " if you expect to use them inside a quoted string using that character as a delimiter. So <foo bar=" &quot; "/> and <foo bar=' &apos; '/>
      Perhaps, but it has nothing to do with it being valid XML or not: it is just about having balanced quotes.

      CountZero

      "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

        Yes it is - if you don't encode apostrophes inside an apostrophe quoted string your XML is immediately invalid. Same thing for double-quotes.