You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

XSLT 2.0中如何用XPath获取下一个图表可用日期?

XSLT 2.0中用following-sibling获取下一个图表可用日期

我正在试用XSLT 2.0,遇到XPath相关问题,需要使用following-sibling。现有代码可正常运行,但无法显示图表的下一个可用日期。

当前输出:

Today is: 06/08/2023
Current Cycle Date: 05/18/2023
Time for new charts.

期望输出:

Today is: 06/08/2023
Current Cycle Date: 05/18/2023
Next new charts: 06/15/2023
Time for new charts.

我已预留注释行,计划用$dtSchedNext变量存储该日期,但不知道如何通过XPath填充,尝试following-sibling未成功,请求帮助。


使用命令

java -jar "C:\Program Files\Saxonica\SaxonJ HE 12.1\saxon-he-12.1.jar" -s:Schedule.xml -Thiscodebelow.xsl

XML文件(Schedule.xml)

<schedule>
  <cycle>
    <date>2023-05-18-05:00</date>
    <yc>2305</yc>
    <enr>n</enr>
  </cycle>
  <cycle>
    <date>2023-06-15-05:00</date>
    <yc>2306</yc>
    <enr>y</enr>
  </cycle>
  <cycle>
    <date>2023-07-13-05:00</date>
    <yc>2307</yc>
    <enr>n</enr>
  </cycle>
  <cycle>
    <date>2023-08-10-05:00</date>
    <yc>2308</yc>
    <enr>y</enr>
  </cycle>
</schedule>

原XSLT样式表(缺少下一个日期输出)

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xs="http://www.w3.org/2001/XMLSchema"
                version="2.0">
  <xsl:output method="text"/>
  <xsl:variable name="docSchedule" select="document('Schedule.xml')/schedule"/>
  <!-- Returns today's date formatted as '2023-02-05-05:00'  -->
  <xsl:variable name="dtToday" as="xs:date" select="xs:date(current-date())"/>
  <!-- Opens schedule.xml,sets context to children of the root 'schedule' -->
  <!--  and returns the latest 'cycle' node only -->
  <xsl:variable name="schedCycles" select="$docSchedule/*[date &lt; $dtToday]"/>
  <xsl:variable name="cycleLast" select="$schedCycles[position()=last()]"/>
  <!-- Extract the value of 'date' from the current cycle node -->
  <xsl:variable name="dtSched" as="xs:date" select="$cycleLast/date"/>
  <xsl:template match="/">
    <xsl:message terminate="no">Today is:
      <xsl:value-of select="format-date($dtToday,'[M01]/[D01]/[Y1]')"/>Current Cycle Date:
      <xsl:value-of select="format-date($dtSched,'[M01]/[D01]/[Y1]')"/>
      <!--  Next new charts`your text`:    <xsl:value-of select="format-date($dtSchedNext,'[M01]/[D01]/[Y1]')"/> -->
    </xsl:message>
    <xsl:choose>
      <xsl:when test="$dtToday gt $dtSched">
        <xsl:message terminate="no">
       Time for new charts.
        </xsl:message>
      </xsl:when>
      <xsl:when test="$dtToday lt $dtSched">
        <xsl:message terminate="no">
       Charts Are up to date.
        </xsl:message>
      </xsl:when>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>

解决方案

要获取下一个可用日期,基于$cycleLast节点使用following-sibling::cycle[1]定位紧邻的下一个cycle兄弟节点,提取其日期即可。

修改步骤

  1. 添加$dtSchedNext变量,通过following-sibling获取下一个周期日期:
    <xsl:variable name="dtSchedNext" as="xs:date" select="$cycleLast/following-sibling::cycle[1]/date"/>
    
  2. 取消注释预留的输出行,调整格式对齐:
    Next new charts:    <xsl:value-of select="format-date($dtSchedNext,'[M01]/[D01]/[Y1]')"/>
    

修改后的完整XSLT代码

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xs="http://www.w3.org/2001/XMLSchema"
                version="2.0">
  <xsl:output method="text"/>
  <xsl:variable name="docSchedule" select="document('Schedule.xml')/schedule"/>
  <!-- Returns today's date formatted as '2023-02-05-05:00'  -->
  <xsl:variable name="dtToday" as="xs:date" select="xs:date(current-date())"/>
  <!-- Opens schedule.xml,sets context to children of the root 'schedule' -->
  <!--  and returns the latest 'cycle' node only -->
  <xsl:variable name="schedCycles" select="$docSchedule/*[date &lt; $dtToday]"/>
  <xsl:variable name="cycleLast" select="$schedCycles[position()=last()]"/>
  <!-- Extract the value of 'date' from the current cycle node -->
  <xsl:variable name="dtSched" as="xs:date" select="$cycleLast/date"/>
  <!-- 获取下一个cycle的日期 -->
  <xsl:variable name="dtSchedNext" as="xs:date" select="$cycleLast/following-sibling::cycle[1]/date"/>
  <xsl:template match="/">
    <xsl:message terminate="no">Today is:           <xsl:value-of select="format-date($dtToday,'[M01]/[D01]/[Y1]')"/>
Current Cycle Date: <xsl:value-of select="format-date($dtSched,'[M01]/[D01]/[Y1]')"/>
Next new charts:    <xsl:value-of select="format-date($dtSchedNext,'[M01]/[D01]/[Y1]')"/>
    </xsl:message>
    <xsl:choose>
      <xsl:when test="$dtToday gt $dtSched">
        <xsl:message terminate="no">
       Time for new charts.
        </xsl:message>
      </xsl:when>
      <xsl:when test="$dtToday lt $dtSched">
        <xsl:message terminate="no">
       Charts Are up to date.
        </xsl:message>
      </xsl:when>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>

说明

  • following-sibling::cycle[1]会精准选中$cycleLast节点之后的第一个cycle兄弟节点,确保获取到紧邻的下一个周期日期。
  • 调整了输出的换行和空格,让格式与期望输出对齐,提升可读性。

内容的提问来源于stack exchange,提问作者Sim Tech

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 06:22:14