XSLT处理器可以保留空的CDATA节吗? [英] Can an XSLT processor preserve empty CDATA sections?

查看:100
本文介绍了XSLT处理器可以保留空的CDATA节吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理XML文档(InstallAnywhere .iap_xml安装程序),然后再将其交给另一个工具(InstallAnywhere本身)以更新某些值.但是,看来我正在使用的XSLT转换正在从文档中剥离CDATA节(对InstallAnywhere似乎很重要).

I'm processing an XML document (an InstallAnywhere .iap_xml installer) before handing it off to another tool (InstallAnywhere itself) to update some values. However, it appears that the XSLT transform I am using is stripping CDATA sections (which appear to be significant to InstallAnywhere) from the document.

我正在使用Ant 1.7.0,JDK 1.6.0_16和基于身份的样式表:

I'm using Ant 1.7.0, JDK 1.6.0_16, and a stylesheet based on the identity:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml" encoding="UTF-8" cdata-section-elements="string" />
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

基本上,字符串"节点看起来像:

Basically, "string" nodes that look like:

<string><![CDATA[]]></string>

被处理为:

<string/>

通过阅读XSLT常见问题解答,我发现就XSLT规范而言,发生的事情是合法的.有什么办法可以防止这种情况发生,并说服XSLT处理器发出CDATA部分?

From reading XSLT FAQs, I can see that what is happening is legal as far as the XSLT spec is concerned. Is there any way I can prevent this from happening and convince the XSLT processor to emit the CDATA section?

推荐答案

为此,您需要为空的string元素添加特殊情况并使用disable-output-escaping.我没有要测试的Ant副本,但是以下模板与libxmlxsltproc一起为我工作,该模板表现出与您描述的相同的行为:

To do this, you'll need to add a special case for empty string elements and use disable-output-escaping. I don't have a copy of Ant to test with, but the following template worked for me with libxml's xsltproc, which exhibits the same behavior you describe:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" cdata-section-elements="string"/>

    <xsl:template match="string">
        <xsl:choose>
            <xsl:when test=". = ''">
                <string>
                    <xsl:text disable-output-escaping="yes"><![CDATA[]]></xsl:text>
                </string>
            </xsl:when>

            <xsl:otherwise>
                <xsl:copy-of select="."/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

输入:

<input>
    <string><![CDATA[foo]]></string>
    <string><![CDATA[]]></string>
</input>

输出:

<input>
    <string><![CDATA[foo]]></string>
    <string><![CDATA[]]></string>
</input>

这篇关于XSLT处理器可以保留空的CDATA节吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆