How do you convert XSLT 2.0 date duration to string?

I use some code to subtract one date from another using XSLT 2.0:

<xsl:template match="moveInDate">
    <xsl:value-of select="current-date() - xs:date(.)"/>
</xsl:template>

This works, however, leaves me with the answer of P2243D, which, I believe, corresponds to the β€œ2243 day period” (which is true in mathematics).

Since I only need the number of days, not P and D, I know that I can use a substring or something similar, but as a newbie to XSLT, I am curious if there is a better, more elegant way to do this than simple manipulations with string.

+3
source share
1 answer

You can simply use fn:days-from-duration()to get the duration as xs:integer:

days-from-duration($arg as xs:duration?) as xs:integer?

a xs:integer, days $arg. .

. XQuery 1.0 XPath 2.0 .

:

<xsl:template match="moveInDate">
    <xsl:value-of select="days-from-duration(current-date() - xs:date(.))"/>
</xsl:template>

, !

EDIT: , , . , , . - - , . current-date() - xs:date(.) xs:duration, :

<xsl:template match="moveInDate">
  <xsl:variable name="dur" select="(current-date() - xs:date(.)) cast as xs:string"/>
  <xsl:value-of select="substring-before(substring-after($dur, 'P'), 'D')"/>
</xsl:template>
+7

Source: https://habr.com/ru/post/1757953/


All Articles