0

Hi,

Please help me in removing the duplicate nodes from the xml.Condition to remove duplicate nodes is quite complicated.

condition 1:In each policy node under policyKey node i have to check policyNbr and PolicyFormCode/code and policyEffectiveDt and policyID are same in all the policy nodes if they are same i have retain only the policy node which has sourceSystemCd/code='SCBP' present in it.

condition 2:If in the above condition policyNbr and PolicyFormCode/code and policyEffectiveDt and policyID any of this have differnt values i need to display all th policy node.

Input xml: condition 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>

Condition 2: display all three policyNodes

2
  • So if there are no duplicates, you want to keep one without a policy id? Commented Dec 3, 2014 at 5:43
  • Are you familiar with Muenchian grouping? Commented Dec 3, 2014 at 6:52

2 Answers 2

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>

This should work. If the policy has an ID, then copy it. If not and there isn't a matching one with an id then also copy it. (Could be shorter but this should be clear enough). Tested.

Sign up to request clarification or add additional context in comments.

2 Comments

Added the check for policy code. Numbers are compared with number(policyNbr) to handle the different formats.
Everywhere in the above code where we have "number(something)", replace with: "substring (concat ('0000000000000000', something), string-length(something)+1, 16)". This pads it out to 16 bytes with leading zeroes and then takes the right-most 16 characters. If some of the hex values are upper and lower case (eg 7E707 and 7e707) then you will have to wrap that in a transform () statement. Yuck, but that's XSL for you.
0

Please replace the below part in the above xslt and try...

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

1 Comment

xslt does not have regular expressions so you cannot specify leading zeroes. The above translation would replace all zeroes with nulls.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.