如何使用xsl将cdata添加到xml文件 [英] how to add cdata to an xml file using xsl

查看:81
本文介绍了如何使用xsl将cdata添加到xml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将CDATA封装为xml文件中的元素.

I am trying to wrap CDATA for the elements in an xml file.

我想念的另一点是,有些元素具有相同的名称,需要避免添加CDATA.

Another point i missed was, there are some elements with same names those need to be escaped from adding CDATA.

这是源xml文件:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<books>
    <jndi:binding name="books/cat/action/configs">
        <jndi:value type="java.lang.String">
            <urlConfig>
              <defaults catID="1983" subcatID="1987" method="get" onError="keep"/>
              <urlKey name="logo" altURL="def.com">
                <address>abc.com</address>
              </urlKey>
              <urlKey name="logo1" altURL="def.com">
                <address>abc.com</address>
              </urlKey>
            </urlConfig>
        </jndi:value>
    </jndi:binding>
    <jndi:binding name="books/cat/romance/configs">
        <jndi:value type="java.lang.String">
            <urlConfig>
              <defaults catID="1983" subcatID="1987" method="get" onError="keep"/>          
              <urlKey name="logo" altURL="def.com">
                <address>abc.com</address>
              </urlKey>          
              <urlKey name="logo1" altURL="def.com">
                <address>abc.com</address>
              </urlKey>  
            </urlConfig>
        </jndi:value>
    </jndi:binding>
<jndi:binding name="books/cat/thriller/configs">
    <jndi:value type="java.lang.String">
        abc.com
    </jndi:value>
</jndi:binding> 
<jndi:binding name="books/cat/classic/configs">
    <jndi:value type="java.lang.String">
        abc.com
    </jndi:value>
</jndi:binding>
</books>

我尝试使用@Dimitre Novatchev在

I have tried to use the same trick that @Dimitre Novatchev has mentioned here at How to insert CDATA into XML text markup exported from Access 2003?. But some how that's not working for me.

这是我尝试过的xsl文件:

This is the xsl file that i tried:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:jndi="urn:jboss:jndi-binding-service:1.0"  >
 <xsl:strip-space elements="*"/>
 <xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>
</xsl:stylesheet>

有人可以建议我如何将CDATA添加到元素,还需要添加另一行<?xml version ="1.0" encoding ="ISO-8859-1"吗?> 在CDATA的开头.

Can some one suggest me how to add CDATA to an element and also need to add an additional line <?xml version="1.0" encoding="ISO-8859-1"?> at the beginning of CDATA.

这是预期的结果:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<books>
    <jndi:binding name="books/cat/action/configs">
        <jndi:value type="java.lang.String">
        <![CDATA[
            <?xml version="1.0" encoding="ISO-8859-1"?>
            <urlConfig>
              <defaults catID="1983" subcatID="1987" method="get" onError="keep"/>
              <urlKey name="logo" altURL="def.com">
                <address>abc.com</address>
              </urlKey>
              <urlKey name="logo1" altURL="def.com">
                <address>abc.com</address>
              </urlKey>
            </urlConfig>
        ]]> 
        </jndi:value>
    </jndi:binding>
    <jndi:binding name="books/cat/romance/configs">
        <jndi:value type="java.lang.String">
        <![CDATA[
            <?xml version="1.0" encoding="ISO-8859-1"?>
            <urlConfig>
              <defaults catID="1983" subcatID="1987" method="get" onError="keep"/>          
              <urlKey name="logo" altURL="def.com">
                <address>abc.com</address>
              </urlKey>          
              <urlKey name="logo1" altURL="def.com">
                <address>abc.com</address>
              </urlKey>  
            </urlConfig>
        ]]> 
        </jndi:value>
    </jndi:binding>
<jndi:binding name="books/cat/thriller/configs">
    <jndi:value type="java.lang.String">
        abc.com
    </jndi:value>
</jndi:binding> 
<jndi:binding name="books/cat/classic/configs">
    <jndi:value type="java.lang.String">
        abc.com
    </jndi:value>
</jndi:binding>
</books>

推荐答案

问题是您需要将 CDATA 部分中包含的XML转换为文本.该XSLT应该可以完成此任务:

The problem is that you need to convert the XML that goes inside the CDATA sections into text. This XSLT should do the job:

<xsl:stylesheet 
  version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:jndi="urn:jboss:jndi-binding-service:1.0">

  <xsl:output method="xml" indent="yes" cdata-section-elements="jndi:value"/>

  <xsl:template match="jndi:value">
    <xsl:copy>
      <!-- Copy the attributes -->
      <xsl:apply-templates select="@*"/>
      <!-- Convert the contained nodes (elements and text) into text -->
      <xsl:variable name="subElementsText">
        <xsl:apply-templates select="node()" mode="asText"/>
      </xsl:variable>
      <!-- Output the XML directive and the converted nodes -->
      <xsl:value-of select="concat('&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;',$subElementsText)"/>
    </xsl:copy>
  </xsl:template>

  <!-- Copy text nodes as text -->
  <xsl:template match="text()" mode="asText">
    <xsl:copy/>
  </xsl:template>

  <!-- Copy elements as text -->
  <xsl:template match="*" mode="asText">
    <xsl:value-of select="concat('&lt;',name())"/>
    <xsl:for-each select="@*">
      <xsl:value-of select="concat(' ',name(),'=&quot;',.,'&quot;')"/>
    </xsl:for-each>
    <xsl:value-of select="'&gt;'"/>
    <xsl:apply-templates select="node()" mode="asText"/>
    <xsl:value-of select="concat('&lt;/',name(),'&gt;')"/>
  </xsl:template>

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

基本上,它使用几个模板来匹配需要转换为文本并手动"转换的XML元素.其余的操作由 cdata-sections-elements 指令完成.

Basically it uses a couple of templates to match the XML elements that need to be converted to text and convert them 'manually'. The cdata-sections-elements directive does the rest.

应用于

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<books xmlns:jndi="urn:jboss:jndi-binding-service:1.0">
  <jndi:binding name="books/cat/action/configs">
    <jndi:value type="java.lang.String">
      <urlConfig>
        <defaults catID="1983" subcatID="1987" method="get" onError="keep"/>
        <urlKey name="logo" altURL="def.com">
          <address>abc.com</address>
        </urlKey>
        <urlKey name="logo1" altURL="def.com">
          <address>abc.com</address>
        </urlKey>
      </urlConfig>
    </jndi:value>
  </jndi:binding>
  <jndi:binding name="books/cat/romance/configs">
    <jndi:value type="java.lang.String">
      <urlConfig>
        <defaults catID="1983" subcatID="1987" method="get" onError="keep"/>
        <urlKey name="logo" altURL="def.com">
          <address>abc.com</address>
        </urlKey>
        <urlKey name="logo1" altURL="def.com">
          <address>abc.com</address>
        </urlKey>
      </urlConfig>
    </jndi:value>
  </jndi:binding>
</books>

产生预期的结果:

<?xml version="1.0" encoding="utf-8"?>
<books xmlns:jndi="urn:jboss:jndi-binding-service:1.0">
  <jndi:binding name="books/cat/action/configs">
    <jndi:value type="java.lang.String"><![CDATA[<?xml version="1.0" encoding="utf-8"?>
      <urlConfig>
        <defaults catID="1983" subcatID="1987" method="get" onError="keep"></defaults>
        <urlKey name="logo" altURL="def.com">
          <address>abc.com</address>
        </urlKey>
        <urlKey name="logo1" altURL="def.com">
          <address>abc.com</address>
        </urlKey>
      </urlConfig>
    ]]></jndi:value>
  </jndi:binding>
  <jndi:binding name="books/cat/romance/configs">
    <jndi:value type="java.lang.String"><![CDATA[<?xml version="1.0" encoding="utf-8"?>
      <urlConfig>
        <defaults catID="1983" subcatID="1987" method="get" onError="keep"></defaults>
        <urlKey name="logo" altURL="def.com">
          <address>abc.com</address>
        </urlKey>
        <urlKey name="logo1" altURL="def.com">
          <address>abc.com</address>
        </urlKey>
      </urlConfig>
    ]]></jndi:value>
  </jndi:binding>
</book

这篇关于如何使用xsl将cdata添加到xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆