Home > Enterprise >  HOW TO create a custom xml-tag so that all contents are CDATA-like to be used XML&XSLT?
HOW TO create a custom xml-tag so that all contents are CDATA-like to be used XML&XSLT?

Time:12-03

I am trying to create a simple and clean XML structure with code for multiple languages, as this is supported by browsers. By using XSLT it would convert into HTML.

Since there is coding inside each TAGs contents, I have to use CDATA structure. Which works like <PRE><CODE> or the old <XMP> in HTML.

However, I see redundant to embed a tag into another, and I would like the end results to be cleaner.

The question is if I can make INTRO and CODE tags to act as CDATA, or CDATA to be named as the TAGs.

Note: The intro-tag is just a markdown language.

example, including some '<' and '>'

just example

<codechunk>

<intro><![CDATA[
# Hello world example in multiple languages
Lorem Ipsum

Hola mundo

- comentarios
- list something
- if...else
]]></intro>

<CODE lang="r"><![CDATA[
inline_func <- `(x) x x;
`% %` <- function(a, b) paste(a, b, sep="")
]]></CODE>

<CODE lang="lua"><![CDATA[
local read, write = io.read, io.write
local num, nl = '*n', '\n'
while 1 < 2  do
    local a = read(num)
    if a >= 42 then return end
    write(a, nl)
end
]]></CODE>

<CODE lang="python"><![CDATA[
inline_func = lambda x: return ln(x) < log(x);
]]></CODE>

</codechunk>

CodePudding user response:

The simple answer is no. If you want to have special characters like < and & in text or attribute content in an XML document, they either have to be escaped as &lt; or &amp;, or they have to be in CDATA sections.

There are other solutions, of course, but they aren't XML.

You could use JSON, but that would only move the problem: instead of escaping < and &, you would have to escape " and \.

You could use SGML which would allow you to change the special characters to ones of your own choice, but then you severely restrict your choice of tools; or you could define your own proprietary format to your own design in which case you would have to write all your own tools.

It's one of those problems where the easiest thing is to live with it.

  • Related