在XSLT 1.0中如何使用concat()函数并添加分隔符

4

我正在尝试将月/日/年元素中的字符串连接成一个显示为MM/DD/YYYY的单个值,但我无法找到在xslt 1.0中执行此操作的方法,该方法将包括“/”分隔符,就像xslt 2.0中的string-join函数一样。 我需要在不创建新模板或使用变量/ if逻辑的情况下完成此操作,因为我们在我的课程中还没有“学习”这个。我试图连接的代码部分如下:

<publishedDate>
<month>7</month>
<day>9</day>
<year>2007</year>
</publishedDate>

目前我能做到的最好的是:
<xsl:value-of select="concat(
format-number(publishedDate/month, '##00', 'date'),
format-number(publishedDate/day, '##00', 'date'),
format-number(publishedDate/year, '####', 'date')
)"/>

将日期输出为这种格式:03082014

目前为止,为了完成任务,我被迫使用一个丑陋而冗长的解决方法,看起来像这样:

<xsl:value-of select="format-number(publishedDate/month, '##00', 'date')"/>/
<xsl:value-of select="format-number(publishedDate/day, '##00', 'date')" />/
<xsl:value-of select="format-number(publishedDate/year, '####', 'date')" />

并正确输出(即 2014 年 3 月 8 日)。你们知道通过使用 1.0 函数的方法来获取这个输出吗?谢谢!


你已经快完成了。你只需要在concat函数中添加'/'即可:concat(format-number(...), '/', format-number(...), '/', format-number(...)) - helderdarocha
2个回答

5

您已经接近成功了。您只需要在 concat() 本身中添加包含'/'的额外参数(仍然是XSLT 1.0-您可以有超过三个术语):

concat(format-number(...), '/', format-number(...), '/', format-number(...))

2
XPath 2.0(包含在XSLT 2.0中)将支持使用string-join($sequence, $seperator)通用解决方案
string-join((
    format-number(publishedDate/month, '##00', 'date'),
    format-number(publishedDate/day, '##00', 'date'),
    format-number(publishedDate/year, '####', 'date')
  ), '/')

这对于连接任意长度的序列尤其重要,而这在XPath 1.0中是不可能的。

如果您只想组合一定数量的字符串(年/月/日),则可以使用XPath 1.0/XSLT 1.0提供的concat(...)函数:

concat(
  format-number(publishedDate/month, '##00', 'date'),
  '/',
  format-number(publishedDate/day, '##00', 'date'),
  '/',
  format-number(publishedDate/year, '####', 'date')
)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接