【问题标题】:Add CDATA to an xml file将 CDATA 添加到 xml 文件
【发布时间】:2013-03-10 04:18:28
【问题描述】:

我想在一些 xml 标签周围添加一些 CDATA 标签

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>

我想要的是

<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] 这样的正则表达式,但我无法让它工作......“

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

最好的问候,纪尧姆

【问题讨论】:

    标签: xml xslt cdata


    【解决方案1】:

    您需要这样做:

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

    【讨论】:

    • 感谢 JLRishe,它适用于您的第一种方法(即使我现在还不太明白)。例如,如何添加正则表达式以使 teaserTest_de 具有相同的结果。
    • 我所做的只是添加一个xsl:copy,它对上下文节点进行浅拷贝(在本例中,它是一个teaserText_fr)。本质上,当当前节点是一个元素时,它会将当前元素的标签放在里面的内容周围。我不确定你是否需要一个正则表达式来处理“teaserText_de”;我在答案末尾添加的模板怎么样?
    【解决方案2】:

    更简洁的方法是使用 cdata-section-elements

    Cdata-section-elements 中的 teaserText_fr 如下所示

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-03
      • 2011-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-30
      • 1970-01-01
      • 2012-02-17
      相关资源
      最近更新 更多