Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to use XSLT text output to generate a file (in a file format that I'm not in control of), and while it is mostly text, it includes low-order non-printable characters as flags, including characters that are not valid within an XLST file (according to the XSLT specification).

I'd like for something like the below to work, but instead it isn't a valid XSLT file since it it contains characters that are not allowed in XSLT files:

<?xml version="1.0" encoding="US-ASCII" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="text" encoding="US-ASCII"/>
  <xsl:template match="/">&#1;</xsl:template>
</xsl:stylesheet>

I get the following error:

[Fatal Error] :4:35: Character reference "&#1" is an invalid XML character.
ERROR:  'Character reference "&#1" is an invalid XML character.'
FATAL ERROR:  'Could not compile stylesheet'

I've tried with an actual character 1 too, with or without a CDATA section, xsl:text elements, xslt-2 character maps, a couple of different encodings, but I can't figure out how to get a ascii character with binary code = 1.

I've had to resort to post-processing my output, which isn't ideal.

Is there any way to generate a single low-order non-printable character output from XSLT?

Environment: Java 6, built in XSL Transformer.

share|improve this question

2 Answers 2

up vote 3 down vote accepted

You can call static methods of Java classes from XSLT. Use the following hack for example to write 0x01 to your output stream:

<?xml version="1.0" encoding="US-ASCII" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:char="java.lang.Character" version="1.0">
    <xsl:output method="text" encoding="US-ASCII" />
    <xsl:template match="/">
    	<xsl:value-of select="char:toString(1)"></xsl:value-of>
    </xsl:template>
</xsl:stylesheet>
share|improve this answer

Another option that I've come up with, is to use an xsl:param which the calling environment sets to character 0x01.

This means that instead of always working within a java environment, and requiring changes anywhere else, that the stylesheet requires environmental support in all environments, but can work unchanged in them all.

I'm not yet sure which side of that trade-off is preferable for what I'm working on.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.
{"cc_re__cc_re__cc_by_sa_3_0": ["                  <li><a href=\"//area51.stackexchange.com\" title=\"proposing new sites in the Stack Exchange network\">Area 51</a></li>\r\n                        <li><a href=\"//careers.stackoverflow.com\">Stack Overflow Careers</a></li>\r\n                    \r\n                </ol>\r\n            </td>\r\n    </tr>\r\n</table>\r\n                </div>\r\n            </div>\r\n\r\n            <div id=\"copyright\">\r\n                site design / logo &#169; 2015 stack exchange inc; user contributions licensed under <a href=\"http://creativecommons.org/licenses/by-sa/3.0/\" rel=\"license\">cc by-sa 3.0</a>                   with <a href=\"http://blog.stackoverflow.com/2009/06/attribution-required/\" rel=\"license\">attribution required</a>\r\n            </div>\r\n            <div id=\"svnrev\">\r\n                rev 2015.1.30.2256\r\n            </div>\r\n            \r\n        </div>\r\n    </div>\r\n    <noscript>\r\n        <div id=\"noscript-warning\">Stack Overflow works best with JavaScript enabled<img src=\"http://pixel.quantserve.com/pixel/p-c1rF4kxgLUzNc.gif\" alt=\"\" class=\"dno\"></div>\r\n    </nos"], "copyright__copyright__copyright": ["ad of always working within a java environment, and requiring changes anywhere else, that the stylesheet requires environmental support in all environments, but can work unchanged in them all.\n\nI'm not yet sure which side of that trade-off is preferable for what I'm working on.\n\nshare|improve this answer\n\nYour Answer\n\n\u00a0\ndiscard\n\nBy posting your answer, you agree to the privacy policy and terms of service.\n\nNot the answer you're looking for? Browse other questions tagged or ask your own question."]}