1

Given the following XML:

<?xml version="1.0" encoding="UTF-8"?>
<Accommodations>
 <AccommodationSegment AccommodationCode="627|26234" AccommodationId="15390132" AccommodationName="blah" Availability="available" BasicAdultCost="180.16" BoardCode="SC" BookableByFAB="true" BrandName="blah" ClassCode="2*" Currency="GBP" EndDate="20170918" ItineraryId="si1068" MaxChildAge="17" NormalisedName="BLAH" NumNights="7" ResortId="558" ResortName="blah" StartDate="20170911" Supplier="TT2" SyndicatorRanking="100" Type="Apartments">
  <AvailableBoardBasis>BB</AvailableBoardBasis>
  <AvailableBoardBasis>HB</AvailableBoardBasis>
  <AvailableBoardBasis>SC</AvailableBoardBasis>
  <Address Address1="blah" Address2="blah" Address3="Tenerife" CityOrTown="Puerto de la Cruz" Country="ES" PostCode="34324" />
  <TelephoneNumber>+34 922370565</TelephoneNumber>
  <Description>blah blah blah blah blah blah blah blah blah blah blah blah 
  blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah</Description>
  <GeoPosition Latitude="28.4129" Longitude="-16.5437" />
  <DirectLink System="FABRIX">
     <DirectLinkURL><![CDATA[https://ww9.website.com/deep_linking/deep_linking.html?rooms=1&amp;journey=Hotels&amp;checkinDate=11092017&amp;duration=7&amp;suppliers=TT2&amp;accommodationId=15390132]]></DirectLinkURL>
  </DirectLink>
  <AccommodationUnits>
     <AccommodationUnit AUID="H2" Code="2SC" MaxExtraChildren="0" MaxExtraInfants="0" MaxOccupancy="1" MinOccupancy="1" Name="Studio - Self catering" QuantityAvailable="9">
        <RoomRate Amount="180.16" BoardBasis="SC" Currency="GBP" EndDate="20170918" RefundType="refundable" StartDate="20170911" />
     </AccommodationUnit>
     <AccommodationUnit AUID="H2" Code="2BB" MaxExtraChildren="0" MaxExtraInfants="0" MaxOccupancy="1" MinOccupancy="1" Name="Studio - Bed and Breakfast" QuantityAvailable="9">
        <RoomRate Amount="216.91" BoardBasis="BB" Currency="GBP" EndDate="20170918" RefundType="refundable" StartDate="20170911" />
     </AccommodationUnit>
     <AccommodationUnit AUID="H2" Code="2HB" MaxExtraChildren="0" MaxExtraInfants="0" MaxOccupancy="1" MinOccupancy="1" Name="Studio - Half board" QuantityAvailable="9">
        <RoomRate Amount="268.36" BoardBasis="HB" Currency="GBP" EndDate="20170918" RefundType="refundable" StartDate="20170911" />
     </AccommodationUnit>
  </AccommodationUnits>
  </AccommodationSegment>
 <AccommodationSegment AccommodationCode="627|26234"  AccommodationId="15390132" AccommodationName="blah" Availability="available" BasicAdultCost="180.16" BoardCode="SC" BookableByFAB="true" BrandName="blah" ClassCode="2*" Currency="GBP" EndDate="20170918" ItineraryId="si1068" MaxChildAge="17" NormalisedName="BLAH" NumNights="7" ResortId="558" ResortName="blah" StartDate="20170911" Supplier="TT2" SyndicatorRanking="100" Type="Apartments">
  <AvailableBoardBasis>BB</AvailableBoardBasis>
  <AvailableBoardBasis>HB</AvailableBoardBasis>
  <AvailableBoardBasis>SC</AvailableBoardBasis>
  <Address Address1="blah" Address2="blah" Address3="Tenerife" CityOrTown="Puerto de la Cruz" Country="ES" PostCode="73737" />
  <TelephoneNumber>0034 922384811</TelephoneNumber>
  <Description>blah blah blah blah blah blah blah blah blah blah blah blah 
  blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah</Description>
  <GeoPosition Latitude="28.4119" Longitude="-16.5473" />
  <DirectLink System="FABRIX">
     <DirectLinkURL><![CDATA[https://ww9.website.com/deep_linking/deep_linking.html?rooms=1&amp;journey=Hotels&amp;checkinDate=12092017&amp;duration=7&amp;suppliers=TT2&amp;accommodationId=16271549]]></DirectLinkURL>
  </DirectLink>
  <AccommodationUnits>
     <AccommodationUnit AUID="H4476" Code="4476BB" MaxExtraChildren="0" MaxExtraInfants="0" MaxOccupancy="1" MinOccupancy="1" Name="Double Room for Single Use - Bed and Breakfast" QuantityAvailable="9">
        <RoomRate Amount="250.04" BoardBasis="BB" Currency="GBP" EndDate="20170919" RefundType="non_refundable" StartDate="20170912" />
     </AccommodationUnit>
     <AccommodationUnit AUID="H4476" Code="4476HB" MaxExtraChildren="0" MaxExtraInfants="0" MaxOccupancy="1" MinOccupancy="1" Name="Double Room for Single Use - Half board" QuantityAvailable="9">
        <RoomRate Amount="276.97" BoardBasis="HB" Currency="GBP" EndDate="20170919" RefundType="non_refundable" StartDate="20170912" />
     </AccommodationUnit>
     <AccommodationUnit AUID="H4476" Code="4476FB" MaxExtraChildren="0" MaxExtraInfants="0" MaxOccupancy="1" MinOccupancy="1" Name="Double Room for Single Use - Full board" QuantityAvailable="9">
        <RoomRate Amount="303.88" BoardBasis="FB" Currency="GBP" EndDate="20170919" RefundType="non_refundable" StartDate="20170912" />
     </AccommodationUnit>
     <AccommodationUnit AUID="H4476" Code="4476AI" MaxExtraChildren="0" MaxExtraInfants="0" MaxOccupancy="1" MinOccupancy="1" Name="Double Room for Single Use - All inclusive" QuantityAvailable="9">
        <RoomRate Amount="317.34" BoardBasis="AI" Currency="GBP" EndDate="20170919" RefundType="non_refundable" StartDate="20170912" />
     </AccommodationUnit>
  </AccommodationUnits>
  </AccommodationSegment>
</Accommodations>

And the following xpath query:-

(//*[local-name()="AccommodationSegment" and //*["RoomRate"][@RefundType="non_refundable"]])[1]

Why does the query return the first (Hotel) that contains 'refundable' rooms and not the second hotel which contains 'non_refundable' hotels? What is wrong with the query and/or is there a query which will work?

4
  • What exact output you want to get? Commented Mar 17, 2017 at 12:08
  • I want the first <AccommodationSegment> that contains non-refundable rooms which, in the xml, is an attribute 'RefundType' of <RoomRate>. Using this tool:codebeautify.org/Xpath-Tester you will find my query returns the first <AccommodationSegment> which contains 'refundable' and not what I would expect - the first <AccommodationSegment> which contains non_refundable Commented Mar 17, 2017 at 12:31
  • Are you sure you need AccommodationSegment, but not AccommodationUnits or AccommodationUnit? There is only one AccommodationSegment on provided xml sample and it has both <RoomRate RefundType="non_refundable"> and <RoomRate RefundType="refundable"> Commented Mar 17, 2017 at 12:35
  • sorry, I pasted the wrong xml - now updated. Yes, I need the AccommodationSegment which has non_refundable rooms Commented Mar 17, 2017 at 12:48

1 Answer 1

2

You can use below XPath expression to match first AccommodationSegment element that contains <RoomRate RefundType="non_refundable">:

(//AccommodationSegment[./descendant::RoomRate[@RefundType="non_refundable"]])[1]

or you can modify your XPath as

(//*[local-name()="AccommodationSegment" and .//*[@RefundType="non_refundable"]])[1]

Note that

  1. I removed ["RoomRate"] predicate as string will always return True
  2. I replaced //*[@RefundType... with .//*[@RefundType... as your expression means

find any element if its name is "AccommodationSegment" and if there is any element with attribute RefundType="non_refundable" SOMEWHERE in the document

while my expression means:

find any element if its name is "AccommodationSegment" and if IT CONTAINS any element with attribute RefundType="non_refundable"

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.