XSL:当method =“text”时包含一些ASCII控件字符 [英] XSL: Include some ASCII control chars when method="text"

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

问题描述

我有一个XSL模板可以输出文本而不是XML。
在本文中,我需要将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; 。但为什么飞行一代也不行?例如,我尝试定义一个返回此char并将其用作< xsl:value-of select =z:get_char(16)/> 但是也会产生无效的字符异常。

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文档中使用控制字符,也就是你可以拥有&#x10;如果您在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天全站免登陆