2012-02-20 354 views
0

我在xml上應用xsl時遇到了問題。 xml對酒店,房間響應和日常使用具有相同的名稱「項目」。我如何解決這個問題?將xsl應用於多個具有相同名稱的元素?

這裏是XML請求,

<availableHotels enc:itemType="ns1:hotel" enc:arraySize="7" xsi:type="ns1:hotelArray"> 
    <item xsi:type="ns1:hotel"> 
     <processId xsi:type="xsd:string">HZ-51743575</processId> 
     <hotelCode xsi:type="xsd:string">INHEYT</hotelCode> 
     <availabilityStatus xsi:type="xsd:string">InstantConfirmation</availabilityStatus> 
     <totalPrice xsi:type="xsd:float">275</totalPrice> 
     <totalTax xsi:type="xsd:float">0</totalTax> 
     <currency xsi:type="xsd:string">USD</currency> 
     <boardType xsi:type="xsd:string">Room and Breakfast (Buffet)</boardType> 
     <rooms enc:itemType="ns1:roomResponse" enc:arraySize="1" xsi:type="ns1:roomResponseArray"> 
      <item xsi:type="ns1:roomResponse"> 
       <roomCategory xsi:type="xsd:string">Standard Twin Room</roomCategory> 
       <paxes enc:itemType="ns1:pax" enc:arraySize="2" xsi:type="ns1:paxesArray"> 
        <item xsi:type="ns1:pax"> 
         <paxType xsi:type="xsd:string">Adult</paxType> 
         <age xsi:type="xsd:integer">30</age> 
        </item> 
        <item xsi:type="ns1:pax"> 
         <paxType xsi:type="xsd:string">Child</paxType> 
         <age xsi:type="xsd:integer">5</age> 
        </item> 
       </paxes> 
       <totalRoomRate xsi:type="xsd:float">275</totalRoomRate> 
       <ratesPerNight enc:itemType="ns1:dailyRate" enc:arraySize="2" xsi:type="ns1:dailyRateArray"> 
        <item xsi:type="ns1:dailyRate"> 
         <date xsi:type="xsd:date">2012-02-25</date> 
         <amount xsi:type="xsd:float">138</amount> 
        </item> 
        <item xsi:type="ns1:dailyRate"> 
         <date xsi:type="xsd:date">2012-02-26</date> 
         <amount xsi:type="xsd:float">137</amount> 
        </item> 
       </ratesPerNight> 
      </item> 
     </rooms> 
    </item> 
</availableHotels> 

XSL我都用過,

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="/"> 
     <Property> 
      <xsl:apply-templates select="//availableHotels/item"/>1 
     </Property> 
    </xsl:template> 
    <xsl:template match="item"> 
     <Rooms> 
      <Room> 
       <Rate> 
        <Nights> 
         <xsl:apply-templates select="ratesPerNight"/> 
        </Nights> 
       </Rate> 
      </Room> 
     </Rooms> 
    </xsl:template> 
    <xsl:template match="ratesPerNight"> 
     <Night> 
      ???????? 
     </Night> 
    </xsl:template> 
</xsl:stylesheet> 

預期的O/P:

<Property> 
    <Rooms> 
     <Room> 
      <Rate> 
       <Nights> 
        <Night Amount="6825.00" BookedDate="2012-02-25"/> 
            <Night Amount="6825.00" BookedDate="2012-02-26"/> 
       </Nights> 
      </Rate> 
     </Room> 
    </Rooms> 
</Property> 

請幫我找出解決辦法。

回答

0

它可以在模板指定的屬性:

<xsl:template match="item[@xsi:type='ns1:dailyRate']"> 
</xsl:template> 

這將只匹配的項目...希望它有助於

事實上,離開了「XSI:」 這裏是更多的代碼讓你開始吧

<xsl:output method="xml" encoding="utf-8" indent="no"/> 
     <xsl:template match="/availableHotels"> 
     <Property> 
      <xsl:apply-templates select="item/rooms"/> 
     </Property> 
    </xsl:template> 

    <xsl:template match="rooms"> 
     <Room> 
      <xsl:apply-templates select="item"/> 
     </Room> 
    <xsl:value-of select="@type"/> 
    </xsl:template> 

    <xsl:template match="item[@type='ns1:roomResponse']"> 
    <xsl:value-of select="totalRoomRate"/> 
    <xsl:value-of select="@type"/> 
    </xsl:template> 



    <xsl:template match="item"> 
    <xsl:value-of select="@type"/> 
    </xsl:template>            

    <xsl:template match="*"> 
    <xsl:value-of select="name()"/> 
    </xsl:template> 

最後一個模板對捕獲錯過的模板非常有用。

+0

這是行不通的,我無法進入ratesPerNight。 – Sujit 2012-02-20 10:37:05

0

您不能進入ratesPerNight,因爲它不是項目的直接子項。在您的XSL中,您可以將其與<xsl:apply-templates select="ratesPerNight"/>內部項目相匹配。 您需要將顯式路徑設置爲<xsl:apply-templates select="rooms/item/ratesPerNight"/>,或者如果您希望在不修復路徑的情況下查找所有ratesPerNight元素,則可以使用<xsl:apply-templates select="descendant::ratesPerNight"/>

相關問題