将 CDATA 添加到 xml 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15534255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add CDATA to an xml file
提问by glmrenard
I would like to add some CDATAtags around some xml tags
我想CDATA在一些 xml 标签周围添加一些标签
XML source is (it's only a small part of my file)
XML 源是(它只是我文件的一小部分)
<teaserText_fr>
<div xmlns:xlink="http://www.w3.org/1999/xlink xmlns="http://www.coremedia.com/2003/richtext-1.0"><p>2012 ist für viele L?nder ein wichtiges Wahljahr. Die Reihe fühlt der weltweiten Demokratie auf den Zahn. </p>
</div>
</teaserText_fr>
What I would like is
我想要的是
<teaserText_fr>
<![CDATA[
<div xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.coremedia.com/2003/richtext-1.0"><p>2012 ist für viele L?nder ein wichtiges Wahljahr. Die Reihe fühlt der weltweiten Demokratie auf den Zahn. </p>
</div>
]]>
</teaserText_fr>
My xslt is
我的 xslt 是
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output
method="html"
encoding="UTF-8"
omit-xml-declaration="yes"
doctype-public="-//W3C//DTD HTML 4.01//EN"
doctype-system="http://www.w3.org/TR/html4/strict.dtd"
indent="yes" />
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="teaserText_fr">
<xsl:text disable-output-escaping="yes"><![CDATA[</xsl:text>
<xsl:copy-of select="*"/>
<xsl:text disable-output-escaping="yes">]]></xsl:text>
</xsl:template>
</xsl:stylesheet>
What I get is
我得到的是
</teaserText_de><![CDATA[<div xmlns="http://www.coremedia.com/2003/richtext-1.0" xmls:xlink="http://www.w3.org/1999/xlink"><p>? partir du 10 janvier, ARTE diffuse "I love democracy", une s??rie documentaire qui, en cette grand ann??e ??lectorale, prend le pouls d??mocratique de la plan?¨te.</p></div>]]><addTeaserText_de>
I lost my teaserText_frtags, I don't understand why
我丢失了我的teaserText_fr标签,我不明白为什么
If possible, I would like to do so for some extra tags (with regexp like [add|]TeaserText_[fr|de]but I can't get it work ... "
如果可能的话,我想为一些额外的标签这样做(使用正则表达式,[add|]TeaserText_[fr|de]但我无法让它工作......“
I did some tests all day long but I wasn't succesfull.
我整天做了一些测试,但我没有成功。
Best regards, Guillaume
最好的问候,纪尧姆
回答by JLRishe
You either need to do this:
你要么需要这样做:
<xsl:template match="teaserText_fr">
<xsl:copy>
<xsl:text disable-output-escaping="yes"><![CDATA[</xsl:text>
<xsl:copy-of select="*"/>
<xsl:text disable-output-escaping="yes">]]></xsl:text>
</xsl:copy>
</xsl:template>
Or this:
或这个:
<xsl:template match="teaserText_fr">
<teaserText_fr>
<xsl:text disable-output-escaping="yes"><![CDATA[</xsl:text>
<xsl:copy-of select="*"/>
<xsl:text disable-output-escaping="yes">]]></xsl:text>
</teaserText_fr>
</xsl:template>
(I recommend the first approach)
(我推荐第一种方法)
and you should be all set.
你应该准备好了。
To give the same treatment to any element whose name starts with "teaserText_":
对名称以“teaserText_”开头的任何元素给予相同的处理:
<xsl:template match="*[starts-with(local-name(), 'teaserText_')]">
<xsl:copy>
<xsl:text disable-output-escaping="yes"><![CDATA[</xsl:text>
<xsl:copy-of select="*"/>
<xsl:text disable-output-escaping="yes">]]></xsl:text>
</xsl:copy>
</xsl:template>
回答by Shoaib Khan
A cleaner approach would be to make use of cdata-section-elements
更简洁的方法是使用cdata-section-elements
Delcare teaserText_frin cdata-section-elements as below
cdata-section-elements 中的Delcare teaserText_fr如下
<xsl:output method="xml" indent="yes" version="1.0" encoding="UTF-16"
standalone="yes" cdata-section-elements="teaserText_fr" />
Then format the XSLT as below. (Do note that you must include CDATA as a wrapper around the element)
然后按如下方式格式化 XSLT。(请注意,您必须包含 CDATA 作为元素的包装器)
<xsl:template match="/">
<teaserText_fr>
<![CDATA[
<div xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.coremedia.com/2003/richtext-1.0"><p>2012 ist für viele L?nder ein wichtiges Wahljahr. Die Reihe fühlt der weltweiten Demokratie auf den Zahn. </p>
</div>
]]>
</teaserText_fr>
</xsl:template>

