首页 > 解决方案 > Qt QML XQuery 按降序排序

问题描述

我正在使用 Qt/Qml。我想使用 Query 按降序对 XML 记录进行排序。我该如何编写这种排序查询?

这是我获取列表和查询道具的 QML 项目

 XmlListModel {
        id: xmlModel
        source: "record.xml"
        query: "/catalog/* [sort($id as item()*)]"

        XmlRole { name: "id"; query: "id/string()" }
    }

假设的 Xml 示例:

<?xml version="1.0"?>
<catalog>
   <booke >
      <id>1</id>
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
   </booke>
   <bookz >
      <id>2</id>
      <author>Ralls, Kim</author>
      <title>Midnight Rain</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-12-16</publish_date>
      <description>A former architect battles corporate zombies, 
      an evil sorceress, and her own childhood to become queen 
      of the world.</description>
   </bookz>
   <bookd >
      <id>3</id>
      <author>Corets, Eva</author>
      <title>Maeve Ascendant</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-11-17</publish_date>
      <description>After the collapse of a nanotechnology 
      society in England, the young survivors lay the 
      foundation for a new society.</description>
   </bookd>
</catalog>

标签: xmlqtxpathqmlxquery

解决方案


正常的 XQuery 1(如果我还记得的话)将类似于

for $item in catalog/*
order by xs:integer($item/id) descending
return $item

sort我认为在 XQuery/XPath 3.0 之前没有引入函数。在那里你可以使用sort(catalog/*, (), function($item) { -$item/id }).

我不知道 qt 支持什么,但我宁愿猜测 XQuery 1 而不是 3。


推荐阅读