2012-02-20 162 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"/>

相关问题