Reputation: 119
I'm trying to concatenate strings in month/date/year elements into a single value that displays MM/DD/YYYY, but I can't find a way to do it in xslt 1.0 that will include the '/' separator in the way a string-join function would in xslt 2.0. I need to do this without creating new templates or using variables/if-logic because we haven't "learned" that yet in my class. The section of code I'm trying to concatenate looks like this:
<publishedDate>
<month>7</month>
<day>9</day>
<year>2007</year>
</publishedDate>
Currently the best I can do is:
<xsl:value-of select="concat(
format-number(publishedDate/month, '##00', 'date'),
format-number(publishedDate/day, '##00', 'date'),
format-number(publishedDate/year, '####', 'date')
)"/>
Which outputs dates like this: 03082014
In the mean time, for the purposed of the assignment, I'm forced to use a hideous, lengthy workaround that looks like this:
<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')" />
And outputs correctly (i.e. 03/08/2014). Do you guys know a way to get this output by using a a 1.0 function? Thanks!
Upvotes: 4
Views: 10129
Reputation: 23637
You're almost there. You just need to add extra parameters containing '/'
in the concat()
itself (it's still XSLT 1.0 - you can have more than three terms):
concat(format-number(...), '/', format-number(...), '/', format-number(...))
Upvotes: 5
Reputation: 38682
XPath 2.0 (included in XSLT 2.0) would have support for a general solution using string-join($sequence, $seperator)
:
string-join((
format-number(publishedDate/month, '##00', 'date'),
format-number(publishedDate/day, '##00', 'date'),
format-number(publishedDate/year, '####', 'date')
), '/')
This is especially important for joining arbitrary-length sequences, which is not possible in XPath 1.0.
As you only want to combine a fixed number of strings (year/month/day), using concat(...)
provided by XPath 1.0/XSLT 1.0 is totally fine:
concat(
format-number(publishedDate/month, '##00', 'date'),
'/',
format-number(publishedDate/day, '##00', 'date'),
'/',
format-number(publishedDate/year, '####', 'date')
)
Upvotes: 2