Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2023-02-06 22:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (37 votes). Check out past polls.

    Notices?