how to use xsl to escape code blocks

A

andrew_nuss

Hi,

I have a <code> tag for my xml file and plan to have code snippets
that contain lots of < > and & metacharacters. That's not tough to
put in xml because I'll just wrap in a CDATA section.

However, I need to use xsl to spit out HTML <pre> tag with the code
block and obviously to
use HTML escapes for < > and &. What's the easiest way to get XSL to
escape the necessary characters
found in the PCDATA of my <code> tag?

Thanks,
Andy
 
J

Joe Kesselman

However said:
block and obviously to
use HTML escapes for < > and &. What's the easiest way to get XSL to
escape the necessary characters
found in the PCDATA of my <code> tag?

Have you tried it yet? An XSLT processor in HTML output mode should
escape those characters appropriately...

As far as outputting the <pre> -- that's just like any other element;
just set up the stylesheet so when it sees <code> it knows it needs to
translate that into <pre>.
 
A

andrew_nuss

Have you tried it yet? An XSLT processor in HTML output mode should
escape those characters appropriately...

As far as outputting the <pre> -- that's just like any other element;
just set up the stylesheet so when it sees <code> it knows it needs to
translate that into <pre>.

Thanks for the feedback. I omitted part of the problem requirement:
I also have an <asis> XML file tag, which contains a large block of
HTML that I need to NOT escape, that is, drop in as it is.

So could someone clarify how to use the modes of (1) having a CDATA
wrapped tag content that gets escaped when I spit out the <pre> tag
wrapper, (2) how do an <asis> tag which contains CDATA with lots of
tags that I don't want escaped. Maybe some sample XSL code.

Thanks, Andy
 
J

Joe Kesselman

(2) how do an <asis> tag which contains CDATA with lots of
tags that I don't want escaped.

Emphatically not a recommended solution. Instead, have the <asis> tag
instead contain real XHTML and just render that out as HTML. Far simpler.

If you absolutely must perform this particular obscenity -- ie, because
your customer refuses to accept recommendations to address the problem
properly -- you can try using the Disable Output Escaping mechanism to
tell the XSLT processor that content of a particular element should be
rendered unchanged. This will not work in all processing environments,
but it's the closest available to what you're looking for. See

http://www.w3.org/TR/1999/REC-xslt-19991116#disable-output-escaping

The cdata-section-elements attribute of xsl:eek:utput may (or may not) also
be worth looking at.

.... But I say again: This is a foot-seeking gun. If you want to keep
your toes intact, learn not to use it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top