Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 将xsl应用于具有相同名称的多个元素?_Xml_Xslt - Fatal编程技术网

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

Xml 将xsl应用于具有相同名称的多个元素?,xml,xslt,Xml,Xslt,在xml上应用xsl时,我遇到了一个问题。xml对于hotel、roomresponse和DailRate具有相同的名称“item”。我如何解决这个问题 这是xml请求 <availableHotels enc:itemType="ns1:hotel" enc:arraySize="7" xsi:type="ns1:hotelArray"> <item xsi:type="ns1:hotel"> <processId xsi:type="xs

在xml上应用xsl时,我遇到了一个问题。xml对于hotel、roomresponse和DailRate具有相同的名称“item”。我如何解决这个问题

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

HZ-51743575
固有
瞬间确认
275
0
美元
客房和早餐(自助餐)
标准双人房
成人
30
小孩
5.
275
2012-02-25
138
2012-02-26
137
我用过的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>

1.
????????
预期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>


请帮助我找到解决方案。

可以在模板上指定属性:

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


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

您不能进入ratesPerNight,因为它不是项目的直接子项。在XSL中,您可以将其与项内的
匹配。 您需要的是将显式路径作为
放在那里,或者如果您想查找所有ratesPerNight元素而不修复路径,您可以使用