【问题标题】:Using concat() with a separator in XSLT 1.0在 XSLT 1.0 中使用带有分隔符的 concat()
【发布时间】:2014-04-12 03:26:57
【问题描述】:

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

<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')" />

并正确输出(即 03/08/2014)。你们知道使用 1.0 函数获得此输出的方法吗?谢谢!

【问题讨论】:

  • 你快到了。您只需要在 concat 本身中添加 '/' 术语:concat(format-number(...), '/', format-number(...), '/', format-number(...))

标签: xml xslt xpath xslt-1.0


【解决方案1】:

你快到了。您只需要在 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')
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-04
      • 2017-06-29
      • 1970-01-01
      • 2016-04-23
      • 2012-12-01
      • 1970-01-01
      • 2018-03-28
      • 2015-07-17
      相关资源
      最近更新 更多