使用Xquery检索XML中的子节点选择

时间:2015-04-20 21:20:51

标签: xml xpath xquery basex

我错过了一些东西,但不确定是什么。我正在使用BaseX和Http服务器在我已经在数据中搜索的XML中抛出REST GET,如下所示。

 <?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 &amp; toilets. Permit parking available.</Notes>
    <Telephone>01904551309</Telephone>
    <URL>http://www.york.gov.uk/transport/Parking/Car_parks/nunnery_ln/</URL>
</CarPark>
</CarParkDataImport>

现在我有2个Xqueries,我已经存储在HTTP服务器中进行查询

(: 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]

(: 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/. 

所以我可以运行我的REST http(根据我查询的内容,.xq会发生变化。

http://localhost:8984/rest?run=loc.xq&$a=York

这两个似乎都有效,并且可以恢复CarPark下的所有节点,但是我如何才能恢复选择的节点?例如CarParkName,Location和Postcode。

1 个答案:

答案 0 :(得分:1)

  

我如何才能恢复选择的节点?例如CarParkName,Location和Postcode

然后不要返回$b中包含的所有内容,而只返回其中的一部分。以下查询仅返回与该位置匹配的停车场的LocationCarParkNamePostcode元素。

该查询适用于我的本地系统,否则,它与您的相同。

(: 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)

将返回这三个节点,以及一些不需要的命名空间声明,但这不应该妨碍。

<?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

  

我收到以下错误消息。 '第1行第2行的错误:文档末尾的额外内容',它只显示一个carparkname,BaseX中是否有日志,我可以查看发生了什么,

我不熟悉BaseX的日志文件,但问题是响应不是格式良好的XML;它没有单个根元素,因为joemfb已经有用地指出了。因此,解决方案是将要返回的元素包装在根元素中:

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>

结果:

<?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