在经典ASP中对XML数据进行排序

时间:2009-06-14 08:16:04

标签: xml xpath asp-classic vbscript

我想在xml以下排序,基于成人和孩子(我需要将成人和孩子视为常数):

<HotelDetails>
  <hotel>
    <rooms>
      <room>
        <roomname>Single</roomname> 
        <Price>100</Price> 
        <Adult>1</Adult> 
        <child>0</child> 
      </room>
    </rooms>
    <rooms>
      <room>
        <roomname>Single</roomname> 
        <Price>150</Price> 
        <Adult>1</Adult> 
        <child>0</child> 
      </room>
    </rooms>
    <rooms>
      <room>
        <roomname>Double</roomname> 
        <Price>200</Price> 
        <Adult>2</Adult> 
        <child>1</child> 
      </room>
    </rooms>   
  </hotel>
</HotelDetails>

给予:

Hotel :   
Single-100,
Double-200,
Total 300

Single-150,
Double-200,
Total 350

我尝试使用下面的代码进行排序,但它就像常量(不同的数据)。任何人都有想法在XML之上使用类似下面的代码进行排序吗?

<%@ Language="VBScript" CodePage="65001"%>  
<%  
Response.ContentType = "text/plain; charset=UTF-8"  

Dim doc   
Set doc = Server.CreateObject("Msxml2.DOMDocument.3.0") 
doc.async = False  

If doc.load(Server.MapPath("ee.xml")) Then   
  doc.setProperty "SelectionLanguage", "XPath"

  Dim xpath
  xpath = "HotelDetails/hotel/rooms[not(room/Adult= preceding-sibling::rooms/room/Adult)]/room/Adult"

  For Each Adult in doc.selectNodes(xpath) 
    Response.Write "Hotel" & VbCrLf
    Response.Write Adult.ChildNodes.Item(0).Text & VbCrLf  
  Next
Else   
  Response.Write doc.parseError.reason   
End If 
%>  

我该怎么做?

2 个答案:

答案 0 :(得分:2)

另一种可能性是使用ADODB断开连接的记录集,并使用ADODB工具进行排序和提取。

一个好的起点(代码在VB中):

  • How To Obtain an ADO Recordset from XML

    Dim oStream As ADODB.Stream
    Set oStream = New ADODB.Stream
    
    oStream.Open
    oStream.WriteText sXML   'Give the XML string to the ADO Stream
    
    oStream.Position = 0    'Set the stream position to the start
    
    Dim oRecordset As ADODB.Recordset
    Set oRecordset = New ADODB.Recordset
    
    oRecordset.Open oStream    'Open a recordset from the stream
    
    oStream.Close
    Set oStream = Nothing
    
    Set RecordsetFromXMLString = oRecordset  'Return the recordset
    
    Set oRecordset = Nothing
    

答案 1 :(得分:0)

这会有所帮助吗? http://www.developer.com/xml/article.php/1560361 ,它展示了如何使用XSL转换来输出以HTML形式排序的XML,但我认为您可以将XSL输出修改为XML(&lt; xsl:output&gt;)

如果这些记录来自SQL Server,那么输出它们要容易得多。 你想做缓存吗?

如果您尝试进行缓存,请查看我的帖子: http://www.moretechtips.net/2008/11/object-oriented-data-caching-for.html