Reputation: 69
I am missing something but not sure what. I am using BaseX and the Http server to throw REST GETs at the the XML i have strored in a data, which looks like 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>
<Notes>Open 24 hours. Park and pay by phone in this car park by calling 01904 279279. The location number for this car park is 7709. Blue badge holders can park free for as long as they want. CCTV & toilets. Permit parking available.</Notes>
<Telephone>01904551309</Telephone>
<URL>http://www.york.gov.uk/transport/Parking/Car_parks/nunnery_ln/</URL>
</CarPark>
</CarParkDataImport>
now i have 2 Xqueries i have stored in the HTTP server to query is
(: XQuery file: location.xq :)
declare default element namespace "http://www.transportdirect.info/carparking";
declare variable $a as xs:string external;
db:open("Car_park_data")/CarParkDataImport/CarPark[Location=$a]
and
(: XQuery file: loc.xq :)
declare default element namespace "http://www.transportdirect.info/carparking";
declare variable $a as xs:string external;
for $b in db:open("Car_park_data")/CarParkDataImport/CarPark
where $b/Location = $a
return $b/.
so i can run my REST http like (with the .xq changing depending on what i am querying.
http://localhost:8984/rest?run=loc.xq&$a=York
These both appear to work, and bring back all nodes under CarPark, but how do i just bring back a selection of nodes? For Example CarParkName, Location and Postcode.
Upvotes: 1
Views: 1256
Reputation: 22647
how do i just bring back a selection of nodes? For Example CarParkName, Location and Postcode
Then do not return everything that is contained in $b
, but only part of it. The following query returns only the Location
, CarParkName
and Postcode
elements of the car park that matches the location.
The query is adapted to work on my local system, otherwise, it's identical to yours.
(: XQuery file: loc.xq :)
declare default element namespace "http://www.transportdirect.info/carparking";
let $a := "York"
for $b in doc("carpark.xml")/CarParkDataImport/CarPark
where $b/Location = $a
return $b/(Location | CarParkName | Postcode)
will return those three nodes, and some namespace declarations that are not needed, but that should not get in the way either.
<?xml version="1.0" encoding="UTF-8"?>
<CarParkName xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.transportdirect.info/carparking">Nunnery Lane</CarParkName>
<Location xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.transportdirect.info/carparking">York</Location>
<Postcode xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.transportdirect.info/carparking">YO23 1AA</Postcode
i get the following error message. 'error on line 2 at column 1: Extra content at the end of the document' and it just shows one carparkname, would there be a log in BaseX, i can check to see what happening,
I'm not familiar with BaseX's log files, but the problem is that the response is not well-formed XML; it does not have a single root element, as joemfb has helpfully pointed out already. So, the solution is to wrap the elements to be returned in a root element:
declare default element namespace "http://www.transportdirect.info/carparking";
let $a := "York"
for $b in doc("carpark.xml")/CarParkDataImport/CarPark
where $b/Location = $a
return
<root>
{$b/(Location | CarParkName | Postcode)}
</root>
and the result:
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="http://www.transportdirect.info/carparking">
<CarParkName xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Nunnery Lane</CarParkName>
<Location xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">York</Location>
<Postcode xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">YO23 1AA</Postcode>
</root
Upvotes: 1