在 XQuery 中查找有关最新元素的信息



>我有一个这样的xml文件:

 <carSchema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="carSchema.xsd">
 <car>
    <License>23</License>
    <year>2010</year>
    <model>a</model>
    <manufacturer>hyundai</manufacturer>
</car>
<car>
    <License>24</License>
    <year>2002</year>
    <model>b</model>
    <manufacturer>hyundai</manufacturer>
</car>
<car>
    <License>25</License>
    <year>2005</year>
    <model>c</model>
    <manufacturer>hyundai</manufacturer>
</car>
<car>
    <License>26</License>
    <year>2004</year>
    <model>d</model>
</car>
<car>
    <License>27</License>
    <year>2016</year>
    <model>f</model>
    <manufacturer>hyundai</manufacturer>
</car>

我想在 Xquery 中查找有关最新汽车的信息。我写了这个查询,返回最新汽车的年份。

 xquery version "1.0";
 max(
 for $x in doc("car.xml")/carSchema/car
   order by $x/year descending
 return $x/year)

如何返回有关该汽车的所有信息(许可证、型号、制造商)?

您可以使用

(for $car in doc("car.xml")/carSchema/car
order by $car/year descending
return $car)[1]/*

查找元素的所有子元素,以及

(for $car in doc("car.xml")/carSchema/car
order by $car/year descending
return $car)[1]

以查找具有最新年份的元素本身。

最新更新