XPath problem - how can I correctly parse this XML?
Hello.
I have a problem with how XPath parses my XML doc, and I don't know if there is any way to fix it.
Let's say I have the following XML which represents room availability for a given hotel :
<GetAvailability>
<IdHotel>x</IdHotel>
<GetRoomDetails>
<Room>
<Type>x</Type>
<Price>x</Price>
</Room>
</GetRoomDetails>
</GetAvailability>
I am using the elements IdHotel, Type and Price to identify a unique GetAvailability. But neither the Type nor the Price elements are mandatory. There can be x amount of Rooms.
Now let's say I receive the following XML request:
<GetAvailability>
<IdHotel>12345</IdHotel>
<GetRoomDetails>
<Room>
<Type>SINGLE</Type>
<Price>50</Price>
</Room>
<Room>
<Type>DOUBLE</Type>
</Room>
<Room>
<Price>150</Price>
</Room>
</GetRoomDetails>
</GetAvailability>
What's seems to be happening is that XPath is getting confused. It finds the first Room element ok because it has both Type and Price sub-elements. But the second Room only has Type, and the third only has Price. What it's doing is pushing the value of Price for the third Room into the second. And it's leaving both Type and Price null for the third Room.
Is there any way to get XPath to return values something like this:
SINGLE
50
DOUBLE
null
null
150
Essentially, is there any way to make XPath aware that when non-mandatory elements are absent to 'fill them in' with values so I can properly parse the response?
Many thanks for any assistance!
Bob

