I hate to look like an XML ayatollah but I think you are going down a slippery path. XML is XML, and what you want is not XML. XML gives you native ways to encode your "literal" chunks so the parser is happy with them. You should use them. If you want a different format then you should use a pre-processor, to turn your quasi-XML into real XML. As the XML parser will never see the original file you can just have a special marker for the beginning and end of literal code, you don't need to use attributes on existing tags. You can basically use anything, I would use something illegal in XML and unlikely to happen in your literal text, &&& for example, or a tag if you really want to:

You pre-processor would then be as simple as this:

#!/usr/bin/perl -w use strict; my $literal_tag= "literal"; { local undef $/; for (<DATA>) { # tag version, the && version would be even simpler # s{&&&(.*?)&&& }{xml_escape($1)}ges; s{<\s*$literal_tag\s*>(.*?)<\s*/\s*$literal_tag>} {xml_escape($1)}geso; print; } } sub xml_escape { my $literal= shift; $literal=~ s/&/&amp;/g; $literal=~ s/</&lt;/g; return $literal; } __DATA__ <doc> <p>A regular para</p> <code><literal>there you put the code you want, including & and <> and all</literal>

another regular para

<literal>more <> code & stuff</literal> </doc> </code>

Frankly using CDATA sections is simpler and let your original documents be well-formed XML, but that's your call.


In reply to Re: CDATA-like "literal" tags in XML-like data by mirod
in thread CDATA-like "literal" tags in XML-like data by John M. Dlugosz

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.