473,394 Members | 1,761 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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:
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>
  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.
Jul 6 '09 #1
3 11756
8,658 Expert Mod 8TB
need to read through the XSLT 2.0 specs, this will take some time so I can't immediately answer.
Jul 7 '09 #2
2,057 Expert 2GB
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">
Jul 7 '09 #3
Thanks jkmyoung that works perfectly - both options.
Jul 7 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

by: Jody Greening | last post by:
Transforming with XSLT, Grouping elements until difference found. I am seeking some help with the following problem, I am fairly new at XSLT transformations, and my problem may lie in looking at...
by: Tristan Miller | last post by:
Greetings. I have a question involving sorting and grouping output using XSLT. I am trying to produce a publication list from a BibTeX-like XML file: <bibxml:file...
by: Per Jørgen Vigdal | last post by:
I have a XML that I need to map. The XML goes like: <Children> <Child> <References> <External> <Reference name="filename" value="1.dat"/> <Reference name="invoicenr" value="1111111"/>...
by: Mark | last post by:
I must say that I'm quite the newb with XML/XSLT. I kind of stubmled upon using it for a report I'm trying to make. If someone would be so kind as to help out with the following I would be...
by: John Wilkinson | last post by:
Hi, I am new to XSLT. My problem is that I wish to create an HTML table, and give each row an incrementing number from 1.This would increment every itteration of a for-each loop. The XSLT...
by: Terious | last post by:
In XML ver 1.0. Here is XML I have: <CustRef> <Contract> <ContractId>1</ContractId> <SumItem> <SumItemResult> <AggSetSumItem>
by: jkmyoung | last post by:
Here's a short list of useful xslt general tricks that aren't taught at w3schools. Attribute Value Template Official W3C explanation and example This is when you want to put dynamic values...
by: gagonm | last post by:
Hi I have simple xml .Here in XSL1.0 ,I want to get count of all speciality tag where attribute isSpecialist= false. Similarily count of all speciality tag with isspecialist attribute value as...
by: Leira | last post by:
Hi, I have a problem with grouping. My source XML has <record> elements that have a @name and a @group attribute. It looks something like this: <root> <result> <record name="test1"...
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.