Reputation: 155
So recently I've been trying to extract the simplest data possible however XSL keeps selecting the first record all the time.
Now I have tested the select="//student", it successfully selects all the data but when it comes to displaying it in the table, it messes up I think
XML
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="stylesheet.xsl"?>
<school>
<class unitId="3311">
<className>English</className>
<studentList>
<student id="1001">Lisa Simpson</student>
<student id="1002">Barney Rubble</student>
<student id="1003">Donald Duck</student>
</studentList>
</class>
</school>
XSL
<?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>My Students</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th style="text-align:left">STUDENT</th>
</tr>
<xsl:for-each select="//student">
<tr>
<td><xsl:value-of select="../student"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Upvotes: 1
Views: 34
Reputation: 456
You should use:
<xsl:value-of select="." />
Also it is better to specifically select the parent node in the for-each:
<xsl:for-each select="/school/studentList/student">
[...]
</xsl:for-each>
Upvotes: 1
Reputation: 5432
The xpath, ../student
, is the problem. For every student
in the document(//student
) you are going to its parent and selecting the children student
s. Using xsl:value-of
you will get only the first value as XSLT1.0 selects only the first element's value.
Change it to
<xsl:value-of select="."/>
so that it selects the current value for every student
.
Upvotes: 0