'how to use xsl to retrieve all the district because the xml structure have some problem but i dont want to change?
How do I select all districts from the xml file? When I read the district it returns the first row only?
Expected output:
XML Input:
<?xml version="1.0"?>
<!DOCTYPE states SYSTEM "states.dtd">
<states>
<state sID="1">
<statename>Perak</statename>
<disctrict>Setiawan</disctrict>
<disctrict>Ipoh</disctrict>
<disctrict>Kampar</disctrict>
</state>
<state sID="2">
<statename>Selangor</statename>
<disctrict>Petaling</disctrict>
<disctrict>Setapak</disctrict>
<disctrict>Cheras</disctrict>
<disctrict>Rawang</disctrict>
</state>
<state sID="3">
<statename>Penang</statename>
<disctrict>Balik Pulau</disctrict>
<disctrict>Bayan Lepas</disctrict>
</state>
</states>
Below is the xsl file:
<?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>Kawan Food Company Delivery States</h2>
<table border="10" width="20%" bgcolor="blue">
<tr bgcolor="yellow" >
<th>---States---</th>
</tr>
<xsl:for-each select="states/state">
<tr>
<td><xsl:value-of select="disctrict"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Solution 1:[1]
i want a separate td for each district
The result you show (and which you should have posted as code instead of as link to a picture) does NOT have a separate td for each district. It puts all the districts in the same td, separated only by a line break.
To get such result, you could do simply:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/states">
<html>
<body>
<h2>Kawan Food Company Delivery States</h2>
<table border="1">
<!-- head -->
<tr>
<th>State</th>
<th>Districts</th>
</tr>
<!-- body -->
<xsl:for-each select="state">
<tr>
<td>
<xsl:value-of select="statename"/>
</td>
<td>
<xsl:for-each select="disctrict">
<xsl:value-of select="."/>
<br/>
</xsl:for-each>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Alternatively, you could create a separate tr for each district:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/states">
<html>
<body>
<h2>Kawan Food Company Delivery States</h2>
<table border="1">
<!-- head -->
<tr>
<th>State</th>
<th>Districts</th>
</tr>
<!-- body -->
<xsl:for-each select="state">
<tr>
<td rowspan="{count(disctrict)}">
<xsl:value-of select="statename"/>
</td>
<td>
<xsl:value-of select="disctrict[1]"/>
</td>
</tr>
<xsl:for-each select="disctrict[position() > 1]">
<tr>
<td>
<xsl:value-of select="."/>
</td>
</tr>
</xsl:for-each>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
The result here will look something like:
Add your own CSS to taste.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |


