【问题标题】:XSL transformation exclude specific qname for cdata section?XSL 转换排除 cdata 部分的特定 qname?
【发布时间】:2012-12-06 08:58:00
【问题描述】:

我正在做一个 xstl 转换,我想保留来自源 xml 的 CDATA 条目。因此,我相应地定义了 cdata-section-elements。但是我有相同的 qname 属性 name="test"。那里我不想应用 cdata。如何排除此元素?

.xml

<my:request><![CDATA[<foo...>]]>
<my:request>

<my:request name="test">
</my:request>

.xsl

<xsl:output cdata-section-elements="my:request"/>

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

    不,不可能有基于属性的异常!

    但是!!如果您坚持使用替代解决方案,那么 .. 我建议您为不具有属性 my:request 的元素插入 &lt;![CDATA[ name .. 请参阅下面的示例:

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:my="http://www.w3.org/2001/something">
      <xsl:output method="xml" indent="yes"/>
    
      <xsl:template match="@* | node()">
        <xsl:copy>
          <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
      </xsl:template>
    
    
      <xsl:template match="my:request[not(@name)]">
        <xsl:copy>
          <xsl:apply-templates select="@*"/>
    
          <!--Insert CDATA-->
          <xsl:value-of select="'&#60;![CDATA['" disable-output-escaping="yes"/>
          <xsl:apply-templates select="node()"/>
          <xsl:value-of select="']]&#62;'" disable-output-escaping="yes"/>
        </xsl:copy>
      </xsl:template>
    
    
    </xsl:stylesheet>
    

    输入 XML:

    <?xml version="1.0" encoding="utf-8"?>
    <my:root xmlns:my="http://www.w3.org/2001/something">
      <my:request other="something">data</my:request>    
      <my:request name="test">data</my:request>
    </my:root>
    

    输出 XML:

    <?xml version="1.0" encoding="utf-8"?>
    <my:root xmlns:my="http://www.w3.org/2001/something">
      <my:request other="something"><![CDATA[data]]></my:request>    
      <my:request name="test">data</my:request>
    </my:root>
    

    【讨论】:

    • 太棒了!它完全符合我的要求。
    猜你喜欢
    • 1970-01-01
    • 2013-09-07
    • 1970-01-01
    • 2014-03-12
    • 2010-10-08
    • 1970-01-01
    • 2015-10-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多