XSLT from excel with duplicates

Here should go questions about transforming XML with XSLT and FOP.
502hollywood
Posts: 5
Joined: Wed Aug 25, 2021 7:38 am

XSLT from excel with duplicates

Post by 502hollywood »

Greetings!

Is there anything I can add to this stylesheet that would consolidate multiple attributes under one "student".

For example on the excel sheet the data is listed as a rows. the student appears multiple times on the sheet for each scholarship.

Student 1 has 3 scholarships
externalFundID= scholarship1 awardAmount = 400
externalFundID=scholarship2 awardAmount= 500

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">
    <xsl:output indent="yes"/>
    <xsl:template match="root">
        <students>
            <xsl:apply-templates/>
        </students>
    </xsl:template>
    <xsl:template match="row">
        <student><xsl:attribute name="externalStudentId1" select="externalStudentId1/text()"/>
              <award>
                  <xsl:attribute name="awardAmount" select="awardAmount/text()"/>
                  <xsl:attribute name="awardEndDate" select="awardEndDate/text()"/>
                  <xsl:attribute name="awardStartDate" select="awardStartDate/text()"/>
                  <xsl:attribute name="awardStatus" select="awardStatus/text()"/>
                  <xsl:attribute name="externalAwardId" select="externalAwardId/text()"/>
                  <xsl:attribute name="externalFundId" select="externalFundId/text()"/>
                  <xsl:attribute name="AwardYear" select="federalAwardYear/text()"/>
                  <xsl:attribute name="maxDisbursementNumeber" select="maxDisbursementNumber/text()"/>
                  <xsl:attribute name="offeredOnDate" select="offeredOnDate/text()"/>
                  <xsl:attribute name="manualAward" select="manualAward/text()"/>
                 
              </award>
        </student>
</xsl:template>
</xsl:stylesheet>
Radu
Posts: 9433
Joined: Fri Jul 09, 2004 5:18 pm

Re: XSLT from excel with duplicates

Post by Radu »

Hi,

Maybe you can post also a sample input XML document which has two entries for the same student.
I guess you need to use the XSLT for-each-group to group all students with the same ID, something like:

Code: Select all

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="xs"
  version="2.0">
  <xsl:output indent="yes"/>
  <xsl:template match="root">
    <persons>
      <xsl:for-each-group select="row" group-by="externalstudentid1" >
        <person>
          <award>
            <xsl:attribute name="awardAmount" select="sum(current-group()//awardamount/text())"/>
          </award>
        </person>
      </xsl:for-each-group>
    </persons>
  </xsl:template>
</xsl:stylesheet>
Inside the for-each-group the "current-group()" function gives you access to the current sequence of students which have the same ID (the sequence may contain either one or multiple "row" items inside).

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
Post Reply