1. Get the nodes which the attribute Avail="true"
2. Collect the single, double, twins rooms.
<?xml version="1.0" encoding="UTF-8"?><Hotels>
<Hotel Code="Hotel1" Name="My Name 1" Avail="true">
<Description>My Description 1</Description>
<Price>80</Price>
<Rooms>
<Room Type="Double">
<Price>120</Price>
</Room>
<Room Type="Single">
<Price>80</Price>
</Room>
<Room Type="Twins">
<Price>150</Price>
</Room>
</Rooms>
</Hotel>
<Hotel Code="Hotel2" Name="My Name 2" Avail="false">
<Description>My Description 2</Description>
<Price>50</Price>
<Rooms>
<Room Type="Double">
<Price>80</Price>
</Room>
<Room Type="Single">
<Price>50</Price>
</Room>
</Rooms>
</Hotel>
<Hotel Code="Hotel3" Name="My Name 3" Avail="true">
<Description>My Description 3</Description>
<Price>180</Price>
<Rooms>
<Room Type="Double">
<Price>180</Price>
</Room>
<Room Type="Twins">
<Price>200</Price>
</Room>
</Rooms>
</Hotel>
<Hotel Code="Hotel4" Name="My Name 4" Avail="true">
<Description>My Description 4</Description>
<Price>300</Price>
<Rooms>
<Room Type="Double">
<Price>450</Price>
</Room>
<Room Type="Single">
<Price>300</Price>
</Room>
<Room Type="Twins">
<Price>480</Price>
</Room>
</Rooms>
</Hotel>
<Hotel Code="Hotel5" Name="My Name 5" Avail="false">
<Description>My Description 1</Description>
<Price>150</Price>
<Rooms>
<Room Type="Double">
<Price>150</Price>
</Room>
<Room Type="Twins">
<Price>180</Price>
</Room>
</Rooms>
</Hotel>
<Hotel Code="Hotel6" Name="My Name 6" Avail="true">
<Description>My Description 6</Description>
<Price>100</Price>
<Rooms>
<Room Type="Single">
<Price>100</Price>
</Room>
<Room Type="Twins">
<Price>180</Price>
</Room>
</Rooms>
</Hotel>
</Hotels>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!--以XML形式输入-->
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<DisplayHotels>
<!--引用模板 在Hotels结点上应用-->
<xsl:apply-templates select="Hotels" mode="collectRooms"/>
</DisplayHotels>
</xsl:template>
<!--创建一个索引,用于快速检索,可以重复-->
<xsl:key name="roomType" match="Hotel/Rooms/Room" use="@Type"/>
<xsl:template match="Hotels" mode="collectRooms">
<!--去重,引用generate-id函数进行比较,默认函数的参数是当前节点,如果传入node-set,默认取第一条记录,XPATH []中的是判断条件-->
<xsl:for-each select="Hotel/Rooms/Room[generate-id() = generate-id(key('roomType', @Type))]">
<DisplayRooms Type="{@Type}">
<xsl:for-each select="key('roomType',@Type)">
<xsl:variable name="index" select="position()"/>
<xsl:variable name="avail" select="../../@Avail"/>
<xsl:if test="$avail = 'true'">
<xsl:if test="$index mod 2 > 0">
<DisplayRoom RowType="odd-row" index="{$index}">
<Name>
<!--可支持向上寻找-->
<xsl:value-of select="../../@Name"/>
</Name>
<Price>
<xsl:value-of select="Price"/>
</Price>
<Description>
<xsl:value-of select="../../Description"/>
</Description>
</DisplayRoom>
</xsl:if>
</xsl:if>
</xsl:for-each>
</DisplayRooms>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>