My goal for
<code> tags is that you should be able to
cut and paste arbitrary code directly into the XML file, with no editing
needed. One problem with this, for example, is that the characters
<
and
> are special in an XML file. If you tried to copy this code sample directly
into the XML file, you would have problems:
Code sample with embedded < and > characters
for i in range(10):
if i > 5:
print "I > 5", i
elif i < 4:
print "I < 4", i
An XML parser would think that the embedded
< and
> characters
were the beginning or end of tags and would give you an error since they don't form
complete tags. Since an XSLT processor is based on XML, the same problem arises
there.
The solution to this is to embed your code inside of a
CDATA tag.
A
CDATA section tells the XML parser to treat the enclosed characters
as pure data and not try to parse it. To wrap your code in a
CDATA tag,
you place the special sequence "
<![CDATA[" at the start of
your code, and the sequence "
]]>" at the end of the code.
Although you
could do this by hand, it quickly becomes very tedious.
To ease the pain, I added some PHP code to automatically insert the
CDATA markers for
both the
<code> and
<c> tags.
One drawback to this is that if you want to insert the literal characters
<code>
into your document (as I just did), you need some sort of escaping mechanism so that the
CDATA
wrappers are not inserted. To support this, I have the PHP code ignore the text
"
<!--NULL-->". Now, when I want a literal
<code>
tag to show up in the output stream, I write it like this:
"
<c><<!--NULL-->code></c>". The PHP code
will ignore the inner
<code> tag when inserting
CDATA tags,
and will delete the "
<!--NULL-->" string
from the final output stream.
Although it's a pain to do all that escaping, I doubt I'll have much need
to place literal
<code> tags in an article, outside of articles
about the markup language itself. I see it as a temporary problem.