[英]How do you convert an XSLT 2.0 date duration to a string?
I am using some code to subtract one date from another using XSLT 2.0: 我使用一些代码使用XSLT 2.0从另一个日期中减去一个日期:
<xsl:template match="moveInDate">
<xsl:value-of select="current-date() - xs:date(.)"/>
</xsl:template>
This works, however it leaves me with an answer of P2243D, which I assume corresponds to a "Period of 2243 Days" (which is correct in terms of the math). 这是有效的,但它让我得到P2243D的答案,我假设它对应于“2243天的时期” (这在数学方面是正确的)。
Since I only need the number of days, not the P and the D, I know I could use substring or something similar, but as a newbie to XSLT, I'm curious if there is a better, more elegant way to do this than simple string manipulation. 由于我只需要天数,而不是P和D,我知道我可以使用子串或类似的东西,但作为XSLT的新手,我很好奇是否有更好,更优雅的方式来做到这一点简单的字符串操作
You could simply use fn:days-from-duration()
to get the duration as a xs:integer
: 您可以简单地使用
fn:days-from-duration()
来将持续时间作为xs:integer
:
days-from-duration($arg as xs:duration?)
asxs:integer?
days-from-duration($arg as xs:duration?)
为xs:integer?
Returns an
xs:integer
representing the days component in the canonical lexical representation of the value of$arg
.返回一个
xs:integer
表示$arg
值的规范词法表示中的days组件。 The result may be negative.结果可能是负面的。
See the XQuery 1.0 and XPath 2.0 Functions and Operators specification for more information. 有关更多信息,请参阅XQuery 1.0和XPath 2.0函数和运算符规范。
In your case: 在你的情况下:
<xsl:template match="moveInDate">
<xsl:value-of select="days-from-duration(current-date() - xs:date(.))"/>
</xsl:template>
Hope this helps! 希望这可以帮助!
EDIT: You could also do it the way you say, with substring processing. 编辑:您也可以按照您说的方式进行子串处理。 But as you point out, it's not prefered.
但正如你所指出的那样,它并不是首选。 If you for some reason would like to do something similar you need to think of the data types.
如果由于某种原因想要做类似的事情,你需要考虑数据类型。 The result of
current-date() - xs:date(.)
is returned as xs:duration
which cannot be processed by the substring functions without being casted: 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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.