0

I would like to find the path of the elements having "cost" tag =505.9214355631349, i tried to do it and i always have null values here is one what I've done and which is giving me more or less what i want, it is not working correctly, I will use this path in java to get the values below the cost tag containing the specific value

my xpath problem/solutions/solution[cost='505.9208295302417']/text() please note that this xml file is dynamic

xml file

<?xml version="1.0" encoding="UTF-8"?>
<problem xmlns="http://www.w3schools.com"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3schools.com vrp_xml_schema.xsd">
     <problemType>
          <fleetSize>FINITE</fleetSize>
     </problemType>
     <vehicles>
          <vehicle>
               <id>1_1</id>
               <typeId>type_1</typeId>
               <startLocation>
                    <id>[x=-20.2675][y=57.4797]</id>
                    <coord x="-20.2675" y="57.4797"/>
               </startLocation>
               <endLocation>
                    <id>[x=-20.2675][y=57.4797]</id>
                    <coord x="-20.2675" y="57.4797"/>
               </endLocation>
               <timeSchedule>
                    <start>0.0</start>
                    <end>1.7976931348623157E308</end>
               </timeSchedule>
               <returnToDepot>true</returnToDepot>
          </vehicle>
          <vehicle>
               <id>1_2</id>
               <typeId>type_1</typeId>
               <startLocation>
                    <id>[x=-20.2675][y=57.4797]</id>
                    <coord x="-20.2675" y="57.4797"/>
               </startLocation>
               <endLocation>
                    <id>[x=-20.2675][y=57.4797]</id>
                    <coord x="-20.2675" y="57.4797"/>
               </endLocation>
               <timeSchedule>
                    <start>0.0</start>
                    <end>1.7976931348623157E308</end>
               </timeSchedule>
               <returnToDepot>true</returnToDepot>
          </vehicle>
          <vehicle>
               <id>2_1</id>
               <typeId>type_2</typeId>
               <startLocation>
                    <id>[x=-20.2675][y=57.4797]</id>
                    <coord x="-20.2675" y="57.4797"/>
               </startLocation>
               <endLocation>
                    <id>[x=-20.2675][y=57.4797]</id>
                    <coord x="-20.2675" y="57.4797"/>
               </endLocation>
               <timeSchedule>
                    <start>0.0</start>
                    <end>1.7976931348623157E308</end>
               </timeSchedule>
               <returnToDepot>true</returnToDepot>
          </vehicle>
          <vehicle>
               <id>3_1</id>
               <typeId>type_3</typeId>
               <startLocation>
                    <id>[x=-20.2675][y=57.4797]</id>
                    <coord x="-20.2675" y="57.4797"/>
               </startLocation>
               <endLocation>
                    <id>[x=-20.2675][y=57.4797]</id>
                    <coord x="-20.2675" y="57.4797"/>
               </endLocation>
               <timeSchedule>
                    <start>0.0</start>
                    <end>1.7976931348623157E308</end>
               </timeSchedule>
               <returnToDepot>true</returnToDepot>
          </vehicle>
     </vehicles>
     <vehicleTypes>
          <type>
               <id>type_1</id>
               <capacity-dimensions>
                    <dimension index="0">120</dimension>
               </capacity-dimensions>
               <costs>
                    <fixed>0.0</fixed>
                    <distance>1.0</distance>
                    <time>0.0</time>
                    <service>0.0</service>
                    <wait>0.0</wait>
               </costs>
          </type>
          <type>
               <id>type_2</id>
               <capacity-dimensions>
                    <dimension index="0">160</dimension>
               </capacity-dimensions>
               <costs>
                    <fixed>0.0</fixed>
                    <distance>1.1</distance>
                    <time>0.0</time>
                    <service>0.0</service>
                    <wait>0.0</wait>
               </costs>
          </type>
          <type>
               <id>type_3</id>
               <capacity-dimensions>
                    <dimension index="0">300</dimension>
               </capacity-dimensions>
               <costs>
                    <fixed>0.0</fixed>
                    <distance>1.3</distance>
                    <time>0.0</time>
                    <service>0.0</service>
                    <wait>0.0</wait>
               </costs>
          </type>
     </vehicleTypes>
     <services>
          <service id="5 " type="service">
               <location>
                    <id>[x=57.49129867553711][y=-20.29949951171875]</id>
                    <coord x="57.49129867553711" y="-20.29949951171875"/>
               </location>
               <capacity-dimensions>
                    <dimension index="0">100</dimension>
               </capacity-dimensions>
               <duration>10.0</duration>
               <timeWindows>
                    <timeWindow>
                         <start>0.0</start>
                         <end>1.7976931348623157E308</end>
                    </timeWindow>
               </timeWindows>
          </service>
          <service id="2 " type="service">
               <location>
                    <id>[x=57.49300003051758][y=-20.298799514770508]</id>
                    <coord x="57.49300003051758" y="-20.298799514770508"/>
               </location>
               <capacity-dimensions>
                    <dimension index="0">20</dimension>
               </capacity-dimensions>
               <duration>10.0</duration>
               <timeWindows>
                    <timeWindow>
                         <start>0.0</start>
                         <end>1.7976931348623157E308</end>
                    </timeWindow>
               </timeWindows>
          </service>
          <service id="1 " type="service">
               <location>
                    <id>[x=57.49380111694336][y=-20.29840087890625]</id>
                    <coord x="57.49380111694336" y="-20.29840087890625"/>
               </location>
               <capacity-dimensions>
                    <dimension index="0">80</dimension>
               </capacity-dimensions>
               <duration>10.0</duration>
               <timeWindows>
                    <timeWindow>
                         <start>0.0</start>
                         <end>1.7976931348623157E308</end>
                    </timeWindow>
               </timeWindows>
          </service>
          <service id="3 " type="service">
               <location>
                    <id>[x=57.49290084838867][y=-20.298200607299805]</id>
                    <coord x="57.49290084838867" y="-20.298200607299805"/>
               </location>
               <capacity-dimensions>
                    <dimension index="0">80</dimension>
               </capacity-dimensions>
               <duration>10.0</duration>
               <timeWindows>
                    <timeWindow>
                         <start>0.0</start>
                         <end>1.7976931348623157E308</end>
                    </timeWindow>
               </timeWindows>
          </service>
          <service id="4 Jean Nicolas Yung" type="service">
               <location>
                    <id>[x=57.49209976196289][y=-20.298599243164062]</id>
                    <coord x="57.49209976196289" y="-20.298599243164062"/>
               </location>
               <capacity-dimensions>
                    <dimension index="0">100</dimension>
               </capacity-dimensions>
               <duration>10.0</duration>
               <timeWindows>
                    <timeWindow>
                         <start>0.0</start>
                         <end>1.7976931348623157E308</end>
                    </timeWindow>
               </timeWindows>
          </service>
     </services>
     <solutions>
          <solution>
               <cost>505.9214355631349</cost>
               <routes>
                    <route>
                         <driverId>noDriver</driverId>
                         <vehicleId>1_1</vehicleId>
                         <start>0.0</start>
                         <act type="service">
                              <serviceId>5 </serviceId>
                              <arrTime>109.9819741964403</arrTime>
                              <endTime>119.9819741964403</endTime>
                         </act>
                         <end>229.9639483928806</end>
                    </route>
                    <route>
                         <driverId>noDriver</driverId>
                         <vehicleId>3_1</vehicleId>
                         <start>0.0</start>
                         <act type="service">
                              <serviceId>2 </serviceId>
                              <arrTime>109.98268205388193</arrTime>
                              <endTime>119.98268205388193</endTime>
                         </act>
                         <act type="service">
                              <serviceId>1 </serviceId>
                              <arrTime>119.98357684436793</arrTime>
                              <endTime>129.98357684436792</endTime>
                         </act>
                         <act type="service">
                              <serviceId>3 </serviceId>
                              <arrTime>129.98449911991617</arrTime>
                              <endTime>139.98449911991617</endTime>
                         </act>
                         <act type="service">
                              <serviceId>4 Jean Nicolas Yung</serviceId>
                              <arrTime>139.98539391040217</arrTime>
                              <endTime>149.98539391040217</endTime>
                         </act>
                         <end>259.9672978232725</end>
                    </route>
               </routes>
          </solution>
          <solution>
               <cost>505.9208295302417</cost>
               <routes>
                    <route>
                         <driverId>noDriver</driverId>
                         <vehicleId>1_1</vehicleId>
                         <start>0.0</start>
                         <act type="service">
                              <serviceId>5 </serviceId>
                              <arrTime>109.9819741964403</arrTime>
                              <endTime>119.9819741964403</endTime>
                         </act>
                         <end>229.9639483928806</end>
                    </route>
                    <route>
                         <driverId>noDriver</driverId>
                         <vehicleId>3_1</vehicleId>
                         <start>0.0</start>
                         <act type="service">
                              <serviceId>4 Jean Nicolas Yung</serviceId>
                              <arrTime>109.98190391287031</arrTime>
                              <endTime>119.98190391287031</endTime>
                         </act>
                         <act type="service">
                              <serviceId>2 </serviceId>
                              <arrTime>119.98282618841856</arrTime>
                              <endTime>129.98282618841856</endTime>
                         </act>
                         <act type="service">
                              <serviceId>1 </serviceId>
                              <arrTime>129.98372097890456</arrTime>
                              <endTime>139.98372097890456</endTime>
                         </act>
                         <act type="service">
                              <serviceId>3 </serviceId>
                              <arrTime>139.9846432544528</arrTime>
                              <endTime>149.9846432544528</endTime>
                         </act>
                         <end>259.9668316441239</end>
                    </route>
               </routes>
          </solution>
     </solutions>
</problem>
Mattieu Kevin
  • 23
  • 1
  • 8
  • Please post the java code in order for us to accurately assess why you're getting null. – George Aristy Mar 02 '18 at 15:59
  • It's not quite clear what your desired result is. Are you expecting all `` elements? Your current xpath tries to get `text()` but your ``does not have any text content. – Filburt Mar 02 '18 at 15:59

1 Answers1

2

I do not know exactly what you are looking for, but you might try this:

/*:problem/*:solutions/*:solution[*:cost/text()="505.9208295302417"]

Your XML is using a default namespace. The easy approach is a wildcard *: but it is better - in general - to declare a namespace and use it accordingly.

This will search for the solution, where the embedded <cost> element has a text() of your search value.

Shnugo
  • 66,100
  • 9
  • 53
  • 114