XSL:当 method=“text” 时,包含一些 ASCII 控制字符
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
过去,我曾使用此技术将换行符输入到 XHTML 生成的文本区域中。如果我没有输入至少一个字符,文本区域将自动关闭(导致浏览器问题)。请注意,该字符被包裹在
中。另外,原始来源在一行,但为了可读性我进行了格式化。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.Microsoft .NET 框架不支持 XML 1.1,这是事实,但它有自己的(不可移植的)方式在 XML 1.0 文档中使用控制字符,即如果将 CheckCharacters 设置为 false,则可以将其作为数字字符引用您的 XmlReaderSettings/XmlWriterSettings。
下面是一个示例样式表和一些使用 .NET 3.5 测试的 .NET 代码,它们不会引发非法字符异常
:
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:
由于 XSLT 文件是 XML 文件,因此您不能包含该字符引用。我认为您无法在纯 XSLT 解决方案中做到这一点。
ASCII 字符 HEX 10/DEC 16 是数据链接转义 (DLE) 控制字符。
XML 规范 只允许三个空格(制表符、回车符、换行符) feed)控制字符。
0x20 以下的所有其他内容都是不允许的。
一种选择是在输出中放置该字符的占位符标记值,然后使用外部进程来查找/用该字符替换您的标记。
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.
Everything else under 0x20 is not allowed.
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.
如果您可以使用 XML 1.1(它允许在 XML 文档中插入此类字符作为字符引用),那么以下内容应该可以工作,至少它对我来说适用于 Sun Java 6 和 Saxon 9.2:
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: