如何使用xslt 1.0删除xml中的重复节点

时间:2014-12-03 05:23:25

标签: xslt-1.0


您好,

请帮我从xml.Condition中删除重复的节点以删除重复的节点是非常复杂的。

条件1:在policyKey节点下的每个策略节点中,我必须检查policyNbr和PolicyFormCode / code以及policyEffectiveDt和policyID 如果它们相同,则在所有策略节点中都相同,我只保留其中存在sourceSystemCd / code ='SCBP'的策略节点。

条件2:如果在上述条件中policyNbr和PolicyFormCode / code以及policyEffectiveDt和policyID中的任何一个具有不同的值,我需要显示所有策略节点。

输入xml:   条件1:

<?xml version="1.0" encoding="utf-8"?>
<policies>
    <!-- policy 1-->
    <policy>
        <policyKey>
            <policyNbr>4567</policyNbr>
            <policyEffectiveDt>2014-11-14</policyEffectiveDt>
            <policyFormCd>
                <code>669</code>
            </policyFormCd>
        </policyKey>
        <transactionSplitTrans>
            <sourceSystemCd>
                <code>ARA</code>
            </sourceSystemCd>
        </transactionSplitTrans>
    </policy> 
    <!-- second -->
    <policy>
        <policyKey>
            <policyNbr>1234</policyNbr>
            <policyID>115774001</policyID>
            <policyEffectiveDt>2014-11-11</policyEffectiveDt>
            <policyFormCd>
                <code>660</code>
            </policyFormCd>
        </policyKey>
        <transactionSplitTrans>
            <sourceSystemCd>
                <code>ARAR</code>
            </sourceSystemCd>
        </transactionSplitTrans>
    </policy>
    <!-- third -->
    <policy>
        <policyKey>
            <policyEffectiveDt>2014-11-14</policyEffectiveDt>
            <policyFormCd>
                <code>660</code>
            </policyFormCd>
            <policyID>115774001</policyID>
            <policyNbr>1234</policyNbr>
        </policyKey>
        <transactionSplitTrans>
            <sourceSystemCd>
                <code>SCBP</code>
            </sourceSystemCd>
        </transactionSplitTrans>
    </policy>
</policies>

Expexted Output:

    <policies>
    <!-- policy 1-->
<policy>
    <policyKey>
        <policyNbr>4567</policyNbr>
        <policyEffectiveDt>2014-11-14</policyEffectiveDt>
        <policyFormCd>
            <code>669</code>
        </policyFormCd>
    </policyKey>
    <transactionSplitTrans>
        <sourceSystemCd>
            <code>ARA</code>
        </sourceSystemCd>
    </transactionSplitTrans>
</policy>
<!-- third -->
<policy>
    <policyKey>
        <policyEffectiveDt>2014-11-14</policyEffectiveDt>
        <policyFormCd>
            <code>660</code>
        </policyFormCd>
        <policyID>115774001</policyID>
        <policyNbr>1234</policyNbr>
    </policyKey>
    <transactionSplitTrans>
        <sourceSystemCd>
            <code>SCBP</code>
        </sourceSystemCd>
    </transactionSplitTrans>
</policy>
</policies>

条件2:显示所有三个policyNodes

2 个答案:

答案 0 :(得分:1)

<xsl:template match="/policies/policy">
<xsl:choose>
    <xsl:when test="./policyKey/policyID">
        <xsl:copy-of select='.'/>
    </xsl:when>
    <xsl:otherwise>
        <xsl:variable name='currNumber' select="number(policyKey/policyNbr)"/>
        <xsl:variable name="currCode" select="policyKey/policyFormCd/code"/>
        <xsl:if test="count(../policy/policyKey[number(policyNbr)=$currNumber and policyFormCd/code=$currCode and policyID]) = 0">
            <xsl:copy-of select='.'/>
        </xsl:if>
    </xsl:otherwise>
</xsl:choose>
</xsl:template>

这应该有效。如果策略具有ID,则将其复制。如果没有,并且没有匹配的id,那么也复制它。 (可能会更短但是应该足够清楚)。测试

答案 1 :(得分:0)

请替换上面xslt中的以下部分并尝试...

<xsl:variable name='currNumber' select="translate(policyKey/policyNbr,'0*','')"/>


<xsl:if test="count(../policy/policyKey[translate(policyNbr,'0*','')=$currNumber and policyFormCd/code=$currCode and policyID]) = 0">