需要从输入中获取日期前一天 xml

Need to get the day before date from the Input xml

需要获取特定属性值的日期前一天,并替换为输入中提到的日期 xml

输入XML我有,<Date>需要更改前一天Cost:Date:date4

<section>
<Plans>
<Date>5/1/2021</Date>
</Plans>
    
<p>date: <keyword keyref="Cost:Date:date4"/></p>
</section>

XSL 我已经尝试过以上 xml:

<xsl:template match="keyword[contains(@keyref, 'Cost:Date:date4')]">
        <xsl:param name="section" as="element()" tunnel="yes">
            <empty/>
        </xsl:param>
        <keyword keyref="Cost:Date:date4">
            <xsl:call-template name="format_variable">
                <xsl:with-param name="cur_keyref" select="@keyref"/>
                <xsl:with-param name="cur_value"
                    select="$section//Plans/Date"/>
                <xsl:with-param name="cur_format" select="'date4'"/>
            </xsl:call-template>
        </keyword>
    </xsl:template>
    
          <xsl:template name="format_variable">
        <xsl:param name="cur_keyref" as="xs:string">MISSING</xsl:param>
        <xsl:param name="cur_value" as="xs:string">MISSING</xsl:param>
        <xsl:param name="cur_format" as="xs:string">MISSING</xsl:param>
        <xsl:choose>
            <xsl:when test="$cur_format = 'date4'">
                <xsl:variable name="date_pieces" select="tokenize($cur_value, '/')"/>
                <xsl:variable name="month" select="$date_pieces[1]"/>
                <xsl:variable name="day" select="$date_pieces[2]"/>
                <xsl:variable name="year" select="$date_pieces[3]"/>
                <xsl:choose>
                    <xsl:when test="$month = '1'">
                        <xsl:text>January</xsl:text>
                    </xsl:when>
                    <xsl:when test="$month = '2'">
                        <xsl:text>February</xsl:text>
                    </xsl:when>
                    <xsl:when test="$month = '3'">
                        <xsl:text>March</xsl:text>
                    </xsl:when>
                    <xsl:when test="$month = '4'">
                        <xsl:text>April</xsl:text>
                    </xsl:when>
                    <xsl:when test="$month = '5'">
                        <xsl:text>May</xsl:text>
                    </xsl:when>
                    <xsl:when test="$month = '6'">
                        <xsl:text>June</xsl:text>
                    </xsl:when>
                    <xsl:when test="$month = '7'">
                        <xsl:text>July</xsl:text>
                    </xsl:when>
                    <xsl:when test="$month = '8'">
                        <xsl:text>August</xsl:text>
                    </xsl:when>
                    <xsl:when test="$month = '9'">
                        <xsl:text>September</xsl:text>
                    </xsl:when>
                    <xsl:when test="$month = '10'">
                        <xsl:text>October</xsl:text>
                    </xsl:when>
                    <xsl:when test="$month = '11'">
                        <xsl:text>November</xsl:text>
                    </xsl:when>
                    <xsl:when test="$month = '12'">
                        <xsl:text>December</xsl:text>
                    </xsl:when>
                </xsl:choose>
                
                <xsl:variable name="day_before" select="concat(' ', $day, ', ', $year)"/>
                <xsl:value-of select="xs:dayTimeDuration($day_before) - xs:dayTimeDuration('P1D')"/>
            </xsl:when>
            </xsl:choose>
            </xsl:template>

之前我使用过 <xsl:value-of select="$day_before - xs:dayTimeDuration('P1D')"/>,但我遇到了 xslt 2.0

的算术错误

预期输出为:

Date: April 31, 2021

但是当我 运行 时,我收到错误 Invalid duration value ' 1, 2021' (missing 'P')。请帮我解决这个问题。我正在使用 XLST 2.0

要将您的输入格式转换为 XSLT/XPath xs:date,您可以使用字符串操作和数字格式,然后您可以减去 xs:dayTimeDuration 并获得新的 xs:date :

let $components := tokenize('5/1/2021', '/')!xs:integer(.),
    $date := xs:date($components[3] || '-' || format-integer($components[1], '00') || '-' || format-integer($components[2], '00')),
    $oneDay := xs:dayTimeDuration('P1D')
return $date - $oneDay

这会给出例如2021-04-30,要对其进行格式化,请使用 format-date,例如format-date($date - $oneDay, '[MNn] [D01], [Y0000]') 这给出了例如April 30, 2021.

至于更广泛的背景,我想你想要例如

  <xsl:template match="keyword[contains(@keyref, 'Cost:Date:date4')]">
      <xsl:copy>
          <xsl:copy-of select="@*"/>
          <xsl:value-of
            select="let $components := tokenize(ancestor::section/Plans/Date, '/')!xs:integer(.),
    $date := xs:date($components[3] || '-' || format-integer($components[1], '00') || '-' || format-integer($components[2], '00')),
    $oneDay := xs:dayTimeDuration('P1D')
return format-date($date - $oneDay, '[MNn] [D01], [Y0000]')"/>
      </xsl:copy>
  </xsl:template>