简体   繁体   English

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

[英]Generate low-order non-printable characters from XLST

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文本输出来生成文件(采用我无法控制的文件格式),虽然它主要是文本,但它包含低阶不可打印字符作为标志,包括那些在XLST文件中无效(根据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: 我希望像下面这样工作,但是它不是有效的XSLT文件,因为它包含XSLT文件中不允许的字符:

<?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. 我也尝试过使用实际字符1,带有或不带有CDATA部分,xsl:text元素,xslt-2字符映射,几种不同的编码,但是我不知道如何使用二进制来获取ascii字符。代码= 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? 有什么方法可以从XSLT生成单个低阶不可打印字符输出吗?

Environment: Java 6, built in XSL Transformer. 环境:Java 6,内置在XSL Transformer中。

You can call static methods of Java classes from XSLT. 您可以从XSLT调用Java类的静态方法。 Use the following hack for example to write 0x01 to your output stream: 以下面的例子为例,将0x01写入输出流:

<?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>

Another option that I've come up with, is to use an xsl:param which the calling environment sets to character 0x01. 我想到的另一种选择是使用xsl:param,调用环境将其设置为字符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. 这意味着样式表不是始终在Java环境中工作并且需要在其他地方进行更改,而是在所有环境中都需要环境支持,但是在所有环境中都可以保持不变。

I'm not yet sure which side of that trade-off is preferable for what I'm working on. 我尚不确定这种权衡的哪一方面适合于我正在研究的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM