Re: [xsl] Re: "if-condition-return something" idiom : is it possible in XSLT 1 ?

Subject: Re: [xsl] Re: "if-condition-return something" idiom : is it possible in XSLT 1 ?
From: "Kerry, Richard richard.kerry@xxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 17 Jun 2014 15:28:09 -0000
"Doing string-manipulations in XSLT 1.0 is painful"
Yes it was rather.  I think I've managed it to my satisfaction, though.  I'll
see if I can send something to the list shortly.

"My preferred solution would be to move to XSLT 2.0+" "There is a DocBook XSLT
2.0 stylesheet available "
Not up to date with the XSLT 1 version, I believe.

"The code in your original post will return a concatenation of each
if-statement that is true, and the last xsl:value-of will be returned always"
Thank you.  That was very useful to know.  I had suspected it wouldn't work
but wondered what the exact result would be.

"not what you want, given the C-style example at the top of your post"
Indeednot.

"you need to use xsl:choose if you have to stick to XSLT 1.0"
Subject to issues about 'sequential' actions and when the whens get tested.

"let matching templates do the heavy lifting for you"
As the data I need to process is all within one string I'm not sure if I can.
At least not without doing something that might be much more cryptic than a
bit of rather ugly sequential XSLT string processing.

"highest-rated solution here "
What I've got is a bit like that.  I must look at whether 'concat' is better
than just a load of value-of and text nodes.

"1. Change the input date-format into a generic date-format"
I'm unlikely to do this.  My input documents have dates in a small number of
formats.  I expect to convert the originals (Word documents) without
attempting to change the dates as found.

"2. Write one named template for parsing the generic date-format into the
format you are after"
I now have two named templates, one for each of the fundamental formats I have
found - ie numeric-with-slashes and numbers-and-strings.  ("dd/mm/yy" etc and
"dd Month yy" etc).  Outputs are all yyyy-mm-dd.

"Alternatively, if you don't mind writing extension functions"
Not in principle, but ...
"you can take ... example for date-parsing, ... a Java commandlet"
I'd need to learn some Java, but as it's nearly the same as C++ that shouldn't
be too difficult ;-)
"It could save you a whole lot of trouble writing it in XSLT 1.0 yourself"
Already done.


Appreciatively,
Richard.


________________________________________
From: Abel Braaksma (Exselt) abel@xxxxxxxxxx
[xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx]
Sent: 16 June 2014 17:26
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] Re: "if-condition-return something" idiom : is it possible
in XSLT 1 ?

Doing string-manipulations in XSLT 1.0 is painful, the lack of regular
expressions, splitting, sequences and type-safety makes it quite to very
hard to do. The toolset in 1.0 is limited to substring-after/before,
translate and concat. My preferred solution would be to move to XSLT
2.0+ where you can do the C-style if-statements simply in XPath itself:
if(condition) then truepart else falsepart. There is a DocBook XSLT 2.0
stylesheet available which may help you:
https://github.com/docbook/xslt20-stylesheets, but I'm not sure your
situation allows making this move.

The code in your original post will return a concatenation of each
if-statement that is true, and the last xsl:value-of will be returned
always, which is not what you want, given the C-style example at the top
of your post. Others have already commented that you need to use
xsl:choose if you have to stick to XSLT 1.0, or let matching templates
do the heavy lifting for you.

Because you are trying to parse dates, where the input is of different
formats, maybe the highest-rated solution here helps you out in the
right direction:
http://stackoverflow.com/questions/17079954/convert-date-time-format-in-xslt.
It does not cover all your situations, but typically I would approach
your problem by:

1. Change the input date-format into a generic date-format
2. Write one named template for parsing the generic date-format into the
format you are after

This has the advantage that it is easy to maintain. If you want to
change the output format, all you need to do is change the way the
generic date-format is formatted. If you ware going to use XSLT 2.0,
instead of a generic self-defined format, you can simply use xs:date or
xs:dateTime, and use formatDate or formatDateTime for the formatting.

If I had the time I could help you expand the XSLT 1.0 example in the
aforementioned link, it would surely benefit the community to have a
generic date-time parsing template.

Alternatively, if you don't mind writing extension functions, you can
take Clark C. Evans's example for date-parsing, he uses a Java
commandlet in a thread of 15 years ago:
http://www.stylusstudio.com/xsllist/199910/post70080.html, which might
just work with Saxon 6.5 or other Java based processors. It could save
you a whole lot of trouble writing it in XSLT 1.0 yourself.

Cheers,

Abel Braaksma
Exselt XSLT 3.0 streaming processor
http://exselt.net

Current Thread