Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: CDATA-like "literal" tags in XML-like data

by mirod (Canon)
on Nov 09, 2001 at 17:46 UTC ( #124357=note: print w/replies, xml ) Need Help??


in reply to CDATA-like "literal" tags in XML-like data

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.

Replies are listed 'Best First'.
Re: Re: CDATA-like "literal" tags in XML-like data
by John M. Dlugosz (Monsignor) on Nov 10, 2001 at 01:14 UTC
    Hmm, I really like the idea of not being context-sensitive. Yes, just because PM has magic <code> tags doesn't mean it's the only solution to the ergonomic problem.

    Having a single sequence that was used to delimit literal text, rather than configurable multiple sequences, would mean that a simple s/// operator would be able to find them.

    In fact, if separate begin and end sequences were used, they could be turned into "<![CDATA[" and "]]>"respecitivly, and not need to grab everything in between. That would be very useful to operate as a simple filter of input.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://124357]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2023-02-06 22:52 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?