-3

I tried with many XSLT methods like Key, for-each-group and match method options, but it is not working for me.

Input XML:

<?xml version='1.0' encoding='UTF-8'?>
<CustomerRecord>
    <Customer>
        <chargeto>ABC</chargeto>
        <chargename>GARY</chargename>
    </Customer>
    <Customer>
        <chargeto>XYZ</chargeto>
        <chargename>DAVIS</chargename>
    </Customer>
    <Customer>
        <chargeto>CDE</chargeto>
        <chargename>GARY DAVIS</chargename>
    </Customer>
    <Customer>
        <chargeto>ABC</chargeto>
        <chargename>DAV</chargename>
    </Customer>
</CustomerRecord>

Expected output XML:

<?xml version='1.0' encoding='UTF-8'?>
<Root>
<Customer_PO>
    <Customer>
        <chargeto>ABC</chargeto>
        <chargename>GARY</chargename>
    </Customer>
    <Customer>
        <chargeto>ABC</chargeto>
        <chargename>DAV</chargename>
    </Customer>
</Customer_PO>
<Customer_Falty>
    <Customer>
        <chargeto>XYZ</chargeto>
        <chargename>DAVIS</chargename>
    </Customer>
    <Customer>
        <chargeto>CDE</chargeto>
        <chargename>GARY DAVIS</chargename>
    </Customer>
</Customer_Falty>
</Root>

Below is the XSLT which I wrote Initially to at least get the some details in the Output, but data is getting missed when the target is generated:


    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:var="http://schemas.microsoft.com/BizTalk/2003/var" exclude-result-prefixes="msxsl var" version="1.0" xmlns:ns0="http://Namespace">
      <xsl:output omit-xml-declaration="yes" method="xml" version="1.0" />
      <xsl:key use="chargeto" match="/CustomerRecord/Customer" name ="groups"/>
      
      <xsl:template match="/">
        <xsl:apply-templates select="/CustomerRecord" />
      </xsl:template>
      <xsl:template match="/CustomerRecord">
        <Root>
         <xsl:for-each select="/CustomerRecord/Customer[generate-id(.)=generate-id(key('groups',chargeto))]">      
            <Customer_PO>
              <xsl:for-each select="key('groups',chargeto)">
              <Customer>
                <chargeto>
                  <xsl:value-of select="CustomerRecord/chargeto/text()" />
                </chargeto>
                <chargename>
                  <xsl:value-of select="CustomerRecord/chargename/text()" />
                </chargename>
              </Customer>
            </xsl:for-each>  
            </Customer_PO>
          </xsl:for-each>
        </Root>
      </xsl:template>
    </xsl:stylesheet>

Please find the explanation about the Expected Output

<Customer_PO>. Under this tag we will have all the <Customer> segments where all the <chargeto> fields are having duplicate value

<Customer_Falty> Under this tag we will have all the <Customer> segment where all the <chargeto> field values are unique

michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
ABC
  • 1
  • 2
  • Why do you think that it is not working? – mkrieger1 Aug 14 '22 at 20:25
  • I am new to this xslt and my xslt transformation is not giving the expected results. Please let me know if you can help – ABC Aug 14 '22 at 20:27
  • Why don't you post your attempt so we can fix it, instead of having to write your code for you from scratch. Even more importantly, explain the logic you're trying to apply here, since the result you show makes very little sense to me. – michael.hor257k Aug 14 '22 at 20:29
  • Sorry, I didn't find the explanation. – mkrieger1 Aug 14 '22 at 20:40
  • Please find the explanation about the Expected Output, Under this tag we will have all the segments where all the fields are having same value Under this tag we will have all the segment where all the field values are not common – ABC Aug 14 '22 at 20:41
  • Please see the section starting: "Please find the explanation about the Expected Output". This part of your question is confusing and needs rewriting. The XML tags were not formatted, so it was unreadable. But also, it does not make much sense. Why does it finish with ``? – halfer Aug 21 '22 at 08:08

1 Answers1

0

If I understand correctly your explanation, you want to do something like:

XSL 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:key name="cust-by-charge" match="Customer" use="chargeto" />

<xsl:template match="/CustomerRecord">
    <Root>
        <Customer_PO>
            <xsl:copy-of select="Customer[count(key('cust-by-charge', chargeto)) > 1]"/>
        </Customer_PO>
        <Customer_Falty>
            <xsl:copy-of select="Customer[count(key('cust-by-charge', chargeto)) = 1]"/>
        </Customer_Falty>
    </Root>
</xsl:template>

</xsl:stylesheet>

Possibly there's a more elegant approach that would count the size of each group only once.

michael.hor257k
  • 113,275
  • 6
  • 33
  • 51