XSLT: Grouping and Count

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JamesDelaney
    New Member
    • Jul 2009
    • 4

    XSLT: Grouping and Count

    Hello All,

    I'm new to XSLT and XML, so please excuse the basic question. I'm trying to apply a number of conditional xsl:if statements to xsl:for-each-group, but it does not filter out the results. I'm assuming this is because the xsl:for-each-group is defining my group and not allowing the ifs to break it apart?

    --------------------
    Here's a sample of my XML, there are about 1200 entries of Student's, so here's one:
    Code:
    <Census>
    <Student Id="100000" ProgramType="Freshman FX" AdmitTerm="N/A">
    <Gender>Female</Gender>
    <Ethnicity>Unknown</Ethnicity>
    <BirthDate>31900</BirthDate>
    <Citizenship>N/A</Citizenship>
    <Degree>Bachelor of Arts</Degree>
    <Major>Business, Marketing</Major>
    <DegreeTWO>N/A</DegreeTWO>
    <MajorTWO>N/A</MajorTWO>
    <Advisor>Tom</Advisor>
    <FinHSGPA>N/A</FinHSGPA>
    <TotCredits UG="2" Grad="3" Load="18" TransferCredits="N/A"/>
    <PermAddress State="CA">
    <City>Fremont</City>
    </PermAddress>
    <SAT1 type="N/A">
    <Score>N/A</Score>
    </SAT1>
    <SAT2 type="N/A">
    <Score>N/A</Score>
    </SAT2>
    <Classification>trad undergrad</Classification>
    </Student>
    </Census>

    Here's my XSL that I'm trying to apply:

    Code:
    <h3>Students by Gender</h3>
    <xsl:for-each-group select="Census/Student" group-by="Gender">
    <xsl:sort select="current-grouping-key()"/>
    <xsl:if test="(Gender='Female') and
    (Advisor='Tom')">
    <p>Number of Students Who Are:
    <b>
    <xsl:value-of select="current-grouping-key()"/>
    </b> is
    <xsl:value-of select="count(current-group())"/>
    </p>
    </xsl:if>
    
    </xsl:for-each-group>


    My Desired Output will give me the count of all females, who have Tom as an advisor, as grouped by their Gender. Currently, it's not applying any of the "if" statements so it's giving me a result of 852 females, rather than the expected value of 8.
    Last edited by Dormilich; Jul 7 '09, 08:01 AM. Reason: please use [code] [/code] tags when posting code
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    need to read through the XSLT 2.0 specs, this will take some time so I can't immediately answer.

    Comment

    • jkmyoung
      Recognized Expert Top Contributor
      • Mar 2006
      • 2057

      #3
      The if is applied to the group. The first one of them has an advisor element Tom. If you want to filter the grouped nodes, you'd want something like this instead: Remove the if clause, and put in a conditional.

      <xsl:value-of select="count(c urrent-group()[Advisor='Tom'])"/>

      ---
      With your current code, if you change the advisor of the first female (to not Tom), you would get nothing.
      ----
      Or, another possibility is prefiltering in the for-each-group clause:
      <xsl:for-each-group select="Census/Student[Advisor='Tom']" group-by="Gender">

      Comment

      • JamesDelaney
        New Member
        • Jul 2009
        • 4

        #4
        Thanks jkmyoung that works perfectly - both options.

        Comment

        Working...