0

I have two XML files i am trying to merge some elements using xsl.

XML1:

<ALL xmlns:a="http://example.com/ns1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<a:BusinessUnit>
    <a:businessUnitCode>SGS</a:businessUnitCode>
    <a:longName>Singapore Global Sourcing</a:longName>
    <a:parentBusinessUnitCode>CGS</a:parentBusinessUnitCode>
</a:BusinessUnit>
<a:BusinessUnit>
    <a:businessUnitCode>EGH</a:businessUnitCode>
    <a:longName>EMS Global HQ</a:longName> 
    <a:parentBusinessUnitCode xsi:nil="true"/>
</a:BusinessUnit> 

XML2:

<Get_ProductFamily xmlns:aa="http://example.com/ns2">
<aa:ProductFamily>
               <aa:integrationId>2323</aa:integrationId>
               <aa:parentBusinessUnitCode>EGH</aa:parentBusinessUnitCode>        
 </aa:ProductFamily>
 <aa:ProductFamily>
               <aa:integrationId>3434</aa:integrationId>                  
               <aa:parentBusinessUnitCode>CGS</aa:parentBusinessUnitCode>        
 </aa:ProductFamily>
  <aa:ProductFamily>
               <aa:integrationId>4545</aa:integrationId>                  
               <aa:parentBusinessUnitCode>CDD</aa:parentBusinessUnitCode>        
 </aa:ProductFamily>
 </Get_ProductFamily>

output:

<GroupList>
    <Group>
    <Name>EGH - EMS Global HQ</Name>
    <ProductLineList>
    <ProductLine>
    <Name>EGH</Name>
        <CODE>2323</CODE>
    </ProductLine>
    </ProductLineList>
    </Group>
<GroupList>

I want to Read Business unit data from 1st file and product family data from 2nd file and generate group info.

Steps are: Read all business unit

Group name is concatenation of a:businessUnitCode and a:longName (I did this part)

when a:parentBusinessUnitCode is blank, then read its a:businessUnitCode, search this businessUnitCode in 2nd file.

if a:businessUnitCode(1st file) is equal to aa:parentBusinessUnitCode (2nd file) then print its integration id.

Please help me as i am new to xsl.

Nic Gibson
  • 7,051
  • 4
  • 31
  • 40

1 Answers1

0

I searched for bussinessUnitCode in the second XML and displayed corresponding data. Notice I used document() function to read second XML.

<xsl:template match="/">
<GroupList>
 <Group>
 <xsl:apply-templates/>
</Group>
</GroupList>
</xsl:template>
<xsl:template match="a:BusinessUnit">
 <xsl:variable name="businessCode" select="a:businessUnitCode"/>
 <Name>
 <xsl:value-of select="$businessCode"/> - <xsl:value-of select="a:longName"/>
 </Name>
  <xsl:if test="a:parentBusinessUnitCode[@i:nil='true']">
  <ProductLineList>
  <xsl:for-each select=
  "document('XML2.xml')/Get_ProductFamily/aa:ProductFamily">
  <xsl:if test="aa:parentBusinessUnitCode = $businessCode">
  <ProductLine>
   <Name><xsl:value-of select="$businessCode"/></Name>

   <CODE><xsl:value-of select="aa:integrationId"/></CODE>
   </ProductLine></xsl:if>
 </xsl:for-each>
 </ProductLineList>
  </xsl:if>
 </xsl:template>
 </xsl:stylesheet>
mg_kedzie
  • 337
  • 1
  • 9