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

[1883 byte] By [syg6a] at [2007-11-27 9:23:45]
# 1

I wanted to make a small clarification. It isn't that XPath is 'pushing' the Price value for the third Room into the second Room exactly. The problem is that when I execute the XPath expression to get Type elements, since Type is missing from the third room it's returning an array of Types of length 2. The same goes for Price, since the Price element is missing from the second Room.

To illustrate:

/GetAvailability/Room/Type/ returns [SINGLE,DOUBLE] because Type is missing from the third Room, and:

/GetAvailability/Room/Price/ returns [50,150] because Price is missing from the second Room.

What I would like it to return is:

[SINGLE,DOUBLE, null]

[50,null,150]

Is this possible?

Thanks,

Bob

syg6a at 2007-7-12 22:19:10 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...