XSL:当 method="text" 时包括一些 ASCII 控制字符; [英] XSL: Include some ASCII control chars when method="text"

查看:24
本文介绍了XSL:当 method="text" 时包括一些 ASCII 控制字符;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个输出文本而不是 XML 的 XSL 模板.在本文中,我需要在特定位置包含 ASCII 字符 0x10.

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.

我知道这个字符在 XML 文档中是不允许的,但是我要输出文本,为什么我还是不允许使用它?

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?

我也明白,无论是在 CDATA 部分还是 &#16; 中,都不可能将这个字符按字面意思放入模板中.但是为什么即时生成也不起作用?例如,我尝试定义一个返回此字符的函数并将其用作 <xsl:value-of select="z:get_char(16)"/> 但这会产生一个 Invalid字符异常.

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.

有办法吗?

推荐答案

Microsoft .NET 框架不支持 XML 1.1,确实如此,但它有自己的(不可移植的)方式来使用 XML 1.0 文档中的控制字符,即你可以拥有 如果您在 XmlReaderSettings/XmlWriterSettings 上将 CheckCharacters 设置为 false,则作为数字字符引用.

The Microsoft .NET framework does not support XML 1.1, that is true, but it has its own (not portable) way to use control characters in XML 1.0 documents, namely you can have  as a numeric character reference if you set CheckCharacters to false on your XmlReaderSettings/XmlWriterSettings.

这里是一个示例样式表和一些使用 .NET 3.5 测试的 .NET 代码,不会引发非法字符异常:

Here is an example stylesheet and some .NET code tested with .NET 3.5 that does not throw an illegal character exception:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="text"/>

  <xsl:template match="/">
    <xsl:text>&#x10;</xsl:text>
  </xsl:template>
</xsl:stylesheet>

 

XmlReaderSettings xrs = new XmlReaderSettings();
xrs.CheckCharacters = false;

XslCompiledTransform proc = new XslCompiledTransform();
using (XmlReader xr = XmlReader.Create(@"sheet.xslt", xrs))
{
    proc.Load(xr);
}

using (XmlReader xr = XmlReader.Create(new StringReader("<foo/>")))
{
    XmlWriterSettings xws = proc.OutputSettings.Clone();
    xws.CheckCharacters = false;

    using (XmlWriter xw = XmlWriter.Create(@"result.txt", xws))
    {
        proc.Transform(xr, null, xw);
        xw.Close();
    }
    xr.Close();
}

这篇关于XSL:当 method="text" 时包括一些 ASCII 控制字符;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆