XProc 和 CDATA

XProc and CDATA

我有一个在节点内创建一些 CDATA 的 XSLT。

XML:

<test><inner>stuff</inner></test>

XSLT:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="test">
        <wrapper>
                <xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text>
                <xsl:copy-of select="*"/>
                <xsl:text disable-output-escaping="yes">]]&gt;</xsl:text>
        </wrapper>
    </xsl:template>
</xsl:stylesheet>

此转换,通过 Saxon 执行,returns:

<wrapper><![CDATA[<inner>stuff</inner>]]></wrapper>

我知道我将 XML 包装在 CDATA 中,这有点荒谬。但这是我正在使用的 API 所期望的,所以我别无选择,只能遵循这种模式。

现在我正尝试将此转换作为更大的 XProc 管道的一部分包含在内:

<p:pipeline xmlns:p="http://www.w3.org/ns/xproc" version="1.0" >
<p:xslt>
    <p:input port="stylesheet">
        <p:document href="test.xsl" />
    </p:input>
</p:xslt>

其中returns(使用最新版本的Calabash):

<wrapper>&lt;![CDATA[<inner>stuff</inner>]]&gt;</wrapper>

XProc 似乎不支持 disable-output-escaping 属性。

我继续尝试了一些 XProc 函数,包括 p:unescape-markup 和 p:string-replace 的各种组合,但我找不到不会对其余部分产生不利影响的解决方案我的输出。

知道我下一步可以尝试什么吗?

XSLT 处理器是 not required to support d-o-e:

An XSLT processor will only be able to disable output escaping if it controls how the result tree is output. This may not always be the case. For example, the result tree may be used as the source tree for another XSLT transformation instead of being output.

在流水线中尤其如此:XSLT 可能无法控制输出树的序列化,而只是将其作为 DOM 或 SAX 事件传递到流水线中的下一步。但即使可以,

An XSLT processor is not required to support disabling output escaping. If an xsl:value-of or xsl:text specifies that output escaping should be disabled and the XSLT processor does not support this, the XSLT processor may signal an error; if it does not signal an error, it must recover by not disabling output escaping.

所以你真的不能依赖 d-o-e,尤其是在管道中。

But this is what is expected by an API that I am working with, so I have no choice but to follow this pattern.

我可以同情这种情况,过去使用过有缺陷的工具,因为它们是最好的。但是,CDATA 部分的存在(和边界)在 XML 信息集中 explicitly not。因此,就其 XML 输入要求而言,依赖于 CDATA 部分的 API 是错误的。如果它确实依赖于 CDATA 部分,那么提交一份关于它的错误报告是个好主意。

另一方面,也许您正在使用的 API 实际上不需要 CDATA 部分;也许它只需要你喂它 XML 以某种方式逃脱?如果是这样,还有其他方法可以实现这一点,而不需要 XML 信息集之外的特定序列化。如果您可以向我们展示有关 API 的文档,我们可以帮助确定它实际需要什么。