繁体   English   中英

如何将XSLT 2。0日期持续时间转换为字符串?

[英]How do you convert an XSLT 2.0 date duration to a string?

我使用一些代码使用XSLT 2.0从另一个日期中减去一个日期:

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

这是有效的,但它让我得到P2243D的答案,我假设它对应于“2243天的时期” (这在数学方面是正确的)。

由于我只需要天数,而不是P和D,我知道我可以使用子串或类似的东西,但作为XSLT的新手,我很好奇是否有更好,更优雅的方式来做到这一点简单的字符串操作

您可以简单地使用fn:days-from-duration()来将持续时间作为xs:integer

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

返回一个xs:integer表示$arg值的规范词法表示中的days组件。 结果可能是负面的。

有关更多信息,请参阅XQuery 1.0和XPath 2.0函数和运算符规范。

在你的情况下:

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

希望这可以帮助!

编辑:您也可以按照您说的方式进行子串处理。 但正如你所指出的那样,它并不是首选。 如果由于某种原因想要做类似的事情,你需要考虑数据类型。 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM