XSL output solution

Hi,
      I am new to XSL and I'm having issues coming up with a solution for this problem.
      I have the XML structure below:
      <Root>
      <CUs>
            <CU>                  
                  <ContractNr>12345</ContractNr>                  
                  <FullName>Alison full</FullName>            
                  <id>111</id>      
            </CU>
            <CU>                  
                  <ContractNr>45678</ContractNr>                  
                  <FullName>Nancy Full</FullName>      
                  <id>222</id>                  
            </CU>            
      </CUs>      
      <Results>
            <Result>
         <AccountId>0000-0000-0005</AccountId>
         <OrgNr>12345</OrgNr>
             <Status>Active</Status>
             <FullName>Alison full</FullName>
        </Result>
        <Result>
          <AccountId>0000-0000-0005</AccountId>
          <OrgNr>45678</OrgNr>
          <Status>Active</Status>
          <FullName>Steve Full</FullName>
        </Result>                  
      </Results>      
</Root>
      
      
      I need using XSLT to output the following XML result:
      <Output>
       <Customer>
        <ContractNr>45678</ContractNr>      
        <FullName>Nancy Full</FullName>
        <Status>Active</Status>
       </Customer>
      </Output>
      
      What i need is to loop into all <CU> and using the <ContractNr> value to finds its match <Result>.  The <OrgNr> of a Result will contain the same value as <ContractNr> in a CU.
      Once the 2 records are matched I need to compare the FullName from the <CU> against the FullName of the <Result>. If the FullNames are not the same then output the XML above.
      If the fullname is the same then do nothing.
      So the output xml above is what I'd get if i use the input xml at the begining.      
      Thanks!
chicho12Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mccarlIT Business Systems Analyst / Software DeveloperCommented:
Try the following XSL. It uses the <xsl:key> element to generate essentially an index of the <Result> items based on their <OrgNr> values, so that you can efficiently look these up when you iterate through the CUs. Now assuming that your XML is more complex than the above, you may want to do some performance analysis comparing between the below approach (ie. key on Result and then iterate through the CU values) and one where you key on CU values and then iterate through the Results instead.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="xml" indent="yes"/>
    
    <xsl:key name="results" match="/Root/Results/Result" use="OrgNr" />
        
	<xsl:template match="/Root">
        <Output>
		<xsl:for-each select="CUs/CU">
            <xsl:if test="FullName != key('results', ContractNr)/FullName">
                <Customer>
                    <ContractNr><xsl:value-of select="ContractNr" /></ContractNr>
                    <FullName><xsl:value-of select="FullName" /></FullName>
                    <Status><xsl:value-of select="key('results', ContractNr)/Status" /></Status>
                </Customer>
            </xsl:if>
        </xsl:for-each>
        </Output>
	</xsl:template>
</xsl:stylesheet>

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
chicho12Author Commented:
Great tips. Thanks for the help!!
mccarlIT Business Systems Analyst / Software DeveloperCommented:
You're welcome!!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.