I've been given a task to complete which involves filtering the names based on their grade, but the tag ID keeps throwing me off. I can get the desired outcome if I remove the ID in question, but I assume it has been put there for a reason.
My XML is a list of student details with the file extension .xml
:
<?xml version="1.0" encoding="UTF-8"?>
<CLASS ID = "AWD">
<STUDENT>
<NAME>Tom</NAME>
<AGE>19</AGE>
<HEIGHT>1.3</HEIGHT>
<SEX>M</SEX>
<GRADE>B</GRADE>
</STUDENT>
<STUDENT>
<NAME>Dick</NAME>
<AGE>29</AGE>
<HEIGHT>1.1</HEIGHT>
<SEX>M</SEX>
<GRADE>A</GRADE>
</STUDENT>
<STUDENT>
<NAME>Harry</NAME>
<AGE>39</AGE>
<HEIGHT>1.5</HEIGHT>
<SEX>M</SEX>
<GRADE>C</GRADE>
</STUDENT>
<STUDENT>
<NAME>Mary</NAME>
<AGE>30</AGE>
<HEIGHT>1.1</HEIGHT>
<SEX>F</SEX>
<GRADE>A</GRADE>
</STUDENT>
<STUDENT>
<NAME>Tim</NAME>
<AGE>23</AGE>
<HEIGHT>1.5</HEIGHT>
<SEX>M</SEX>
<GRADE>A</GRADE>
</STUDENT>
</CLASS>
My XSLT code (.xslt
file extension) is:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Student List
</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th style="text-align:left">Name
</th>
<th style="text-align:left">Age
</th>
</tr>
<xsl:for-each select="CLASS/STUDENT">
<tr>
<xsl:if test="GRADE = 'A'">
<td>
<xsl:value-of select="NAME"/>
</td>
<td>
<xsl:value-of select="AGE"/>
</td>
</xsl:if>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
I think it is likely that the problem is on this line, but I've tried to include the ID in the code and that doesn't solve the issue.
<xsl:for-each select="CLASS/STUDENT">