检查Previous节点Value的值并将当前值增加1

时间:2013-07-29 11:05:34

标签: xml xslt xpath xhtml

我需要检查特定酒店中每个可用房间的shrui值,如果它与之前的房间相等,则noofroom值必须为2.其他明智的值仅为1。我试过它没用。以下是我的代码。任何人都可以帮助我。

  <xsl:for-each select="hm:AvailableRoom ">
            <availableroom>           
                <hotelcode>
                  <xsl:value-of select="preceding-sibling::hm:HotelInfo/hm:Code"/>
                </hotelcode>             
              <xsl:for-each select="hm:HotelOccupancy ">
                <roomcount>
                  <xsl:value-of select="hm:RoomCount"/>
                </roomcount>
                <xsl:for-each select="hm:Occupancy ">
                  <guests>
                    <xsl:value-of select="sum((hm:AdultCount | hm:ChildCount)[number(.) = .])"/>
                  </guests>
                  <adults>
                    <xsl:value-of select="hm:AdultCount"/>
                  </adults>
                  <children>
                    <xsl:value-of select="hm:ChildCount"/>
                  </children>
                </xsl:for-each>
              </xsl:for-each>
              <xsl:for-each select="hm:HotelRoom ">
                <shrui>
                  <xsl:value-of select="@SHRUI"/>
                </shrui>
                <xsl:choose>
                  <xsl:when test="preceding-sibling::hm:HotelRoom[1]/@SHRUI=@SHRUI">
                    <noofroom>2</noofroom>                    
                  </xsl:when>
                  <xsl:otherwise>
                    <noofroom>1</noofroom>
                  </xsl:otherwise>
                </xsl:choose>
                <board>
                  <xsl:value-of select="hm:Board"/>
                </board>
                <roomtype>
                  <xsl:value-of select="hm:RoomType"/>
                </roomtype>
                <roomcode>
                  <xsl:value-of select="hm:RoomType/@code"/>
                </roomcode>
                <boardcode>
                  <xsl:value-of select="hm:Board/@code"/>
                </boardcode>
                <xsl:for-each select="hm:Price ">
                  <amount>
                    <xsl:value-of select="hm:Amount"/>
                  </amount>
                </xsl:for-each>
              </xsl:for-each>
            </availableroom>
          </xsl:for-each>

我的XML

<AvailableRoom>
        <HotelOccupancy>
          <RoomCount>1</RoomCount>
          <Occupancy>
            <AdultCount>1</AdultCount>
            <ChildCount>0</ChildCount>
          </Occupancy>
        </HotelOccupancy>
        <HotelRoom SHRUI="H6xFv7ejhSWcGuc+BCBYmg==" availCount="2" onRequest="N">
          <Board type="SIMPLE" code="FB-E10" shortname="FB">FULL BOARD</Board>
          <RoomType type="SIMPLE" code="SGL-E10" characteristic="ST">SINGLE STANDARD</RoomType>
          <Price>
            <Amount>269.580</Amount>
          </Price>
        </HotelRoom>
      </AvailableRoom>
      <AvailableRoom>
        <HotelOccupancy>
          <RoomCount>1</RoomCount>
          <Occupancy>
            <AdultCount>1</AdultCount>
            <ChildCount>0</ChildCount>
          </Occupancy>
        </HotelOccupancy>
        <HotelRoom SHRUI="H6xFv7ejhSWcGuc+BCBYmg==" availCount="2" onRequest="N">
          <Board type="SIMPLE" code="FB-E10" shortname="FB">FULL BOARD</Board>
          <RoomType type="SIMPLE" code="SGL-E10" characteristic="ST">SINGLE STANDARD</RoomType>
          <Price>
            <Amount>269.580</Amount>
          </Price>
        </HotelRoom>
      </AvailableRoom>

1 个答案:

答案 0 :(得分:0)

在您的示例中,HotelRoom元素不是兄弟姐妹,我想您想要更改

<xsl:when test="preceding-sibling::hm:HotelRoom[1]/@SHRUI=@SHRUI">

<xsl:when test="parent::hm:AvailableRoom/preceding-sibling::hm:AvailableRoom[1]/hm:HotelRoom/@SHRUI=@SHRUI">

因为HotelRoom元素是AvailableRoom元素的子元素。