简体   繁体   English

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

[英]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?) as xs: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.

相关问题 如果将Java Long转换为另一个基数,如何从转换后的String中取回它? - If you convert a Java Long to another base, how do you get it back from the String it was converted to? 如何将任何整数值转换为 2 位文本字符串 - How do you convert any whole number value into a 2-digit text string 在 Java 中,如何将十进制数转换为基数 36? - In Java how do you convert a decimal number to base 36? 在Java中如何将基数36转换为十进制数? - In Java how do you convert a base 36 number to a decimal number? 如何将欧拉角转换为 Python 中的轴角表示? - How do you convert Euler angles to the Axis Angle representation in Python? 如何更改jQuery动画的持续时间? - How do I change the duration of a jQuery animation? 你如何编写一个程序来将度数从java中的列表转换为弧度 - How do you write a program to convert degrees to radians from a list in java 当最后一个十进制不是5时,如何从二进制转换为IEEE754? - How do you convert from binary to IEEE754 when the last decimal isn't 5? 如何将二进制 0001,0010,0100,1000 转换为 0,1,2,3 以索引 4x4 二维数组? - How do you convert binary 0001,0010,0100,1000 to 0,1,2,3 to index a 4x4 2D array? PHP - 拆分丑陋的日期字符串,然后对它进行日期数学运算? - PHP - Split ugly date string and then do date maths on it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM