[英]Add CDATA to an xml file
I would like to add some CDATA
tags around some xml tags 我想在一些xml标签周围添加一些CDATA
标签
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_fr
tags, 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 最好的问候,纪尧姆
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>
A cleaner approach would be to make use of cdata-section-elements 更简洁的方法是使用cdata-section-elements
Delcare teaserText_fr in cdata-section-elements as below Delcare teaserText_fr在cdata-section-elements中如下所示
<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. 然后格式化XSLT,如下所示。 (Do note that you must include CDATA as a wrapper around the element) (请注意,必须包含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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.