0
votes

Input XML

    <Location>
        <AdditionalInterest>
            <GeneralPartyInfo>
                <NameInfo>
                    <CommercialName>munmun</CommercialName>
                </NameInfo>
                <Addr>
                    <Addr1>1234 Oak Street</Addr1>
                    <City>Waterville</City>
                    <StateProvCd>ME</StateProvCd>
                </Addr>
            </GeneralPartyInfo>
            <AdditionalInterestInfo>
                <NatureInterestCd>Loss Payee</NatureInterestCd>
            </AdditionalInterestInfo>
            <GeneralPartyInfo>
                <NameInfo>
                    <CommercialName>jaan</CommercialName>
                </NameInfo>
                <Addr>
                    <Addr1>555 Park Avenue</Addr1>
                    <City>Waterville</City>
                    <StateProvCd>ME</StateProvCd>
                </Addr>
            </GeneralPartyInfo>
            <AdditionalInterestInfo>
                <NatureInterestCd>Mortgage Holder</NatureInterestCd>
            </AdditionalInterestInfo>
        </AdditionalInterest>
    </Location>

Desired output XML

    <Location>
        <AdditionalInterest>
            <GeneralPartyInfo>
                <NameInfo>
                    <CommercialName>munmun</CommercialName>
                </NameInfo>
                <Addr>
                    <Addr1>1234 Oak Street</Addr1>
                    <City>Waterville</City>
                    <StateProvCd>ME</StateProvCd>
                </Addr>
            </GeneralPartyInfo>
            <AdditionalInterestInfo>
                <NatureInterestCd>Loss Payee</NatureInterestCd>
            </AdditionalInterestInfo>
        </AdditionalInterest>
        <AdditionalInterest>
            <GeneralPartyInfo>
                <NameInfo>
                    <CommercialName>jaan</CommercialName>
                </NameInfo>
                <Addr>
                    <Addr1>555 Park Avenue</Addr1>
                    <City>Waterville</City>
                    <StateProvCd>ME</StateProvCd>
                </Addr>
            </GeneralPartyInfo>
            <AdditionalInterestInfo>
                <NatureInterestCd>Mortgage Holder</NatureInterestCd>
            </AdditionalInterestInfo>
        </AdditionalInterest>
    </Location>

I need to create separate AdditionalInterest parent nodes, each with child nodes GeneralPartyInfo and AdditionalInterestInfo as shown in the desired output XML above. Any ideas on how to accomplish this using XSLT 1.0? Thanks!

1

1 Answers

0
votes

If it can be assumed that each GeneralPartyInfo is immediately followed by exactly one AdditionalInterestInfo, then you could do:

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:template match="/Location">
    <xsl:copy>
        <xsl:for-each select="AdditionalInterest/GeneralPartyInfo">
             <AdditionalInterest>
                <xsl:copy-of select=". | following-sibling::AdditionalInterestInfo[1]"/>
              </AdditionalInterest>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>