mli*_*ner 3 math xslt duration date
我使用一些代码使用XSLT 2.0从另一个日期中减去一个日期:
<xsl:template match="moveInDate">
<xsl:value-of select="current-date() - xs:date(.)"/>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)
这是有效的,但它让我得到P2243D的答案,我假设它对应于"2243天的时期"(这在数学方面是正确的).
由于我只需要天数,而不是P和D,我知道我可以使用子串或类似的东西,但作为XSLT的新手,我很好奇是否有更好,更优雅的方式来做到这一点简单的字符串操作
您可以简单地使用fn:days-from-duration()以获取持续时间xs:integer:
days-from-duration($arg as xs:duration?)如xs:integer?返回
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>
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助!
编辑:您也可以按照您说的方式进行子串处理.但正如你所指出的那样,它并不是首选.如果由于某种原因想要做类似的事情,你需要考虑数据类型.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>
Run Code Online (Sandbox Code Playgroud)