简体   繁体   中英

Is there any way to apply a count() depending on the current value in a for-each?

I've got an xml file with this structure


<?xml version='1.0' encoding='UTF-8'?>
<students>
  <student>
    <number>1</number>
    <firstname>Sandrine</firstname>
    <name>BERTIN</name>
    <gender>F</gender>
    <highschool>de la Côte d'Albâtre</highschool>
    <city>Saint-Valéry-en-Caux</city>
    <specialization>Physique chimie</specialization>
  </eleve>

[...]
</students>

The end result should be a pdf document, each highschool will be displayed on a new page along with other informations ( list of all the student belonging to this highschool etc...) One of those informations would be the total number of student where gender ='M' ( or 'F').

This is a part of my code:

  <fo:page-sequence master-reference="book">
    <fo:flow flow-name="xsl-region-body"> 
     <xsl:for-each select="students/student[count(. | key('cityKey',city)[1]) = 1]">
      <xsl:sort select="city" />
             <xsl:for-each select="key('cityKey', city)[count(. | key('highschoolKey',highschool)[1]) = 1]">
              <xsl:sort select="highschool" /> 
               <fo:block break-before="page"><xsl:value-of  select="highschool"/> </fo:block>
               <fo:block> City : <xsl:value-of select="city" /> </fo:block>

              <!-- <fo:block> Number of male students : <xsl:value-of select="count(/students/student[gender='M'])" /> </fo:block> -->



          </xsl:for-each>
        </xsl:for-each>
    </fo:flow>
  </fo:page-sequence>

I'd like to get the count () number of male students and print it for each 'highschool' (so different number on each pages/highschool of my document) in my xsl:for-each. But i either get all the male students from all highschools (/students/student[gender='M']) or nothing.

Ideally it would look like this:

**de la Côte d'Albâtre**

City : Saint-Valéry-en-Caux

Number of male students: " "

Number of female students: " "

List of all the student in a Table : [...]

Thanks in advance

If you are grouping student by city , then:

<xsl:value-of select="count(key('cityKey', city)[gender ='M'])"/>

should give you the count of male students in the current group.

--
Untested, because you did not provide a complete example.

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