2012-02-20 11 views
0

xmlをxmlに適用しているときに問題が発生しています。 xmlはホテル、roomresponseとdailyrateのために同じ名前 "項目"を持っています。どうすればこの問題を解決できますか?解決策を見つけるために私を助けてください同じ名前の複数の要素にxslを適用しますか?

<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> 

:ここ

は、私が使用している

<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、P/oを期待

<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> 

、XML要求です。

答えて

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

0

商品の直属の子供ではないため、ratesPerNightに入ることはできません。あなたのXSLでそれを項目内の<xsl:apply-templates select="ratesPerNight"/>とマッチさせます。 明示的なパスを<xsl:apply-templates select="rooms/item/ratesPerNight"/>とするか、パスを固定せずにすべてのratesPerNight要素を検索する場合は、<xsl:apply-templates select="descendant::ratesPerNight"/>を使用する必要があります。