使用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"><![CDATA[</text>
<value-of select="normalize-space(text())" disable-output-escaping="yes"/>
<text disable-output-escaping="yes">]]></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"/>