将xsl应用于具有相同名称的多个元素?

时间:2012-02-20 10:15:58

标签: xml xslt

我在xml上应用xsl时遇到问题。 xml具有相同的名称“item”用于酒店,roomresponse和dailyrate。我该如何解决这个问题?

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

请帮我找出解决方案。

2 个答案:

答案 0 :(得分: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> 

最后一个模板对于捕获您错过的模板非常有用。

答案 1 :(得分:0)

你不能进入ratesPerNight,因为它不是item的直接子项。在您的XSL中,您将其与项目内的<xsl:apply-templates select="ratesPerNight"/>匹配。 您需要的是将显式路径设置为<xsl:apply-templates select="rooms/item/ratesPerNight"/>,或者如果要查找所有ratesPerNight元素而不修复可以使用<xsl:apply-templates select="descendant::ratesPerNight"/>的路径。

相关问题