大型xml由akey排序

时间:2013-07-08 19:47:52

标签: php xml xml-parsing

如何对这样的xml进行排序我们想用'BestAmount'元素对它进行排序,这是一个非常大的xml约900条记录的一部分,请告诉我们如何快速排序。通过soap请求检索xml

    <?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Body>
      <HotelSearchResult xmlns="http://www.sson.com/XSD">
         <HotelAvailRS>
            <Success />
            <ResponseData>
               <CheckIn>2013-08-22T00:40:44</CheckIn>
               <CheckOut>2013-08-23T00:40:44</CheckOut>
               <ISOCurrency>USD</ISOCurrency>
               <LocationCode>SG9</LocationCode>
               <HotelAvails>
                  <Hotel>
                     <Status>Available</Status>
                     <Code>WSMA0511000140</Code>
                     <Name>Fragrance Crystal</Name>
                     <Rating>2</Rating>
                     <Description>An affordable but comfortable option in the busy Geylang area, the Fragrance Hotel Crystal offers outstanding monetary value for both tourists and bus</Description>
                     <Image>http://dxhapi.travelcarma.com/ImgHandler.ashx?p=gc.uvzt.genirypnezn.pbz/GY/JFZN0511000140.wct</Image>
                     <Address>50</Address>
                     <City>Singapore City</City>
                     <Country>Singapore</Country>
                     <Phone>(65) 6844 - 7888</Phone>
                     <BestAmount>50.00</BestAmount>
                     <HotelToken>E4+WDyPLOLGEQ2A4rl5MSyN9PvgddSt25KxIJ3DRg78=</HotelToken>
                  </Hotel>
                  <Hotel>
                     <Status>Available</Status>
                     <Code>WSMA0511000142</Code>
                     <Name>Fragrance Pearl</Name>
                     <Rating>2</Rating>
                     <Description>Situated in an exciting part of Singapore that provides a glimpse into the local flavour of the island, this property is an ideal place for those who</Description>
                     <Image>http://dxhapi.travelcarma.com/ImgHandler.ashx?p=gc.uvzt.genirypnezn.pbz/GY/JFZN0511000142.wct</Image>
                     <Address>21 Lorong</Address>
                     <City>Singapore City</City>
                     <Country>Singapore</Country>
                     <Phone>(65) 6842 - 7888</Phone>
                     <BestAmount>50.00</BestAmount>
                     <HotelToken>E4+WDyPLOLGEQ2A4rl5MSyN9PvgddSt25KxIJ3DRg78=</HotelToken>
                  </Hotel>
                  <Hotel>
                     <Status>Available</Status>
                     <Code>WSMA0910100087</Code>
                     <Name>Fragrance Sunflower</Name>
                     <Rating>2</Rating>
                     <Description>Fragrance Chain of Hotels is one of the leading chain of tourist class hotels in Singapore, founded in 1996, it started its debut in hotel management</Description>
                     <Image>http://dxhapi.travelcarma.com/ImgHandler.ashx?p=gc.uvzt.genirypnezn.pbz/GY/JFZN0910100087.wct</Image>
                     <Address>10 Lorong 10 Geylang</Address>
                     <City>Singapore City</City>
                     <Country>Singapore</Country>
                     <Phone>(65) 6846 - 7888</Phone>
                     <BestAmount>50.00</BestAmount>
                     <HotelToken>E4+WDyPLOLGEQ2A4rl5MSyN9PvgddSt25KxIJ3DRg78=</HotelToken>
                  </Hotel>
                  <Hotel>
                     <Status>Available</Status>
                     <Code>WSMA0511000141</Code>
                     <Name>Fragrance Emerald</Name>
                     <Rating>2</Rating>
                     <Description>Catering to international budget travellers, the hotel is designed to uplift your spirit with its Asian inspired decor and enables you to discover its</Description>
                     <Image>http://dxhapi.travelcarma.com/ImgHandler.ashx?p=gc.uvzt.genirypnezn.pbz/GY/JFZN0511000141.wct</Image>
                     <Address>20 Lorong 6 Geylang</Address>
                     <City>Singapore City</City>
                     <Country>Singapore</Country>
                     <Phone>(65) 6842 - 3888</Phone>
                     <BestAmount>54.00</BestAmount>
                     <HotelToken>E4+WDyPLOLGEQ2A4rl5MSyN9PvgddSt25KxIJ3DRg78=</HotelToken>
                  </Hotel>
                  <Hotel>
                     <Status>Available</Status>
                     <Code>WSMA0511000093</Code>
                     <Name>Fragrance Joo Chiat</Name>
                     <Rating>2</Rating>
                     <Description>The Fragrance Hotel has been specially designed for convenience and comfort and boasts a great location in the Malay-heritage Joo Chiat area and close</Description>
                     <Image>http://dxhapi.travelcarma.com/ImgHandler.ashx?p=gc.uvzt.genirypnezn.pbz/GY/JFZN0511000093.wct</Image>
                     <Address>219</Address>
                     <City>Singapore City</City>
                     <Country>Singapore</Country>
                     <Phone>(65) 6344 - 9888</Phone>
                     <BestAmount>54.00</BestAmount>
                     <HotelToken>E4+WDyPLOLGEQ2A4rl5MSyN9PvgddSt25KxIJ3DRg78=</HotelToken>
                  </Hotel>
                  <Hotel>
                     <Status>Available</Status>
                     <Code>WSMA0511000166</Code>
                     <Name>Fragrance Ruby</Name>
                     <Rating>2</Rating>
                     <Description>Offering an atmosphere of warmth and comfort to the weary travellers, this property is a second home, while you are on a holiday in the bustle of the</Description>
                     <Image>http://dxhapi.travelcarma.com/ImgHandler.ashx?p=gc.uvzt.genirypnezn.pbz/GY/JFZN0511000166.wct</Image>
                     <Address>10</Address>
                     <City>Singapore City</City>
                     <Country>Singapore</Country>
                     <Phone>(65) 6841 - 3888</Phone>
                     <BestAmount>54.00</BestAmount>
                     <HotelToken>E4+WDyPLOLGEQ2A4rl5MSyN9PvgddSt25KxIJ3DRg78=</HotelToken>
                  </Hotel>
                  <Hotel>
                     <Status>Available</Status>
                     <Code>WSMA0511000167</Code>
                     <Name>Fragrance Sapphire</Name>
                     <Rating>2</Rating>
                     <Description>Set against the historical hub of Geylang, Fragrance Hotel Sapphire is a unique harmony of history, tradition and modernity. Fragrance Hotel Sapphire</Description>
                     <Image>http://dxhapi.travelcarma.com/ImgHandler.ashx?p=gc.uvzt.genirypnezn.pbz/GY/JFZN0511000167.wct</Image>
                     <Address>3</Address>
                     <City>Singapore City</City>
                     <Country>Singapore</Country>
                     <Phone>(65) 6841 - 4888</Phone>
                     <BestAmount>54.00</BestAmount>
                     <HotelToken>E4+WDyPLOLGEQ2A4rl5MSyN9PvgddSt25KxIJ3DRg78=</HotelToken>
                  </Hotel>

1 个答案:

答案 0 :(得分:1)

具有2条规则的XSLT样式表可以完成工作:

<xsl:template match="*">
  <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>

<xsl:template match="h:HotelAvails" xmlns:h="http://www.sson.com/XSD">
  <xsl:copy>
    <xsl:apply-templates select="h:Hotel">
      <xsl:sort select="h:BestAmount" data-type="number"/>
    </xsl:apply-templates>
  </xsl:copy> 
</xsl:template> 

这将在XSLT 1.0或2.0下运行。