Forum Moderators: open
I just started using XLST like today. I have a sort that is working but I am also attempting to get the roomTypeDescription (xml node) to also loop (directly below). This does not have to be sorted.
To simplify the problem I have to have several for-each select('s)because several xml nodes have to be displayed. I have only been able to have one node display but if attempt two nodes of them one does not want to loop for some reason. Maybe my problem is with the html table? ...
Thanks
--------------------
<xsl:for-each select="/HotelRoomAvailabilityResults/HotelRoomAvailabilityResult/roomTypeDescription"/>
--------------------
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/">
<html>
<body>
<table border="1">
<tr bgcolor="#9acd32">
<th>Room Rate</th>
<th>Details</th>
</tr>
<xsl:for-each select="HotelRoomAvailabilityResults/HotelRoomAvailabilityResult/RateInfo">
<xsl:sort select="displayRoomRate" data-type="number"/>
<tr>
<td><xsl:value-of select="displayRoomRate"/></td>
<td><xsl:value-of select="/HotelRoomAvailabilityResults/HotelRoomAvailabilityResult/roomTypeDescription"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template></xsl:stylesheet>
---------------------------------
XML example(below)
<HotelRoomAvailabilityResults size='3'>
<HotelRoomAvailabilityResult>
<hotelId>117086</hotelId>
<cancellationPolicy>Cancel by 4PM the day of arrival</cancellationPolicy>
<policy/>
<rateCode>RAC</rateCode>
<roomTypeCode>C1D</roomTypeCode>
<hrnQuoteKey/>
<rateDescription>Standard Rate</rateDescription>
<roomTypeDescription>STUDIO APARTMENT W/ 1 DOUB - Highest rate change for stay is 120</roomTypeDescription>
<supplierType>S</supplierType>
<propertyType>H</propertyType>
<taxRate>12 PCT 3 PCT</taxRate>
<otherInformation/>
<rateChange>true</rateChange>
<guaranteeRequired>true</guaranteeRequired>
<depositRequired>false</depositRequired>
<immediateChargeRequired>false</immediateChargeRequired>
<currentAllotment>-1</currentAllotment>
<propertyId>014674</propertyId>
<promoDescription/>
<promoType/>
<promoDetailText/>
<bedTypes/>
<RateInfo>
<displayCurrencyCode>USD</displayCurrencyCode>
<displayRoomRate>94.81</displayRoomRate>
<nativeCurrencyCode>CAD</nativeCurrencyCode>
<nativeRoomRate>120.0</nativeRoomRate>
<rateFrequency>D</rateFrequency>
</RateInfo>
</HotelRoomAvailabilityResult>
I think
<td><xsl:value-of select="/HotelRoomAvailabilityResults/HotelRoomAvailabilityResult/roomTypeDescription"/></td>
means it will select the first roomTypeDescription each time.
try
<td><xsl:value-of select="../roomTypeDescription"/></td>
-Ana
[edited by: engine at 11:41 am (utc) on May 25, 2005]
[edit reason] No urls, thanks. See TOS [webmasterworld.com] [/edit]
You are looping over
HotelRoomAvailabilityResults/HotelRoomAvailabilityResult/RateInfo
Do you expect multiple RateInfor, or multiple HotelRoomAvailabilityResult?
If it is the latter, the for each should be
<xsl:for-each select="HotelRoomAvailabilityResults/HotelRoomAvailabilityResult">
...
-Ana
[edited by: engine at 11:40 am (utc) on May 25, 2005]
[edit reason] No urls, thanks. See TOS [webmasterworld.com] [/edit]
Can you explain why the double dot notation (../roomTypeDescription) solved the problem.It is a matter of navigating the node tree, i.e. your XPath syntax here.
Once the <xsl:for-each> begins, the current node becomes <RateInfo>. If you select ../roomTypeDescription, you're telling the processor to work on "a <roomTypeDescription> which is a sibling of the current <RateInfo>."
But in your original template, you were telling the processor to display "/HotelRoomAvailabilityResults/HotelRoomAvailabilityResult/roomTypeDescription" which is to say "I don't care what the current node is, go to the very top of the document tree and display the contents of the first <roomTypeDescription> inside the first <HotelRoomAvailabilityResult> inside the first <HotelRoomAvailablityResults> you find."
You don't need for-each
I've been doing XSL for 5 years and I can count the number of times I used
for-eachon one hand. Sometimes I'll use it becuaes I'm lazy but I know I'm being lazy. Every legitamate use of
for-eachis a highly complex problem that you are not likely to run into until you've done XSL for a year or two (if ever).
If you use
for-each, you'll get yourself into a pattern that you'll have a hard time getting out of.
You want to look at using multple templates, modes and [b]
apply-templates[\b]. This is the correct approach to doing XSLT.
Full stop.
I would strong suggest getting your hands on the XSLT Bible (AKA the XSLT Programmer's Reference by Michael Kay published by WROX). It'll help you in more ways that you can imagine.