使用 Xquery 检索 XML 中的子节点选择
Retrieving selection of child nodes in XML using Xquery
我遗漏了一些东西但不确定是什么。我正在使用 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 & toilets. Permit parking available.</Notes>
<Telephone>01904551309</Telephone>
<URL>http://www.york.gov.uk/transport/Parking/Car_parks/nunnery_ln/</URL>
</CarPark>
</CarParkDataImport>
现在我有 2 个存储在 HTTP 服务器中的 Xqueries 查询是
(: 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 下的所有节点,但我如何才能恢复选定的节点?例如停车场名称、位置和邮政编码。
how do i just bring back a selection of nodes? For Example CarParkName, Location and Postcode
然后不要return $b
中包含的所有内容,而只是其中的一部分。以下查询 return 仅查询与位置匹配的停车场的 Location
、CarParkName
和 Postcode
元素。
该查询适用于我的本地系统,否则与您的相同。
(: 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)
将 return 这三个节点,以及一些不需要的名称空间声明,但也不应妨碍。
<?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,
我不熟悉 BaseX 的日志文件,但问题是响应格式不正确 XML;它没有单个根元素,正如 joemfb 已经有用地指出的那样。因此,解决方案是将要 returned 的元素包装在根元素中:
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
我遗漏了一些东西但不确定是什么。我正在使用 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 & toilets. Permit parking available.</Notes>
<Telephone>01904551309</Telephone>
<URL>http://www.york.gov.uk/transport/Parking/Car_parks/nunnery_ln/</URL>
</CarPark>
</CarParkDataImport>
现在我有 2 个存储在 HTTP 服务器中的 Xqueries 查询是
(: 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 下的所有节点,但我如何才能恢复选定的节点?例如停车场名称、位置和邮政编码。
how do i just bring back a selection of nodes? For Example CarParkName, Location and Postcode
然后不要return $b
中包含的所有内容,而只是其中的一部分。以下查询 return 仅查询与位置匹配的停车场的 Location
、CarParkName
和 Postcode
元素。
该查询适用于我的本地系统,否则与您的相同。
(: 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)
将 return 这三个节点,以及一些不需要的名称空间声明,但也不应妨碍。
<?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,
我不熟悉 BaseX 的日志文件,但问题是响应格式不正确 XML;它没有单个根元素,正如 joemfb 已经有用地指出的那样。因此,解决方案是将要 returned 的元素包装在根元素中:
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