Xpath有助于找到元素

时间:2017-03-14 15:04:22

标签: xml xpath

从这个XML:

<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.websitek.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>
  <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>的第一个RefundType="non_refundable"。有一个<RoomRate>.的内部元素可以保存这个属性。

以下xpath:

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

选择两个AccommodationSegments(酒店(包含具有可退款和不可退款的RoomRate属性的两个AccommodationUnits(房间)

为什么会这样?

3 个答案:

答案 0 :(得分:1)

您可以将所需的路径用作所需的实际节点查询的一部分,如下所示:

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

答案 1 :(得分:1)

你是如此亲密:

(//*[local-name()="AccommodationUnit"]//*["RoomRate"][@RefundType='refundable'])[1]//..

修改过的测试数据(注意我使用MaxOccupancy字段作为记录ID):

<?xml version="1.0" encoding="UTF-8"?>
<AccommodationUnits>
 <AccommodationUnit MaxOccupancy="1" MinOccupancy="1" MaxExtraChildren="0" MaxExtraInfants="0" QuantityAvailable="9" Name="Studio - Self catering" Code="2SC" AUID="H2">
   <RoomRate StartDate="20170909" EndDate="20170916" Amount="180.16" Currency="GBP" BoardBasis="SC" RefundType="refundable" />
 </AccommodationUnit>
 <AccommodationUnit MaxOccupancy="2" MinOccupancy="1" MaxExtraChildren="0" MaxExtraInfants="0" QuantityAvailable="9" Name="Studio - Bed and Breakfast" Code="2BB" AUID="H2">
   <RoomRate StartDate="20170909" EndDate="20170916" Amount="216.91" Currency="GBP" BoardBasis="BB" RefundType="refundable" />
 </AccommodationUnit>
 <AccommodationUnit MaxOccupancy="3" MinOccupancy="1" MaxExtraChildren="0" MaxExtraInfants="0" QuantityAvailable="9" Name="Studio - Self catering" Code="2SC" AUID="H2">
   <RoomRate StartDate="20170909" EndDate="20170916" Amount="180.16" Currency="GBP" BoardBasis="SC" RefundType="non-refundable" />
 </AccommodationUnit>
 <AccommodationUnit MaxOccupancy="4" MinOccupancy="1" MaxExtraChildren="0" MaxExtraInfants="0" QuantityAvailable="9" Name="Studio - Bed and Breakfast" Code="2BB" AUID="H2">
   <RoomRate StartDate="20170909" EndDate="20170916" Amount="216.91" Currency="GBP" BoardBasis="BB" RefundType="non-refundable" />
 </AccommodationUnit>
</AccommodationUnits>

答案 2 :(得分:0)

我不确定这是一种通用方法,但这是一个非常简单的解决方案:

//AccommodationUnit[//@RefundType = 'refundable'][1]

这将选择示例中的第一个AccommodationUnit节点。