Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find maximum value of all child elements and get its parent element in XSLT

Tags:

xml

xslt

Using the below XML, I need to figure out which person worked more hours in each site. For instance in the below XML, person 1 worked 8 hours in site 1 but person 2 worked only 6 hours. So result should contain person 1 and site 1 in transformed XML. If the hours are equal, select first person.

EDIT: I want this to be implemented using XSLT 1.0.

<root>
    <WorkSite Person="P1" Site="S1">
        <Hours>8</Hours>
    </WorkSite>
    <WorkSite Person="P1" Site="S2">
        <Hours>2</Hours>
    </WorkSite>
    <WorkSite Person="P1" Site="S3">
        <Hours>9</Hours>
    </WorkSite>
    <WorkSite Person="P2" Site="S1">
        <Hours>6</Hours>
    </WorkSite>
    <WorkSite Person="P2" Site="S2">
        <Hours>10</Hours>
    </WorkSite>
    <WorkSite Person="P2" Site="S3">
        <Hours>2</Hours>
    </WorkSite>
</root>

XSLT transform result should be like this:

<root> 
    <WorkSite Person="P1" Site="S1"/>  
    <WorkSite Person="P2" Site="S2"/> 
    <WorkSite Person="P1" Site="S3"/> 
</root>
like image 560
Amzath Avatar asked Jan 21 '23 15:01

Amzath


2 Answers

This XSLT 1.0 transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:key name="kSiteByName" match="@Site" use="."/>

 <xsl:key name="kWorksiteBySite"
   match="WorkSite" use="@Site"/>

 <xsl:variable name="vSites" select=
  "/*/*/@Site[generate-id()
             =
              generate-id(key('kSiteByName',.)[1])
              ]"
  />

 <xsl:template match="/">
  <root>
    <xsl:for-each select="$vSites">
      <xsl:for-each select="key('kWorksiteBySite', .)">
        <xsl:sort select="Hours" data-type="number"
         order="descending"/>
        <xsl:if test="position()=1">
         <xsl:copy>
           <xsl:copy-of select="@*"/>
         </xsl:copy>
        </xsl:if>
      </xsl:for-each>
    </xsl:for-each>
  </root>
 </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document:

<root>
    <WorkSite Person="P1" Site="S1">
        <Hours>8</Hours>
    </WorkSite>
    <WorkSite Person="P1" Site="S2">
        <Hours>2</Hours>
    </WorkSite>
    <WorkSite Person="P1" Site="S3">
        <Hours>9</Hours>
    </WorkSite>
    <WorkSite Person="P2" Site="S1">
        <Hours>6</Hours>
    </WorkSite>
    <WorkSite Person="P2" Site="S2">
        <Hours>10</Hours>
    </WorkSite>
    <WorkSite Person="P2" Site="S3">
        <Hours>2</Hours>
    </WorkSite>
</root>

produces the wanted, correct result:

<root>
    <WorkSite Person="P1" Site="S1"/>
    <WorkSite Person="P2" Site="S2"/>
    <WorkSite Person="P1" Site="S3"/>
</root>

Do note:

  1. The use of the Muenchian method for grouping to find all different Site values.

  2. The way maximum is found by sorting in descending order and getting the first result from the sorted node-list.

like image 175
Dimitre Novatchev Avatar answered Jan 24 '23 05:01

Dimitre Novatchev


<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

   <xsl:template match="/">
      <root>
         <xsl:for-each-group select="*/WorkSite" group-by="@Site">
            <WorkSite Person="{(current-group()[Hours = max(current-group()/Hours)])[1]/@Person}" Site="{current-grouping-key()}" />
         </xsl:for-each-group>
      </root>
   </xsl:template>

</xsl:stylesheet>
like image 29
Max Toro Avatar answered Jan 24 '23 05:01

Max Toro