0
votes

Input XML

 <Location>
    <LocationNumber>1</LocationNumber>
    <SublocationNumber>1</SublocationNumber>
    <Construction>
        <YearBuilt>2018</YearBuilt>
    </Construction>
    <AdditionalInterest>
        <GeneralPartyInfo>
            <NameInfo>
                <CommercialName>Joe Smith</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>Susan Jones</CommercialName>
            </NameInfo>
            <Addr>
                <Addr1>555 Park Avenue</Addr1>
                <City>Waterville</City>
                <StateProvCd/>
            </Addr>
        </GeneralPartyInfo>
        <AdditionalInterestInfo>
            <NatureInterestCd>Mortgage Holder</NatureInterestCd>
        </AdditionalInterestInfo>
        <GeneralPartyInfo>
            <NameInfo>
                <CommercialName>PNC Bank</CommercialName>
            </NameInfo>
            <Addr>
                <Addr1>2000 Money Street</Addr1>
                <City>Mason</City>
                <StateProvCd/>
            </Addr>
        </GeneralPartyInfo>
        <AdditionalInterestInfo>
            <NatureInterestCd>Additional Interest</NatureInterestCd>
        </AdditionalInterestInfo>
    </AdditionalInterest>
</Location>

Desired output XML

<Location>
    <LocationNumber>1</LocationNumber>
    <SublocationNumber>1</SublocationNumber>
    <Construction>
        <YearBuilt>2018</YearBuilt>
    </Construction>
    <AdditionalInterest>
        <GeneralPartyInfo>
            <NameInfo>
                <CommercialName>Joe Smith</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>Susan Jones</CommercialName>
            </NameInfo>
            <Addr>
                <Addr1>555 Park Avenue</Addr1>
                <City>Waterville</City>
                <StateProvCd>ZZ</StateProvCd>
            </Addr>
        </GeneralPartyInfo>
        <AdditionalInterestInfo>
            <NatureInterestCd>Mortgage Holder</NatureInterestCd>
        </AdditionalInterestInfo>
    </AdditionalInterest>
    <AdditionalInterest>
        <GeneralPartyInfo>
            <NameInfo>
                <CommercialName>PNC Bank</CommercialName>
            </NameInfo>
            <Addr>
                <Addr1>2000 Money Street</Addr1>
                <City>Mason</City>
                <StateProvCd>ZZ</StateProvCd>
            </Addr>
        </GeneralPartyInfo>
        <AdditionalInterestInfo>
            <NatureInterestCd>Additional Interest</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. Also, if the StateProvCd node is blank, add default value of ZZ. Any ideas on how to accomplish this using XSLT 1.0? Thanks!

1
It would be great if you could provide a minimal reproducible example: (1) Input XML. (2) Your logic, and XSLT that tried to implement it. (3) Desired output. (4) XSLT processor and its version.Yitzhak Khabinsky

1 Answers

0
votes
 <xsl:template match="AdditionalInterest">
    <xsl:apply-templates select="GeneralPartyInfo"/>
  </xsl:template>

  <xsl:template match="GeneralPartyInfo">
    <xsl:element name="AdditionalInterest">
      <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
      <xsl:apply-templates select="following-sibling::AdditionalInterestInfo[1]"/>
    </xsl:element>
  </xsl:template>

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