4

Trying to use XPath/Xquery for the first time, within BaseX and i have a collection/database of opendata.gov for carpark snippet below,

<?xml version="1.0" encoding="utf-8"?>
<CarParkDataImport xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xsi:schemaLocation="http://www.transportdirect.info/carparking B:/CODE/carparks/CarParking.xsd" xmlns="http://www.transportdirect.info/carparking">
<CarPark>
    <CarParkRef>3</CarParkRef>
    <CarParkName>Nunnery Lane</CarParkName>
    <Location>York</Location>
    <Address>Nunnery Lane--York--North Yorkshire</Address>
    <Postcode>YO23 1AA</Postcode>
</CarPark>

what i am trying to do is find where the location is a certain place, in this instance "Nunnery Lane" and then return the carpark reference so i tried (the db is called Car_park_data, and has 8 documents inside)

collection("Car_park_data")/CarParkDataImport/CarPark[Location="Nunnery Lane"]/CarParkRef

and then tried flowr

for $x in collection("Car_park_data")/CarParkDataImport/CarPark
where $x/Location="Nunnery Lane"
order by $x/CarParkRef
return $x/CarParkRef

both bring back no hits..the full details the first query is(the result bit of basex)

Compiling:
- pre-evaluating fn:collection("Car_park_data")
- adding text() step
- applying text index
Query:
collection("Car_park_data")/CarParkDataImport/CarPark[Location="Nunnery  Lane"]/CarParkRef
Optimized Query:
db:text("Car_park_data", "Nunnery  Lane")/parent::Location/parent::CarPark[parent::CarParkDataImport/parent::docume nt-node()]/CarParkRef
Result:
- Hit(s): 0 Items
- Updated: 0 Items
- Printed: 0 Bytes
- Read Locking: local [Car_park_data]
- Write Locking: none
Timing:
- Parsing: 1.33 ms
- Compiling: 0.54 ms
- Evaluating: 0.36 ms
- Printing: 0.28 ms
- Total Time: 2.52 ms
Query plan:
<QueryPlan>
<CachedPath>
 <ValueAccess data="Car_park_data" type="TEXT">
  <Str value="Nunnery Lane" type="xs:string"/>
 </ValueAccess>
 <IterStep axis="parent" test="Location"/>
  <IterStep axis="parent" test="CarPark">
  <CachedPath>
    <IterStep axis="parent" test="CarParkDataImport"/>
    <IterStep axis="parent" test="document-node()"/>
  </CachedPath>
   </IterStep>
   <IterStep axis="child" test="CarParkRef"/>
  </CachedPath>
</QueryPlan>

what am i doing wrong, As i said using basex, you can see that it its viable Xpat/Xquery, (i.e. basex reports no errors) but i am guessing something wrong in my Xquery?

if i do an "find" with BaseX for "Nunnery Lane" this is the results that come back

Query:
/descendant-or-self::*[text() contains text "Nunnery Lane"]
Result:
- Hit(s): 4 Items
- Updated: 0 Items
- Printed: 601 Bytes
- Read Locking: global
- Write Locking: global

so i then tried adding the contains text to my query, to the same avale, no hits

Thank you for any help

1 Answers1

7

Take the namespace into account by adding

declare default element namespace "http://www.transportdirect.info/carparking";

see http://www.w3.org/TR/xquery/#id-default-namespace.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
  • Thank you for the reply, i have added it before both queries and it has the same result still no hits, does not BaseX declare the namespace for you, when you create the xml as a database? – funkmaster20th Apr 12 '15 at 16:35
  • Well your sample has `York` while your query does `Location="Nunnery Lane"`, so I don't see how that condition can be true. I think the namespace declaration is needed to deal with XML with namespaces. So try the condition `CarParkName = "Nunnery Lane"` if your XML really has `Nunnery Lane` posted. – Martin Honnen Apr 12 '15 at 16:41
  • EDIT: okay.. some reason... is working now... Thank you.. `declare default element namespace "http://www.transportdirect.info/carparking"; /CarParkDataImport/CarPark[Location="York"]/CarParkRef` brings back results sorry york was just a snippet, nunnery lane is in the rest of the xml, thank you for your help – funkmaster20th Apr 12 '15 at 16:52
  • At last the solution. I gave up trying to use BaseX some time ago and just came back to it to try some datamining. This is a significant omission from the getting started/tutorial docs as most peoples XML will declare its own namespace. You can get BaseX to strip the namespace when creating a new database from an existing XML file, see the Parsing tab – JamesP Jul 02 '18 at 15:50