【问题标题】:XSL 1.0: Identity transform that skips elements where an attribute is emptyXSL 1.0:跳过属性为空的元素的身份转换
【发布时间】:2016-08-25 14:58:58
【问题描述】:

我希望修改身份转换以跳过任何属性为空的元素,例如: 复制<anyElement anyAttr1="a" anyAttr2="b"/> 但不要复制<anotherElement anotherAttr1="a" anotherAttr2=""/>

注意事项:

  1. 元素是否有子元素并不重要。

  2. 应该复制完全没有属性的元素(容器元素)

源示例 XML:

<?xml version="1.0" encoding="utf-8"?>
<Parameterset>
    <Type Code=""/>
    <Inventory Code="250" Index="0"/>
    <Inventory Code="350" Index=""/>
</Parameterset>

转换后的示例 XML:

<?xml version="1.0" encoding="utf-8"?>
<Parameterset>
    <Inventory Code="250" Index="0"/>       
</Parameterset>

我能想到的只有:

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

,但这只会删除空属性并仍然复制元素。

【问题讨论】:

    标签: xml xslt-1.0


    【解决方案1】:

    使用&lt;xsl:template match="*[@*[. = '']]"/&gt; 删除这些元素或使用&lt;xsl:template match="*[@*[. = '']]"&gt;&lt;xsl:apply-templates&gt;&lt;/xsl:template&gt; 仅处理它们的内容。当然,这些模板与身份转换模板一起使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-27
      • 1970-01-01
      • 2015-02-13
      相关资源
      最近更新 更多