Home > Software design >  XSLT: How to not include results with a specific element in my table?
XSLT: How to not include results with a specific element in my table?

Time:05-24

I am trying to remove any result that has the <absent> element in my table.

XML:

 <classTest>
  <testDetails>
     <description>Class Test</description>
     <totalMarks>40</totalMarks>
  </testDetails>
  <results>
    <student num="1008"><result>1</result><absent/></student>
    <student num="1009"><result>23</result></student>
    <student num="1007"><result>3</result></student>
    <student num="1028"><result>0</result><absent/></student>
    ...
  </results>    
</classTest>

XSL:

<xsl:template match="classTest">
  <html>
  <body>
  <table border="1">
    <tr>
      <td><xsl:text>Total number of students</xsl:text></td>
      <td><xsl:value-of select="count(/classTest/results/student)"/></td>
    </tr>
    <tr>
      <td><xsl:text>Total number of students absent</xsl:text></td>
      <td><xsl:value-of select="count(/classTest/results/student/absent)"/></td>
    </tr>
    <tr>
      <th style="width: 300px; text-align:center">Student number</th>
      <th style="width: 150px; text-align:center">Mark</th>
    </tr>
    <xsl:for-each select="results/student">        
      <xsl:sort select="@num" order="descending"/>        
      <tr>
        <td><xsl:value-of select="@num"/></td>        
        <xsl:choose>
          <xsl:when test="result &lt; 20">
            <td style="color:rgb(255, 0, 0);"><xsl:number value="result"/></td>
          </xsl:when>  
          <xsl:otherwise>
            <td><xsl:number value="result"/></td>
          </xsl:otherwise>
        </xsl:choose>
      </tr>
    </xsl:for-each>
  </table>
  </body>
  </html>
</xsl:template>

I tried using not contains check, when and if statements but I cant seem to remove any result with a student that is absent. I need to have the <absent> element in my table as I needed to have a total number of absent students as a SUM but after I add them together i dont need to have them included in my table. What do i need to do to fix this?

It is supposed to look like this after I am done: Result

CodePudding user response:

Instead of:

<xsl:for-each select="results/student">      

try:

<xsl:for-each select="results/student[not(absent)]">      
  • Related