XSL: Include some ASCII control chars when method="text"

GSerg picture GSerg · Feb 11, 2010 · Viewed 8.3k times · Source

I've got an XSL template that outputs text as opposed to XML. In this text, I need to include ASCII character 0x10 in certain position.

I understand this character is not allowed in an XML document, but I'm going to output text, so why am I not allowed to use it anyway?

I also understand it will not be possible to put this character literally into the template, neither within a CDATA section nor as &#16;. But why does on-the-fly generation not work either? I tried, for instance, to define a function that returns this char and used it as <xsl:value-of select="z:get_char(16)"/> but that produces an Invalid character exception either.

Is there a way?

Answer

Mads Hansen picture Mads Hansen · Feb 11, 2010

Since the XSLT file is an XML file, you cannot include that character reference. I don't think you can do this in a pure XSLT solution.

The ASCII character HEX 10/DEC 16 is the Data Link Escape (DLE) control character.

The XML Spec only allows the three whitespace(tab, carriage return, line feed) control characters.

Legal characters are tab, carriage return, line feed, and the legal characters of Unicode and ISO/IEC 10646.

Everything else under 0x20 is not allowed.

Character Range 2 Char ::=
#x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF] /* any Unicode character, excluding the surrogate blocks, FFFE, and FFFF. */

One option is to put a placeholder token value for that character in your output, and then use an external process to find/replace your token with the character.