2

I want to sort below xml, Based on the Adult and child ( i need to take Adult and child as constant):

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

to give:

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

Single-150,
Double-200,
Total 350

I try to sort with below code, but it comes like constant (distinct data). Anyone have an idea to sort above XML use something like below code?

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

How can I do this?

Tomalak
  • 332,285
  • 67
  • 532
  • 628
Alex
  • 1,933
  • 9
  • 36
  • 40
  • You do not seem to attempt any sorting, but you do seem to try to *group* things in some way. What should happen if another "Double" room with a different price is added? – Tomalak Jun 14 '09 at 09:48
  • (And why is there never more than one in each element?) – Tomalak Jun 14 '09 at 09:49
  • I provide only a sample asp code, what i need.. I want to sort the room types with the adult and child details.. there is any possible way to sort with out xsl... or There is any possiblity to sort with Xquery in SQL Server? – Alex Jun 14 '09 at 15:05
  • Care to answer any of the questions I asked you? – Tomalak Jun 14 '09 at 17:53
  • This question makes no sense. Its unclear how the displayed output could be arrived at? – AnthonyWJones Jun 14 '09 at 19:10

2 Answers2

2

Another possibility is to use an ADODB Disconnected Recordset, and use the ADODB tools for sorting and extracting.

A good place to start (code is in 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
    
sirdank
  • 3,351
  • 3
  • 25
  • 58
bugmagnet
  • 7,631
  • 8
  • 69
  • 131
0

Would this be helpful http://www.developer.com/xml/article.php/1560361 , It shows how to use XSL transformation to output XML sorted in the form of HTML, But I think you can modify XSL output to be XML (<xsl:output>)

If these records coming from SQL Server, It is much easier to output them sorted already.. Are you trying to do caching??

If you trying to do caching , check my post: http://www.moretechtips.net/2008/11/object-oriented-data-caching-for.html

Mike More
  • 94
  • 4