简体   繁体   中英

Counting values in Xslt

I have an xml file like this:

<RD>
<RE>
    <RecordID>D</RecordID>
    <instanceID>00323</instanceID>
    <Employee_Count>31</Employee_Count>
    <Workers>
        <Time_Type Descriptor="Full time">
            <ID type="Position_Time_Type_ID">Full_time</ID>
        </Time_Type>
    </Workers>
    <Workers>
        <Time_Type Descriptor="Full time">
           <ID type="Position_Time_Type_ID">Full_time</ID>
        </Time_Type>
    </Workers>
    <Workers>
        <Time_Type Descriptor="Part time">
            <ID type="Position_Time_Type_ID">Part_time</ID>
        </Time_Type>
    </Workers>
</RE>
<RE>
    <RecordID>D</RecordID>
    <instanceID>09903</instanceID>
    <Employee_Count>41</Employee_Count>
    <Workers>
        <Time_Type Descriptor="Full time">
            <ID type="Position_Time_Type_ID">Full_time</ID>
        </Time_Type>
    </Workers>
    <Workers>
        <Time_Type Descriptor="Full time">
            <ID type="Position_Time_Type_ID">Full_time</ID>
        </Time_Type>
    </Workers>
</RE> </RD>

and I want to produce an output like this:
D0000000323 count of Full Time for instanceID= 00323

D0000000323 count of Part Time for instanceID= 00323
D0000009903 count of Part Time for instanceID= 09903

I have tried various combinations of for-each-group and conditions but I am stuck. Also, I am a beginner in xslt transformations. Please suggest any pointers how I can achieve this.

I am trying the below code:

 <xsl:output method="text"/>
  <xsl:variable name="linefeed" select="'&#xA;'"></xsl:variable>
    <xsl:template match="/">
<xsl:value-of select="$linefeed"/>
<xsl:for-each select="/RD/RE">
    <xsl:variable name="CC">
        <xsl:value-of select="/RD/RE/instanceID"/>
    </xsl:variable>
    <xsl:for-each-group select="RE" group-by="instanceID">
        <xsl:sort select= "current-grouping-key()"/>
        <xsl:variable name="CostCenter" select="current-grouping-key()"/>
        <xsl:variable name="CC1">
            <xsl:value-of select="/RD/RE/instanceID"/>
        </xsl:variable>
        <CC1>
            <xsl:value-of select="$CC1"/>
        </CC1>
        <xsl:variable name="FullTimeCount">
            <xsl:value-of select="count(/RD/RE[instanceID='$CC']/Workers/Time_Type[@Descriptor='Full time'])"/>
        </xsl:variable>
        <FullTime>
            <xsl:value-of select="$FullTimeCount"/>
        </FullTime>
    </xsl:for-each-group>
    <xsl:value-of select="$linefeed"/>
</xsl:for-each>

If the RE elements are already identified by the instanceID then I don't think you have to group them, you only need to group the Workers elements by the descriptor:

<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 method="text"/>

    <xsl:template match="/">
        <xsl:for-each select="RD/RE">
            <xsl:sort select="instanceID"/>
            <xsl:for-each-group select="Workers" group-by="Time_Type/@Descriptor">
                <xsl:sort select="current-grouping-key()"/>
                <xsl:value-of select="concat(../RecordID, format-number(../instanceID, '0000000000'), ' ', current-grouping-key(), ' ', count(current-group()), '&#10;')"/>
            </xsl:for-each-group>       
        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>

Output is eg

D0000000323 Full time 2
D0000000323 Part time 1
D0000009903 Full time 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM