XSL:当 method=“text” 时,包含一些 ASCII 控制字符

发布于 2024-08-21 16:48:36 字数 341 浏览 3 评论 0原文

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

我知道 XML 文档中不允许使用该字符,但我要输出文本,那么为什么我不允许使用它呢?

我还了解到,无论是在 CDATA 部分中还是作为  ,都不可能按字面意思将此字符放入模板中。但为什么即时生成也不起作用呢?例如,我尝试定义一个返回此字符的函数并将其用作 但会产生 Invalid字符异常。

有办法吗?

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.

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?

I also understand it will not be possible to put this character literally into the template, neither within a CDATA section nor as . 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.

Is there a way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你怎么这么可爱啊 2024-08-28 16:48:37

过去,我曾使用此技术将换行符输入到 XHTML 生成的文本区域中。如果我没有输入至少一个字符,文本区域将自动关闭(导致浏览器问题)。请注意,该字符被包裹在 中。另外,原始来源在一行,但为了可读性我进行了格式化。

<textarea name="qry" rows="4" cols="50" id="query">
 <xsl:value-of select="$qry" /><xsl:text>
</xsl:text>
</textarea>

In the past, I have used this technique to enter a linefeed into an XHTML generated textarea. If I didn't put at least one character, the textarea would self close (causing browser issues). Notice the character is wrapped in <xsl:text>. Also, the original source was on one line, but I formatted for readability.

<textarea name="qry" rows="4" cols="50" id="query">
 <xsl:value-of select="$qry" /><xsl:text>
</xsl:text>
</textarea>

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

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

<?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></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();
}

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.

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></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();
}
不乱于心 2024-08-28 16:48:36

由于 XSLT 文件是 XML 文件,因此您不能包含该字符引用。我认为您无法在纯 XSLT 解决方案中做到这一点。

ASCII 字符 HEX 10/DEC 16 是数据链接转义 (DLE) 控制字符

XML 规范 只允许三个空格(制表符、回车符、换行符) feed)控制字符

合法字符为制表符、回车符
返回、换行和合法的
Unicode 和 ISO/IEC 字符
10646。

0x20 以下的所有其他内容都是不允许的。

字符范围2 Char ::=
#x9 | #xA | #xD | [#x20-#xD7FF] |
[#xE000-#xFFFD] |
[#x10000-#x10FFFF] /* 任何 Unicode
角色,不包括代理人
块、FFFE 和 FFFF。 */

一种选择是在输出中放置该字符的占位符标记值,然后使用外部进程来查找/用该字符替换您的标记。

Since the XSLT file is an XML file, you cannot include that character reference. I don't think you can do this in a pure XSLT solution.

The ASCII character HEX 10/DEC 16 is the Data Link Escape (DLE) control character.

The XML Spec only allows the three whitespace(tab, carriage return, line feed) control characters.

Legal characters are tab, carriage
return, line feed, and the legal
characters of Unicode and ISO/IEC
10646.

Everything else under 0x20 is not allowed.

Character Range 2 Char ::=
#x9 | #xA | #xD | [#x20-#xD7FF] |
[#xE000-#xFFFD] |
[#x10000-#x10FFFF] /* any Unicode
character, excluding the surrogate
blocks, FFFE, and FFFF. */

One option is to put a placeholder token value for that character in your output, and then use an external process to find/replace your token with the character.

℡Ms空城旧梦 2024-08-28 16:48:36

如果您可以使用 XML 1.1(它允许在 XML 文档中插入此类字符作为字符引用),那么以下内容应该可以工作,至少它对我来说适用于 Sun Java 6 和 Saxon 9.2:

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

  <xsl:output method="text"/>

  <xsl:template name="main">
    <xsl:text></xsl:text>
  </xsl:template>

</xsl:stylesheet>

If you can use XML 1.1 (which allows inserting such characters in an XML document as a character reference) then the following should work, at least it works for me with Sun Java 6 and Saxon 9.2:

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

  <xsl:output method="text"/>

  <xsl:template name="main">
    <xsl:text></xsl:text>
  </xsl:template>

</xsl:stylesheet>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文