Reputation: 269
I have XML
code like this:
<hotels>
<hotel contact="(855) 23 430 732">
<name>hotel A</name>
<type>hotel or apartment</type>
<rating>1 to 5</rating>
<address>
<houseNo>73 </houseNo>
<street>Preah Monivong Blvd </street>
<Sangkat>Mean Chey</Sangkat >
<Khan>Daun Penh</Khan>
<city>Bangkok</city>
</address>
<room>
<roomType>hotel: standard/deluxe, apartment: studio/2-bedroom/3-bedroom</roomType>
<price>209</price>
<description>Descriptions</description>
</room>
<room>
<roomType>hotel: standard/deluxe, apartment: studio/2-bedroom/3-bedroom</roomType>
<price>210</price>
<description>Descriptions</description>
</room>
<room>
.....
</room>
</hotel>
<hotel contact="...">
... ...
... ...
</hotel>
</hotels>
Using xsl
code how I can read it? This is my code:
<?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>test xsl</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th style="text-align:left;">Name</th>
<th style="text-align:left;">Type</th>
<th style="text-align:left;">Rating</th>
<th style="text-align:left;">Address</th>
<th style="text-align:left;">Room</th>
</tr>
<xsl:for-each select="hotels/hotel">
<tr>
<!--<xsl:value-of select="."/>-->
<td><xsl:value-of select="name" /></td>
<td><xsl:value-of select="type" /></td>
<td><xsl:value-of select="rating" /></td>
<td><xsl:value-of select="address" /></td>
<td><xsl:value-of select="room" /></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
problemMy code is nothing errors but for <room>
can load only first child. Any one has experience with xsl
please help me to solve it
Upvotes: 1
Views: 933
Reputation: 116959
Change this:
<td><xsl:value-of select="room" /></td>
to:
<td>
<xsl:for-each select="room">
<xsl:text>- </xsl:text>
<xsl:value-of select="roomType" />
<xsl:text> Price: </xsl:text>
<xsl:value-of select="price" />
<xsl:text> Description: </xsl:text>
<xsl:value-of select="description" />
<br/>
</xsl:for-each>
</td>
Personally, I would prefer to do something like:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/hotels">
<html>
<body>
<h2>test xsl</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Name</th>
<th>Type</th>
<th>Rating</th>
<th>Address</th>
<th>Room Type</th>
<th>Room Price</th>
<th>Description</th>
</tr>
<xsl:apply-templates select="hotel"/>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="hotel">
<xsl:variable name="rowspan" select="count(room) + 1" />
<tr>
<td rowspan="{$rowspan}"><xsl:value-of select="name"/></td>
<td rowspan="{$rowspan}"><xsl:value-of select="type"/></td>
<td rowspan="{$rowspan}"><xsl:value-of select="rating"/></td>
<td rowspan="{$rowspan}"><xsl:value-of select="address"/></td>
</tr>
<xsl:apply-templates select="room"/>
</xsl:template>
<xsl:template match="room">
<tr>
<td><xsl:value-of select="roomType"/></td>
<td><xsl:value-of select="price"/></td>
<td><xsl:value-of select="description"/></td>
</tr>
</xsl:template>
</xsl:stylesheet>
to get:
Upvotes: 1