Skip to content Skip to sidebar Skip to footer

Adding XML Tags Dynamically And Should Not Allow Duplicates Based On Count Of Particular Tags In Xml Using XSLT

I have challenge to add xml tags dynamically based on count of one xml tag and also should not allow duplicates (I am using XSLT 1.0). For ex: I have 3 Creditor records in 'Credito

Solution 1:

Use Muenchian grouping to get only distinct creditors:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:key name="creditor" match="Creditor" use="Contact/AddressBookUID" />

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="PolicyContactRoles">
    <xsl:copy>
        <xsl:for-each select="//Creditor[count(. | key('creditor', Contact/AddressBookUID)[1]) = 1]">
            <Entry>
                <AccountContactRole>
                    <Subtype>Creditor_De</Subtype>
                    <AccountContact>
                        <Contact>
                            <xsl:copy-of select="Contact/AddressBookUID"/>
                        </Contact>
                    </AccountContact>
                </AccountContactRole>
                <Subtype>PolicyCreditor_De</Subtype>
            </Entry>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

Post a Comment for "Adding XML Tags Dynamically And Should Not Allow Duplicates Based On Count Of Particular Tags In Xml Using XSLT"