foo")和 XSLT 样式表 当我运行xsltproc stylesheet.xs-6ren">
gpt4 book ai didi

xslt - 不要在 XSLT 中将 > 转换为 >

转载 作者:行者123 更新时间:2023-12-02 11:42:02 26 4
gpt4 key购买 nike

我有一些看起来像这样的 XML

<?xml version="1.0"?>
<root>
<![CDATA[
> foo
]]>
</root>

(注意 > 登录 "> foo")和 XSLT 样式表

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/root">
<foo><xsl:value-of select='.'/></foo>
</xsl:template>
</xsl:stylesheet>

当我运行xsltproc stylesheet.xsl data.xml时,我得到

<?xml version="1.0"?>
<foo>

&gt; foo

</foo>

但我想要的输出是

<?xml version="1.0"?>
<foo>

> foo

</foo>

即保持“>”原样,而不是将其转换为实体。我怎样才能做到这一点?

最佳答案

@Oded,@khachik,

尝试checking his desired output for well-formedness 。它确实是格式良好的 XML。 (“有效”在这里甚至不是一个问题,因为没有模式。)

一种常见的误解是“>”在格式良好的 XML 中不合法。在大多数情况下,“<”不合法,但“>”在任何地方都是合法的,只有一个罕见的异常(exception)。 relevant paragraph规范:

The ampersand character (&) and the left angle bracket (<) MUST NOT appear in their literal form, except when used as markup delimiters, or within a comment, a processing instruction, or a CDATA section. If they are needed elsewhere, they MUST be escaped using either numeric character references or the strings " & " and " < " respectively. The right angle bracket (>) may be represented using the string " &gt; ", and MUST, for compatibility, be escaped using either " &gt; " or a character reference when it appears in the string " ]]> " in content, when that string is not marking the end of a CDATA section.

使用 XSLT 2.0,执行您想要的操作的“正确”方法是使用 <xsl:character-map> 。对于 XSLT 1.0,我认为在输出中强制使用“">”的唯一方法是使用 disable-output-escaping,正如 @khachik 建议的那样。但请注意,XSLT 处理器是 not required to honor DOE or character maps ,有些则不能(例如,如果它们位于管道中并且未连接到序列化)。但您现在可能知道您的是否可以,如果不能,您将需要在管道末尾处理序列化问题。

但是,值得一问的是,为什么要将“">”序列化为“">”?如规范中所示,>就 XML 而言,这是表达完全相同信息的完全可接受的方式。任何下游 XML 消费者都不应该知道其中的差异或关心。您出于美观原因想要它吗?

更新:OP 希望因为输出不仅需要是格式良好的 XML,还需要是格式良好的 Literate Haskell。

关于xslt - 不要在 XSLT 中将 > 转换为 >,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4248230/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com