从XLST生成低阶不可打印字符

从XLST生成低阶不可打印字符

本文介绍了从XLST生成低阶不可打印字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用XSLT文本输出来生成文件(采用我无法控制的文件格式),尽管它主要是文本,但它包含低阶不可打印字符作为标志,包括在XLST文件中无效的字符(根据XSLT规范).

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).

我希望像下面这样工作,但是它不是有效的XSLT文件,因为它包含XSLT文件中不允许的字符:

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>

我收到以下错误:

[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'

我也尝试过使用一个实际的字符1,带有或不带有CDATA部分,xsl:text元素,xslt-2字符映射,几种不同的编码,但是我不知道如何获取ascii二进制代码= 1的字符.

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.

有什么方法可以从XSLT生成单个低阶不可打印字符输出吗?

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

环境:Java 6,内置于XSL Transformer中.

Environment: Java 6, built in XSL Transformer.

推荐答案

您可以从XSLT调用Java类的静态方法.以下面的示例为例,将0x01写入输出流:

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>

这篇关于从XLST生成低阶不可打印字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 15:20