XSLT XPATH 学习笔记

本文介绍了一段XSLT代码,该代码用于从给定的XML数据中筛选出属性Avail为true的酒店节点,并收集这些酒店中的单人间、双人间和双床间的信息。通过使用XSLT的特性,如key、generate-id和模板匹配等功能,实现了对酒店及其房间类型的高效查询。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


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>





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值