Connecting Tech Pros Worldwide Help | Site Map

XSLT: Grouping and Count

  #1  
Old July 6th, 2009, 09:44 PM
Newbie
 
Join Date: Jul 2009
Posts: 4
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:
Expand|Select|Wrap|Line Numbers
  1. <Census>
  2. <Student Id="100000" ProgramType="Freshman FX" AdmitTerm="N/A">
  3. <Gender>Female</Gender>
  4. <Ethnicity>Unknown</Ethnicity>
  5. <BirthDate>31900</BirthDate>
  6. <Citizenship>N/A</Citizenship>
  7. <Degree>Bachelor of Arts</Degree>
  8. <Major>Business, Marketing</Major>
  9. <DegreeTWO>N/A</DegreeTWO>
  10. <MajorTWO>N/A</MajorTWO>
  11. <Advisor>Tom</Advisor>
  12. <FinHSGPA>N/A</FinHSGPA>
  13. <TotCredits UG="2" Grad="3" Load="18" TransferCredits="N/A"/>
  14. <PermAddress State="CA">
  15. <City>Fremont</City>
  16. </PermAddress>
  17. <SAT1 type="N/A">
  18. <Score>N/A</Score>
  19. </SAT1>
  20. <SAT2 type="N/A">
  21. <Score>N/A</Score>
  22. </SAT2>
  23. <Classification>trad undergrad</Classification>
  24. </Student>
  25. </Census>

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

Expand|Select|Wrap|Line Numbers
  1. <h3>Students by Gender</h3>
  2. <xsl:for-each-group select="Census/Student" group-by="Gender">
  3. <xsl:sort select="current-grouping-key()"/>
  4. <xsl:if test="(Gender='Female') and
  5. (Advisor='Tom')">
  6. <p>Number of Students Who Are:
  7. <b>
  8. <xsl:value-of select="current-grouping-key()"/>
  9. </b> is
  10. <xsl:value-of select="count(current-group())"/>
  11. </p>
  12. </xsl:if>
  13.  
  14. </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; July 7th, 2009 at 09:01 AM. Reason: please use [code] [/code] tags when posting code
  #2  
Old July 7th, 2009, 09:10 AM
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,485
Provided Answers: 9

re: XSLT: Grouping and Count


need to read through the XSLT 2.0 specs, this will take some time so I can't immediately answer.
  #3  
Old July 7th, 2009, 05:01 PM
Moderator
 
Join Date: Mar 2006
Posts: 1,103

re: XSLT: Grouping and Count


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(current-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">
  #4  
Old July 7th, 2009, 06:51 PM
Newbie
 
Join Date: Jul 2009
Posts: 4

re: XSLT: Grouping and Count


Thanks jkmyoung that works perfectly - both options.
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
XSLT grouping / xsl:key problem Leira answers 0 June 25th, 2009 05:32 PM
Help on xslt - grouping Per Jørgen Vigdal answers 6 July 20th, 2005 09:39 AM
Transforming with XSLT, Grouping elements until difference found. Jody Greening answers 5 July 20th, 2005 09:08 AM
Transforming with XSLT, Grouping elements until difference found. Jody Greening answers 0 July 20th, 2005 09:08 AM