草庐IT

xml - XPath 3.x - 排序功能

coder 2024-07-05 原文

我需要使用纯 XPath 3.0 对一系列元素进行排序,因此不需要 XQuery、XSL-T 和代码。我尝试关注 this answer on how to use the sort function ,但我不是 XPath 专家,所以我不知道如何使用它。

所以,我的文档基本上有以下结构:

<?xml version="1.0" encoding="UTF-8"?>
<AppointmentList>
    <Appointment id="11" creatorID="1" creationDate="2018-03-01" reschedulable="ja" appointmentSeries="ja">
        <Start date="2018-03-14" time="12:00:00"/>
        <End date="2018-03-14" time="13:30:00"/>
        <Description>Vorführung</Description>
    </Appointment>

    <Appointment id="22" creatorID="2" creationDate="2018-02-14" reschedulable="ja" appointmentSeries="nein">
        <Start date="2018-03-20" time="13:00:00"/>
        <End date="2018-03-20" time="14:00:00"/>        
        <Description>Programm Meeting</Description>
        <Benachrichtigung art="EMail"/>
    </Appointment>

    <Appointment id="33" creatorID="3" creationDate="2018-02-23" reschedulable="nein" appointmentSeries="ja">
        <Start date="2018-02-24" time="15:00:00"/>
        <End date="2018-02-24" time="16:00:00"/>        
        <Description>Burglary Report</Description>
        <Benachrichtigung art="Beep"/>
    </Appointment>

    <Appointment id="44" creatorID="1" creationDate="2018-01-01" reschedulable="nein" appointmentSeries="nein">
        <Start date="2018-05-01" time="10:00:00"/>
        <End date="2018-05-01" time="17:00:00"/>        
        <Description>Besprechung Ministerium</Description>
    </Appointment>

    <Appointment id="55" creatorID="8" creationDate="2018-02-28" reschedulable="nein" appointmentSeries="nein">
        <Start date="2018-06-08" time="08:00:00"/>
        <End date="2018-06-09" time="18:00:00"/>        
        <Description>Spam Konferenz</Description>
    </Appointment>

    <Appointment id="66" creatorID="10" creationDate="2018-03-22" reschedulable="nein" appointmentSeries="nein">
        <Start date="2018-05-07" time="08:00:00"/>
        <End date="2018-05-07" time="18:00:00"/>        
        <Description>XML Tutorium</Description>
    </Appointment>

    <Appointment id="77" creatorID="9" creationDate="2018-03-15" reschedulable="ja" appointmentSeries="nein">
        <Start date="2018-04-20" time="08:00:00"/>
        <End date="2018-04-20" time="09:00:00"/>        
        <Description>Abschlussprüfung</Description>
    </Appointment>

    <Appointment id="88" creatorID="7" creationDate="2018-03-09" reschedulable="nein" appointmentSeries="ja">
        <Start date="2018-03-14" time="17:00:00"/>
        <End date="2018-03-14" time="18:00:00"/>        
        <Description>Versammlung Workaholics</Description>
    </Appointment>

    <Appointment id="99" creatorID="6" creationDate="2018-02-01" reschedulable="nein" appointmentSeries="nein">
        <Start date="2018-02-28" time="10:00:00"/>
        <End date="2018-02-28" time="17:00:00"/>        
        <Description>Fortbildung Datenbanken</Description>
    </Appointment>
</AppointmentList>

我想按日期升序对接下来的三个约会进行排序。因此,从现在开始的下一个约会位于结果序列的第一个位置,而从距现在最近的第三个日期开始的约会应该是序列的最后一个元素。下面显示的查询为我提供了尚未发生的约会,但我不知道如何正确应用排序功能。

/AppointmentList/Appointment[fn:dateTime(Start/@date, Start/@time) > fn:current-dateTime()]

谢谢,因为我找不到任何关于 fn:sort 函数的好文档。

最佳答案

函数fn:sort(...)存在三个版本,最多三个参数:

  1. $input as item()*:输入序列,
  2. $collat​​ion as xs:string?:用于字符串比较(或空序列)的collat​​ion
  3. $key as function(item()) as xs:anyAtomicType*) as item()*:提取排序键<>函数项/strong>(即应该比较的部分)从项目中排序。

因为您不想对字符串进行排序,所以第二个参数应该是 ()

您案例中的排序键是每次约会的开始日期和时间。因此,您需要提供一个函数项,给定一个 Appointment 节点,返回其开始日期的 xs:dateTime: function($app) { fn:日期时间($app/Start/@date, $app/Start/@time) }

将所有内容放在一起:

subsequence(
  sort(
    /AppointmentList/Appointment[fn:dateTime(Start/@date, Start/@time) > fn:current-dateTime()],
    (),
    function($app) { fn:dateTime($app/Start/@date, $app/Start/@time) }
  ),
  1,
  3
)

结果:

<Appointment id="77" creatorID="9" creationDate="2018-03-15" reschedulable="ja" appointmentSeries="nein">
  <Start date="2018-04-20" time="08:00:00"/>
  <End date="2018-04-20" time="09:00:00"/>
  <Description>Abschlussprüfung</Description>
</Appointment>
<Appointment id="44" creatorID="1" creationDate="2018-01-01" reschedulable="nein" appointmentSeries="nein">
  <Start date="2018-05-01" time="10:00:00"/>
  <End date="2018-05-01" time="17:00:00"/>
  <Description>Besprechung Ministerium</Description>
</Appointment>
<Appointment id="66" creatorID="10" creationDate="2018-03-22" reschedulable="nein" appointmentSeries="nein">
  <Start date="2018-05-07" time="08:00:00"/>
  <End date="2018-05-07" time="18:00:00"/>
  <Description>XML Tutorium</Description>
</Appointment>

关于xml - XPath 3.x - 排序功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49559927/

有关xml - XPath 3.x - 排序功能的更多相关文章

  1. ruby - 如何使用 Nokogiri 的 xpath 和 at_xpath 方法 - 2

    我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div

  2. ruby-on-rails - 如何从 format.xml 中删除 <hash></hash> - 2

    我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为

  3. ruby-on-rails - Nokogiri:使用 XPath 搜索 <div> - 2

    我使用Nokogiri(Rubygem)css搜索寻找某些在我的html里面。看起来Nokogiri的css搜索不喜欢正则表达式。我想切换到Nokogiri的xpath搜索,因为这似乎支持搜索字符串中的正则表达式。如何在xpath搜索中实现下面提到的(伪)css搜索?require'rubygems'require'nokogiri'value=Nokogiri::HTML.parse(ABBlaCD3"HTML_END#my_blockisgivenmy_bl="1"#my_eqcorrespondstothisregexmy_eq="\/[0-9]+\/"#FIXMEThefoll

  4. ruby-on-rails - Cucumber 是否只是 rspec 的包装器以帮助将测试组织成功能? - 2

    只是想确保我理解了事情。据我目前收集到的信息,Cucumber只是一个“包装器”,或者是一种通过将事物分类为功能和步骤来组织测试的好方法,其中实际的单元测试处于步骤阶段。它允许您根据事物的工作方式组织您的测试。对吗? 最佳答案 有点。它是一种组织测试的方式,但不仅如此。它的行为就像最初的Rails集成测试一样,但更易于使用。这里最大的好处是您的session在整个Scenario中保持透明。关于Cucumber的另一件事是您(应该)从使用您的代码的浏览器或客户端的角度进行测试。如果您愿意,您可以使用步骤来构建对象和设置状态,但通常您

  5. ruby-on-rails - capybara ::ElementNotFound:无法找到 xpath "/html" - 2

    我正在学习http://ruby.railstutorial.org/chapters/static-pages上的RubyonRails教程并遇到以下错误StaticPagesHomepageshouldhavethecontent'SampleApp'Failure/Error:page.shouldhave_content('SampleApp')Capybara::ElementNotFound:Unabletofindxpath"/html"#(eval):2:in`text'#./spec/requests/static_pages_spec.rb:7:in`(root)'

  6. ruby-on-rails - 需要帮助最大化多个相似对象中的 3 个因素并适当排序 - 2

    我需要用任何语言编写一个算法,根据3个因素对数组进行排序。我以度假村为例(如Hipmunk)。假设我想去度假。我想要最便宜的地方、最好的评论和最多的景点。但是,显然我找不到在所有3个中都排名第一的方法。Example(assumingthereare20importantattractions):ResortA:$150/night...98/100infavorablereviews...18of20attractionsResortB:$99/night...85/100infavorablereviews...12of20attractionsResortC:$120/night

  7. ruby-on-rails - 在具有 ActiveRecord 条件的相关模型中按字段排序 - 2

    我正在尝试按Rails相关模型中的字段进行排序。我研究的所有解决方案都没有解决如果相关模型被另一个参数过滤?元素模型classItem相关模型:classPriority我正在使用where子句检索项目:@items=Item.where('company_id=?andapproved=?',@company.id,true).all我需要按相关表格中的“位置”列进行排序。问题在于,在优先级模型中,一个项目可能会被多家公司列出。因此,这些职位取决于他们拥有的company_id。当我显示项目时,它是针对一个公司的,按公司内的职位排序。完成此任务的正确方法是什么?感谢您的帮助。PS-我

  8. ruby - 按数字(从大到大)然后按字母(字母顺序)对对象集合进行排序 - 2

    我正在构建一个小部件来显示奥运会的奖牌数。我有一个“国家”对象的集合,其中每个对象都有一个“名称”属性,以及奖牌计数的“金”、“银”、“铜”。列表应该排序:1.首先是奖牌总数2.如果奖牌相同,按类型分割(金>银>铜,即2金>1金+1银)3.如果奖牌和类型相同,则按字母顺序子排序我正在用ruby​​做这件事,但我想语言并不重要。我确实找到了一个解决方案,但如果感觉必须有更优雅的方法来实现它。这是我做的:使用加权奖牌总数创建一个虚拟属性。因此,如果他们有2个金牌和1个银牌,加权总数将为“3.020100”。1金1银1铜为“3.010101”由于我们希望将奖牌数排序为最高的,因此列表按降序排

  9. ruby-on-rails - rails 功能测试 - 2

    在Rails自动生成的功能测试(test/functional/products_controller_test.rb)中,我看到以下代码:classProductsControllerTest我的问题是:方法调用products()在哪里/如何定义?products(:one)到底是什么意思?看代码,大概意思是“创建一个产品”,但是它是如何工作的呢?注意我是Ruby/Rails的新手,如果这些是微不足道的问题,我深表歉意。 最佳答案 如果您查看test/fixtures文件夹,您会看到一个products.yml文件。这是在您创建

  10. ruby-on-rails - 在不重新查询数据库的情况下重新排序 Rails 中的事件记录? - 2

    例如,假设我有一个名为Products的模型,并且在ProductsController中,我有以下代码用于product_listView以显示已排序的产品。@products=Product.order(params[:order_by])让我们想象一下,在product_listView中,用户可以使用下拉菜单按价格、评级、重量等进行排序。数据库中的产品不会经常更改。我很难理解的是,每次用户选择新的order_by过滤器时,rails是否必须查询,或者rails是否能够以某种方式缓存事件记录以在服务器端重新排序?有没有一种方法可以编写它,以便在用户排序时rails不会重新查询结果

随机推荐