2013-03-20 71 views
9

我想补充周围的一些XML标记添加CDATA XML文件

XML源是一些CDATA标签(这只是我的文件的一小部分)

<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> 

我想在

<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> 

我的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">&lt;![CDATA[</xsl:text> 
    <xsl:copy-of select="*"/>  
    <xsl:text disable-output-escaping="yes">]]&gt;</xsl:text> 
</xsl:template> 

</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, 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> 

我失去了我的teaserText_fr标签,我不明白为什么

如果可能的话,我想一些额外的标签这么做(像[add|]TeaserText_[fr|de]正则表达式,但我不能得到它的工作...“

我整天做了一些测试,但我没有成功充分。

最好的问候,纪尧姆

回答

13

你要么需要这样做:

<xsl:template match="teaserText_fr"> 
    <xsl:copy> 
    <xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text> 
    <xsl:copy-of select="*"/>  
    <xsl:text disable-output-escaping="yes">]]&gt;</xsl:text> 
    </xsl:copy> 
</xsl:template> 

或者这样:

<xsl:template match="teaserText_fr"> 
    <teaserText_fr> 
    <xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text> 
    <xsl:copy-of select="*"/>  
    <xsl:text disable-output-escaping="yes">]]&gt;</xsl:text> 
    </teaserText_fr> 
</xsl:template> 

(我推荐的第一种方法)

,你应该全部设置。

给予同样的待遇的任何元素,其名称为“teaserText_”开头:

<xsl:template match="*[starts-with(local-name(), 'teaserText_')]"> 
    <xsl:copy> 
    <xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text> 
    <xsl:copy-of select="*"/>  
    <xsl:text disable-output-escaping="yes">]]&gt;</xsl:text> 
    </xsl:copy> 
</xsl:template> 
+0

感谢JLRishe,它适用于您的第一种方法(即使我现在不明白)。我怎么能通过例子添加正则表达式来为teaserTest_de获得相同的结果。 – glmrenard 2013-03-20 21:10:16

+2

我所做的只是添加一个'xsl:copy',它对上下文节点进行浅拷贝(在这种情况下,它是一个'teaserText_fr')。从本质上讲,当前节点是一个元素时,它会为当前元素的内容添加标签。我不确定你需要一个正则表达式来处理“teaserText_de”;我在回答结束时添加的模板如何? – JLRishe 2013-03-20 21:15:38

2

一个清洁的方法是利用CDATA段元素

Delcare teaserText_fr in cdata-section-elements如下

<xsl:output method="xml" indent="yes" version="1.0" encoding="UTF-16" 
standalone="yes" cdata-section-elements="teaserText_fr" /> 

然后如下所示格式化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>