0

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">
Bevvy
  • 15
  • 8
  • So what is your desired outcome? I checked your code and it appears to work according to your requirements... – zx485 Nov 17 '16 at 19:36
  • 2
    What ID?? I don't see any in the xml nor xsl? – Stefan Hegny Nov 17 '16 at 19:42
  • Sorry my mistake that's the way i did it without the id. The class tag had an id of "AWD" which I've just added to the original – Bevvy Nov 18 '16 at 14:45
  • zx485 my desired outcome was to get a list of students who acheived an A grade. This worked but only after removing the ID of the class tag, with that ID it doesn't function correctly. – Bevvy Nov 18 '16 at 14:51
  • @Bevvy Your problem cannot be reproduced using your code: http://xsltransform.net/bwdws4 -- Is it possible you also have a namespace declaration on your root element, something like ``? – michael.hor257k Nov 18 '16 at 15:52
  • Thanks for the answer. No i don't have anything like that in my code, this may be a stupid question but are you able to do this in sublime text? As that is where i've been doing it. Are you required to link the .xml to the .xslt file? – Bevvy Nov 18 '16 at 19:12
  • I've managed to get it working, i didn't realise that you can't just open the file in a normal browser. I had to setup my own server and run it from there. Thanks for you suggestions and help though – Bevvy Nov 18 '16 at 19:38
  • See if this helps: http://stackoverflow.com/questions/24629700/testing-xslt-code-using-your-browser/24632054#24632054 – michael.hor257k Nov 18 '16 at 21:11

0 Answers0