XSLT删除空节点

时间:2016-03-17 19:55:25

标签: xml xslt

输入:

<Root><output>
<queries>
    <query name="Test">
        <parameters>
            <parameter>298674,298673,298675,298676</parameter>
        </parameters>
        <queryResults>
            <record id="1">
                <column name="Order">272334</column>
                <column name="Task">272093</column>
                <column name="FirstAction">2709305</column>                     
            </record>
            <record id="2">
                <column name="Order">272334</column>
                <column name="Task">272093</column>
                <column name="FirstAction">2709301</column>
            </record>
            <record id="3">
                <column name="Order">272334</column>
                <column name="Task">272093</column>
                <column name="FirstAction">2709306</column>
            </record>
            <record id="4">
                <column name="Order">268997</column>
                <column name="Task">268756</column>
                <column name="FirstAction">2709307</column>
            </record>
            <record id="5">
                <column name="Order"/>
                <column name="Task"/>
                <column name="FirstAction">2709307</column>
            </record>
        </queryResults>
    </query>
</queries></output>

如何删除clumn Order中没有值的记录?

期望的输出:

<Root><output>
    <queries>
        <query name="Test">
            <parameters>
                <parameter>298674,298673,298675,298676</parameter>
            </parameters>
            <queryResults>
                <record id="1">
                    <column name="Order">272334</column>
                    <column name="Task">272093</column>
                    <column name="FirstAction">2709305</column>                     
                </record>
                <record id="2">
                    <column name="Order">272334</column>
                    <column name="Task">272093</column>
                    <column name="FirstAction">2709301</column>
                </record>
                <record id="3">
                    <column name="Order">272334</column>
                    <column name="Task">272093</column>
                    <column name="FirstAction">2709306</column>
                </record>
                <record id="4">
                    <column name="Order">268997</column>
                    <column name="Task">268756</column>
                    <column name="FirstAction">2709307</column>
                </record>                   
            </queryResults>
        </query>
    </queries></output>

我当前的XSL只删除空节点

<xsl:strip-space elements="*"/>
 <xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>
<xsl:template match="*[not(child::node())]"/>

我似乎无法掌握模板匹配以删除整个“记录”标记... 非常感谢你!

1 个答案:

答案 0 :(得分:1)

添加一个新模板到删除clumn中没有值的记录

<xsl:template match="record[column[ @name='Order']= '']"/>

此匹配记录在名称为Order的列中没有值,并忽略它们。