将 CDATA 添加到 xml 文件 [英] Add CDATA to an xml file
问题描述
我想在一些 xml 标签周围添加一些 CDATA
标签
XML 源代码是(它只是我文件的一小部分)
<div xmlns:xlink="http://www.w3.org/1999/xlink xmlns="http://www.coremedia.com/2003/richtext-1.0"><p>2012 年 1 月Länder ein wichtiges Wahljahr. Die Reihe fühlt der weltweiten Demokratie auf den Zahn.</p>
</teaserText_fr>
我想要的是
<![CDATA[<div xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.coremedia.com/2003/richtext-1.0"><p>2012 年viele Länder ein wichtiges Wahljahr.Die Reihe fühlt der weltweiten Demokratie auf den Zahn.</p>
]]></teaserText_fr>
我的 xslt 是
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:输出方法=html"编码=UTF-8"省略-xml-声明=是"doctype-public="-//W3C//DTD HTML 4.01//EN"doctype-system="http://www.w3.org/TR/html4/strict.dtd"缩进=是"/><xsl:template match="node()|@*"><xsl:copy><xsl:apply-templates select="node()|@*"/></xsl:copy></xsl:模板><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:模板></xsl:stylesheet>
我得到的是
</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, 艺术传播我爱民主", une série documentaire qui, en cette grand année électorale, prend le pouls démocratique de la plané.</p></div>]]><addTeaserText_de>
我丢失了 teaserText_fr
标签,我不明白为什么
如果可能的话,我想为一些额外的标签这样做(使用像 [add|]TeaserText_[fr|de]
这样的正则表达式,但我无法让它工作......
我一整天都做了一些测试,但我没有成功.
最好的问候,纪尧姆
你要么需要这样做:
<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:模板>
或者这个:
<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:模板>
(我推荐第一种方法)
你应该准备好了.
对名称以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:模板>
I would like to add some CDATA
tags around some xml tags
XML source is (it's only a small part of my file)
<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
<?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
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 ... "
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_":
<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>
这篇关于将 CDATA 添加到 xml 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!