使用XSLT转换XML并保留CDATA(在Ruby中) [英] Transform XML with XSLT and preserve CDATA (in Ruby)

查看:104
本文介绍了使用XSLT转换XML并保留CDATA(在Ruby中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将具有以下内容的文档转换为另一个文档,使CDATA完全与第一个文档中的一样,但是我还没有弄清楚如何使用XSLT保留CDATA.

I am trying to convert a document with content like the following into another document, leaving the CDATA exactly as it was in the first document, but I haven't figured out how to preserve the CDATA with XSLT.

初始XML:

<node>
    <subNode>
        <![CDATA[ HI THERE ]]>
    </subNode>
    <subNode>
        <![CDATA[ SOME TEXT ]]>
    </subNode>
</node>

最终XML:

<newDoc>
    <data>
        <text>
            <![CDATA[ HI THERE ]]>
        </text>
        <text>
            <![CDATA[ SOME TEXT ]]>
        </text>
    </data>
</newDoc>

我已经尝试过类似的方法,但是没有运气,一切都变得混乱了:

I've tried something like this, but no luck, everything gets jumbled:

<xsl:element name="subNode">
    <xsl:value-of select="." disable-output-escaping="yes"/>
</xsl:element>

有什么想法如何保存CDATA吗?

Any ideas how to preserve the CDATA?

谢谢! 兰斯

使用ruby/nokogiri

Using ruby/nokogiri

更新:这是可行的方法.

Update: Here's something that works.

<text disable-output-escaping="yes">&lt;![CDATA[</text>
<value-of select="normalize-space(text())" disable-output-escaping="yes"/>
<text disable-output-escaping="yes">]]&gt;</text>

这会将所有text()节点包装在CDATA中,这可以满足我的需要,并且将在文本内保留html标签.

That will wrap all text() nodes in CDATA, which works for what I need, and it will preserve html tags inside the text.

推荐答案

如果CDATA节点与纯文本节点混合在一起,则无法保留确切的顺序.充其量,您可以通过在xsl:output/@cdata-section-elements:

You cannot preserve the precise sequence of CDATA nodes if they're mixed with plain text nodes. At best, you can force all content of a particular element in the output to be CDATA, by listing that element name in xsl:output/@cdata-section-elements:

<xsl:output cdata-section-elements="text"/>

这篇关于使用XSLT转换XML并保留CDATA(在Ruby中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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