我有一些看起来像的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>
> foo
</foo>
但我想要的输出是
<?xml version="1.0"?>
<foo>
> foo
</foo>
即保持“>”不是将其转换为实体 . 我怎么能做到这一点?
3 回答
@Oded,@ khachik,
试试checking his desired output for well-formedness . 它确实是格式良好的XML . ("Valid"在这里甚至不是问题,因为没有架构 . )
一种常见的误解是">"在格式良好的XML中是不合法的 . 在大多数情况下,"<"不合法,但">"在任何地方都是合法的,只有一个例外 . 规范的relevant paragraph:
使用XSLT 2.0,"right"方法可以使用
<xsl:character-map>
. 使用XSLT 1.0,我认为在输出中强制使用">"的唯一方法是使用 disable-output-escaping ,正如@khachik建议的那样 . 但请注意,XSLT处理器是not required to honor DOE or character maps,有些可以't (e.g. if they'在管道中并且没有连接到序列化 . 但是你现在可能知道你的是否可以,以及它是否需要在管道末端处理序列化问题 .However, 值得一提, why 你想">"序列化为">"吗?如规范中所见,&gt;就XML而言,表达完全相同的信息是完全可以接受的方式 . 没有下游XML消费者应该知道差异或关注 . 你想要它出于美学原因吗?
Update: OP想要的是因为输出不仅需要格式良好的XML,还需要格式良好的Literate Haskell .
Adding to the very good explanation of @LarsH :
If your XSLT processor allows DOE, then you can use :
and when this transformation is applied on the provided XML document :
the wanted output is produced :
<xsl:value-of select='.' disable-output-escaping="yes"/>
但它不是格式良好的XML .Update 随着
>
,它将会很好地形成 . (<
不会 . )