XSL output  solution

chicho12
chicho12 used Ask the Experts™
on
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!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
IT Business Systems Analyst / Software Developer
Top Expert 2015
Commented:
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

Author

Commented:
Great tips. Thanks for the help!!
mccarlIT Business Systems Analyst / Software Developer
Top Expert 2015

Commented:
You're welcome!!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial