1

我正在尝试为 xml 文件中的元素包装 CDATA。

我错过的另一点是,有一些具有相同名称的元素需要从添加 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 在如何将 CDATA 插入从 Access 2003 导出的 XML 文本标记中提到的相同技巧?. 但是有些方法对我不起作用。

这是我尝试过的 xsl 文件:

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

<?xml version="1.0" encoding="ISO-8859-1"?>有人可以建议我如何将 CDATA 添加到元素,并且还需要在 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>
4

1 回答 1

1

问题是您需要将CDATA节内的 XML 转换为文本。这个 XSLT 应该可以完成这项工作:

<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指令完成其余的工作。

当应用于

<?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
于 2013-09-09T15:02:27.067 回答