xmlxsltxslt-groupingaltova

How to use the Group By Clause


I'm trying to achieve a XSLT mapping for a XML data.

Here is what i'm looking for.

Input XML :

    <ListOfPersons xmlns="http://ws.wso2.org/dataservice">
  <PersonDisclosureDate>
     <Emplid>123456</Emplid>
     <uniqname>abcd</uniqname>
     <HasDisclosed>0</HasDisclosed>
     <HasDisclosedThisFY>1</HasDisclosedThisFY>
     <LastDiscloseDt></LastDiscloseDt>
     <HasInterest>1</HasInterest>
     <oeID>FI00006088</oeID>
     <oeName>ArborSense, Inc.</oeName>
     <oeStatus>Under Modification</oeStatus>
     <oeSignDate>2016-07-15T15:53:42.000-04:00</oeSignDate>
     <relationship>ID00000001</relationship>
     <Types>Equity Interest - Non-Public::Advisory, Strategic, Scientific, or Medical Board; and/or Government Agency::Leadership Role</Types>
     <startdt>2016-02-08T00:00:00.000-05:00</startdt>
     <enddt/>
     <oeDCstate>Department Review</oeDCstate>
  </PersonDisclosureDate>
  <PersonDisclosureDate>
     <Emplid>23456</Emplid>
     <uniqname>pqr</uniqname>
     <HasDisclosed>0</HasDisclosed>
     <HasDisclosedThisFY>1</HasDisclosedThisFY>
     <LastDiscloseDt></LastDiscloseDt>
     <HasInterest>1</HasInterest>
     <oeID>FI00003483</oeID>
     <oeName>Dimension Therapeutics</oeName>
     <oeStatus>Under Modification</oeStatus>
     <oeSignDate>2016-09-30T09:09:05.000-04:00</oeSignDate>
     <relationship>ID00000001</relationship>
     <Types>Consultant</Types>
     <startdt>2014-11-18T00:00:00.000-05:00</startdt>
     <enddt/>
     <oeDCstate>Department Review</oeDCstate>
  </PersonDisclosureDate>
  <PersonDisclosureDate>
     <Emplid>23456</Emplid>
     <uniqname>pqr</uniqname>
     <HasDisclosed>0</HasDisclosed>
     <HasDisclosedThisFY>1</HasDisclosedThisFY>
     <LastDiscloseDt></LastDiscloseDt>
     <HasInterest>1</HasInterest>
     <oeID>FI00007824</oeID>
     <oeName>intercept</oeName>
     <oeStatus>Under Modification</oeStatus>
     <oeSignDate>2016-09-30T09:09:58.000-04:00</oeSignDate>
     <relationship>ID00000001</relationship>
     <Types>Advisory, Strategic, Scientific, or Medical Board; and/or Government Agency::Travel</Types>
     <startdt>2016-10-05T00:00:00.000-04:00</startdt>
     <enddt/>
     <oeDCstate>Department Review</oeDCstate>
  </PersonDisclosureDate>
  <PersonDisclosureDate>
     <Emplid>23456</Emplid>
     <uniqname>pqr</uniqname>
     <HasDisclosed>0</HasDisclosed>
     <HasDisclosedThisFY>1</HasDisclosedThisFY>
     <LastDiscloseDt></LastDiscloseDt>
     <HasInterest>1</HasInterest>
     <oeID>FI00007823</oeID>
     <oeName>wilson therapeutics</oeName>
     <oeStatus>Under Modification</oeStatus>
     <oeSignDate>2016-09-30T09:09:31.000-04:00</oeSignDate>
     <relationship>ID00000001</relationship>
     <Types>Advisory, Strategic, Scientific, or Medical Board; and/or Government Agency::Travel</Types>
     <startdt>2016-11-09T00:00:00.000-05:00</startdt>
     <enddt/>
     <oeDCstate>Department Review</oeDCstate>
  </PersonDisclosureDate>
  <PersonDisclosureDate>
     <Emplid>34567</Emplid>
     <uniqname>xyz</uniqname>
     <HasDisclosed xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
     <HasDisclosedThisFY>0</HasDisclosedThisFY>
     <LastDiscloseDt xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
     <HasInterest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
     <oeID xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
     <oeName xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
     <oeStatus xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
     <oeSignDate xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
     <relationship xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
     <Types xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
     <startdt xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
     <enddt/>
     <oeDCstate/>
  </PersonDisclosureDate>
</ListOfPersons>

Expected Response:

<?xml version="1.0" encoding="UTF-8"?>
<DisclosureData xsi:noNamespaceSchemaLocation="C:/XSL/eResearch_Expected_Response.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <count>3</count>
    <disclosures>
        <uniqName>abcd</uniqName>
        <hasDisclosed>0</hasDisclosed>
        <lastDiscloseDt></lastDiscloseDt>
        <disclosureRecords>
            <oeId>FI00006088</oeId>
            <oeName>ArborSense, Inc.</oeName>
            <oeSignDate>2016-07-15T15:53:42.000-04:00</oeSignDate>
            <oeRelationship>ID00000001</oeRelationship>
            <oeTypes>Equity Interest - Non-Public::Advisory, Strategic, Scientific, or Medical Board; and/or Government Agency::Leadership Role</oeTypes>
            <oeStartDt>2016-02-08T00:00:00.000-05:00</oeStartDt>
            <oeEndDt/>
        </disclosureRecords>
    </disclosures>
    <disclosures>
        <uniqName>pqr</uniqName>
        <hasDisclosed>0</hasDisclosed>
        <lastDiscloseDt></lastDiscloseDt>
        <disclosureRecords>
            <oeId>FI00003483</oeId>
            <oeName>Dimension Therapeutics</oeName>
            <oeSignDate>2016-09-30T09:09:05.000-04:00</oeSignDate>
            <oeRelationship>ID00000001</oeRelationship>
            <oeTypes>Consultant</oeTypes>
            <oeStartDt>2014-11-18T00:00:00.000-05:00</oeStartDt>
            <oeEndDt/>
        </disclosureRecords>
        <disclosureRecords>
            <oeId>FI00007824</oeId>
            <oeName>intercept</oeName>
            <oeSignDate>2016-09-30T09:09:58.000-04:00</oeSignDate>
            <oeRelationship>ID00000001</oeRelationship>
            <oeTypes>Advisory, Strategic, Scientific, or Medical Board; and/or Government Agency::Travel</oeTypes>
            <oeStartDt>2016-10-05T00:00:00.000-04:00</oeStartDt>
            <oeEndDt/>
        </disclosureRecords>
        <disclosureRecords>
            <oeId>FI00007823</oeId>
            <oeName>wilson therapeutics</oeName>
            <oeSignDate>2016-09-30T09:09:31.000-04:00</oeSignDate>
            <oeRelationship>ID00000001</oeRelationship>
            <oeTypes>Advisory, Strategic, Scientific, or Medical Board; and/or Government Agency::Travel</oeTypes>
            <oeStartDt>2016-11-09T00:00:00.000-05:00</oeStartDt>
            <oeEndDt/>
        </disclosureRecords>
    </disclosures>
    <disclosures>
        <uniqName>xyz</uniqName>
        <hasDisclosed/>
        <lastDiscloseDt/>
        <disclosureRecords>
            <oeId/>
            <oeName/>
            <oeSignDate/>
            <oeRelationship/>
            <oeTypes/>
            <oeStartDt/>
            <oeEndDt/>
        </disclosureRecords>
    </disclosures>
</DisclosureData>

I tried using the ALtova Mapforce tool to map the xml but didn't get the proper results.

Altova Mapping: enter image description here

Can someone please provide some guidance on how to achieve the above result using Altova Mapforce or even a normal XSLT document using the group by tag ?

Thanks in advance, Kranthi


Solution

  • It appears to be a simple for-each-group selecting PersonDisclosureDate and grouping by uniqname.

    Here's a basic example using a pull approach for simplicity...

    XSLT 2.0 (working example here: http://xsltfiddle.liberty-development.net/nbUY4kp)

    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
      xpath-default-namespace="http://ws.wso2.org/dataservice">
      <xsl:output indent="yes"/>
      <xsl:strip-space elements="*"/>
    
      <xsl:template match="/*">
        <DisclosureData xsi:noNamespaceSchemaLocation="C:/XSL/eResearch_Expected_Response.xsd" 
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
          <count><xsl:value-of select="count(distinct-values(/*/PersonDisclosureDate/uniqname))"/></count>
          <xsl:for-each-group select="PersonDisclosureDate" group-by="uniqname">
            <disclosures>
              <uniqName><xsl:value-of select="current-grouping-key()"/></uniqName>
              <hasDisclosed><xsl:value-of select="current-group()[1]/HasDisclosed"/></hasDisclosed>
              <lastDiscloseDt><xsl:value-of select="current-group()[1]/LastDiscloseDt"/></lastDiscloseDt>
              <xsl:for-each select="current-group()">
                <disclosureRecords>
                  <oeId><xsl:value-of select="oeID"/></oeId>
                  <oeName><xsl:value-of select="oeName"/></oeName>
                  <oeSignDate><xsl:value-of select="oeSignDate"/></oeSignDate>
                  <oeRelationship><xsl:value-of select="relationship"/></oeRelationship>
                  <oeTypes><xsl:value-of select="Types"/></oeTypes>
                  <oeStartDt><xsl:value-of select="startdt"/></oeStartDt>
                  <oeEndDt><xsl:value-of select="enddt"/></oeEndDt>
                </disclosureRecords>
              </xsl:for-each>            
            </disclosures>
          </xsl:for-each-group>
        </DisclosureData>
      </xsl:template>
    
    </xsl:stylesheet>