2

我需要使用普通的 XPath 3.0 对一系列元素进行排序,所以没有 XQuery、没有 XSL-T 和没有代码。我试过以下这个关于如何使用排序功能的答案,但我不是 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 函数的任何好的文档。

4

1 回答 1

4

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

  1. $input as item()*:输入序列,
  2. $collation as xs:string?:字符串比较的排序规则(或空序列),
  3. $key as function(item()) as xs:anyAtomicType*) as item()*:一个功能项,从要排序的项目中提取排序键(即应该比较的部分)。

由于您不想对字符串进行排序,因此第二个参数应该是().

您的案例中的排序键是每个约会的开始日期和时间。所以你需要提供一个函数项,给定一个Appointment节点,返回xs:dateTime它的开始日期:function($app) { fn:dateTime($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>
于 2018-03-29T16:01:24.397 回答