xml processing: if elseif else / switch case
Hi, I was wondering whether we have tests similar to <if-else if-else> or <switch case> in context's xml processing. (And even in context more general...) I have found these doifelse tests, but elseif doesn't seem to exist. Is that correct? If yes, could that be added? Of course, you can always just nest doifelse-constructs, but that will become ugly quickly. Being able to specify multiple branches would be handy. Best, Denis
I believe \orelse might be what you want. It works with most \if... macros,
and it avoids nesting of \doifelse constructs. Here's an example, which I
use in conditionally removing preceding punctuation if any other keys are
defined before a specific one in an assignment:
```
% If any of the preceding keys was specified, then replace any
preceding punctuation with a space
% and typeset the number with its CSL abbreviation, followed by a
comma
\ifdefined\btxsbllocvol
\removeunwantedspaces\removepunctuation\btxspace
\orelse\ifdefined\btxsbllocno
\removeunwantedspaces\removepunctuation\btxspace
\fi
\orelse\ifdefined\btxsbllocpt
\removeunwantedspaces\removepunctuation\btxspace
\fi
\orelse\ifdefined\btxsbllocp
\removeunwantedspaces\removepunctuation\btxspace
\fi
\orelse\ifdefined\btxsbllocn
\removeunwantedspaces\removepunctuation\btxspace
\fi
\orelse\ifdefined\btxsbllocfig
\removeunwantedspaces\removepunctuation\btxspace
\fi
\orelse\ifdefined\btxsbllocop
\removeunwantedspaces\removepunctuation\btxspace
\fi
\orelse\ifdefined\btxsbllocbk
\removeunwantedspaces\removepunctuation\btxspace
\fi
\orelse\ifdefined\btxsbllocep
\removeunwantedspaces\removepunctuation\btxspace
\fi
\orelse\ifdefined\btxsbllocchap
\removeunwantedspaces\removepunctuation\btxspace
\fi
\orelse\ifdefined\btxsbllocv
\removeunwantedspaces\removepunctuation\btxspace
\fi
\orelse\ifdefined\btxsbllocfol
\removeunwantedspaces\removepunctuation\btxspace
\fi
\orelse\ifdefined\btxsbllocfrag
\removeunwantedspaces\removepunctuation\btxspace
\fi
\orelse\ifdefined\btxsbllocpl
\removeunwantedspaces\removepunctuation\btxspace
\fi
\orelse\ifdefined\btxsblloccol
\removeunwantedspaces\removepunctuation\btxspace
\fi
\orelse\ifdefined\btxsbllocl
\removeunwantedspaces\removepunctuation\btxspace
\fi
\orelse\ifdefined\btxsbllocsv
\removeunwantedspaces\removepunctuation\btxspace
\fi
\orelse\ifdefined\btxsbllocsec
\removeunwantedspaces\removepunctuation\btxspace
\fi
\fi
```
Joey
On Thu, Dec 9, 2021, 3:19 AM Denis Maier via ntg-context
Hi,
I was wondering whether we have tests similar to «if-else if-else» or «switch case» in context’s xml processing. (And even in context more general...) I have found these doifelse tests, but elseif doesn’t seem to exist. Is that correct? If yes, could that be added? Of course, you can always just nest doifelse-constructs, but that will become ugly quickly. Being able to specify multiple branches would be handy.
Best,
Denis
___________________________________________________________________________________ If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context webpage : http://www.pragma-ade.nl / http://context.aanhet.net archive : https://bitbucket.org/phg/context-mirror/commits/ wiki : http://contextgarden.net
___________________________________________________________________________________
Joey McCollum via ntg-context schrieb am 09.12.2021 um 15:08:
I believe \orelse might be what you want. It works with most \if... macros, and it avoids nesting of \doifelse constructs. Here's an example, which I use in conditionally removing preceding punctuation if any other keys are defined before a specific one in an assignment:
``` % If any of the preceding keys was specified, then replace any preceding punctuation with a space % and typeset the number with its CSL abbreviation, followed by a comma \ifdefined\btxsbllocvol \removeunwantedspaces\removepunctuation\btxspace \orelse\ifdefined\btxsbllocno \removeunwantedspaces\removepunctuation\btxspace \fi \orelse\ifdefined\btxsbllocpt [...] \fi ```
The \fi at the end of each \orelse branch is wrong. \ifdefined\btxsbllocvol ... \orelse\ifdefined\btxsbllocno ... \else ... \fi Wolfgang
On 12/9/2021 5:11 PM, Wolfgang Schuster via ntg-context wrote:
Joey McCollum via ntg-context schrieb am 09.12.2021 um 15:08:
I believe \orelse might be what you want. It works with most \if... macros, and it avoids nesting of \doifelse constructs. Here's an example, which I use in conditionally removing preceding punctuation if any other keys are defined before a specific one in an assignment:
``` % If any of the preceding keys was specified, then replace any preceding punctuation with a space % and typeset the number with its CSL abbreviation, followed by a comma \ifdefined\btxsbllocvol \removeunwantedspaces\removepunctuation\btxspace \orelse\ifdefined\btxsbllocno \removeunwantedspaces\removepunctuation\btxspace \fi \orelse\ifdefined\btxsbllocpt [...] \fi ```
The \fi at the end of each \orelse branch is wrong.
\ifdefined\btxsbllocvol ... \orelse\ifdefined\btxsbllocno ... \else ... \fi and when 'string' comparison is needed one can do:
\starttext \def\foo{foo} \def\ofo{ofo} \def\oof{oof} \def\XXX{ofo} \ifcstok\XXX\foo FOO \orelse\ifcstok\XXX\ofo OFO \orelse\ifcstok\XXX\oof OOF \else XXX \fi \def\XXX{\ofo} \iftok{\XXX}{\foo}% FOO \orelse\iftok{\XXX}{\ofo}% OFO \orelse\iftok{\XXX}{\oof}% OOF \else XXX \fi \ifcstok{\XXX}\foo FOO \orelse\ifcstok{\XXX}\ofo OFO \orelse\ifcstok\XXX}\oof OOF \else XXX \fi \stoptext watch the subtle differences. It all makes coding in tex primitives a bit more visual appealing and readable. Oone of the objectives of lmtx is that users can read the context code without pondering too much about the syntax or getting depressed by possibly obscure helper macros and expansion trickery. Hans ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl -----------------------------------------------------------------
Thanks for correcting that, Wolfgang. I must have added the other \fi instances because I was trying to use \orelse in MKIV and couldn't get it to work. Is it only supported in LMTX? Joey On Thu, Dec 9, 2021 at 11:30 AM Hans Hagen via ntg-context < ntg-context@ntg.nl> wrote:
On 12/9/2021 5:11 PM, Wolfgang Schuster via ntg-context wrote:
Joey McCollum via ntg-context schrieb am 09.12.2021 um 15:08:
I believe \orelse might be what you want. It works with most \if... macros, and it avoids nesting of \doifelse constructs. Here's an example, which I use in conditionally removing preceding punctuation if any other keys are defined before a specific one in an assignment:
``` % If any of the preceding keys was specified, then replace any preceding punctuation with a space % and typeset the number with its CSL abbreviation, followed by a comma \ifdefined\btxsbllocvol \removeunwantedspaces\removepunctuation\btxspace \orelse\ifdefined\btxsbllocno \removeunwantedspaces\removepunctuation\btxspace \fi \orelse\ifdefined\btxsbllocpt [...] \fi ```
The \fi at the end of each \orelse branch is wrong.
\ifdefined\btxsbllocvol ... \orelse\ifdefined\btxsbllocno ... \else ... \fi and when 'string' comparison is needed one can do:
\starttext
\def\foo{foo} \def\ofo{ofo} \def\oof{oof}
\def\XXX{ofo}
\ifcstok\XXX\foo FOO \orelse\ifcstok\XXX\ofo OFO \orelse\ifcstok\XXX\oof OOF \else XXX \fi
\def\XXX{\ofo}
\iftok{\XXX}{\foo}% FOO \orelse\iftok{\XXX}{\ofo}% OFO \orelse\iftok{\XXX}{\oof}% OOF \else XXX \fi
\ifcstok{\XXX}\foo FOO \orelse\ifcstok{\XXX}\ofo OFO \orelse\ifcstok\XXX}\oof OOF \else XXX \fi
\stoptext
watch the subtle differences. It all makes coding in tex primitives a bit more visual appealing and readable. Oone of the objectives of lmtx is that users can read the context code without pondering too much about the syntax or getting depressed by possibly obscure helper macros and expansion trickery.
Hans
----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl -----------------------------------------------------------------
___________________________________________________________________________________ If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context webpage : http://www.pragma-ade.nl / http://context.aanhet.net archive : https://bitbucket.org/phg/context-mirror/commits/ wiki : http://contextgarden.net
___________________________________________________________________________________
On 12/9/2021 5:30 PM, Joey McCollum via ntg-context wrote:
Thanks for correcting that, Wolfgang. I must have added the other \fi instances because I was trying to use \orelse in MKIV and couldn't get it to work. Is it only supported in LMTX? yes, it's one of the native luametatex features (already there for a while)
Hans ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl -----------------------------------------------------------------
Thanks for all the answers. That's helpful. But I think that does not work with the xml tests, or does it? I mean, is this a general mechanism?
Denis
________________________________________
Von: ntg-context
Thanks for correcting that, Wolfgang. I must have added the other \fi instances because I was trying to use \orelse in MKIV and couldn't get it to work. Is it only supported in LMTX? yes, it's one of the native luametatex features (already there for a while)
Hans ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl ----------------------------------------------------------------- ___________________________________________________________________________________ If your question is of interest to others as well, please add an entry to the Wiki! maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context webpage : http://www.pragma-ade.nl / http://context.aanhet.net archive : https://bitbucket.org/phg/context-mirror/commits/ wiki : http://contextgarden.net ___________________________________________________________________________________
Denis Maier via ntg-context schrieb am 09.12.2021 um 18:24:
Thanks for all the answers. That's helpful. But I think that does not work with the xml tests, or does it? I mean, is this a general mechanism?
Since you didn't provide a example with your problem there is no way to tell how to improve it. Wolfgang
Well, it was more a question out of curiosity, not a specific problem that needs to be solved. (Or actually, I found another solution for the problem I've had first.) Anyway, I still think this would be handy as that would be a more maintainble control structure. I'll try to come up with an example next week... Denis
-----Ursprüngliche Nachricht----- Von: Wolfgang Schuster
Gesendet: Donnerstag, 9. Dezember 2021 18:32 An: mailing list for ConTeXt users ; Denis Maier via ntg- context Cc: Maier, Denis Christian (UB) Betreff: Re: [NTG-context] xml processing: if elseif else / switch case Denis Maier via ntg-context schrieb am 09.12.2021 um 18:24:
Thanks for all the answers. That's helpful. But I think that does not work with the xml tests, or does it? I mean, is this a general mechanism?
Since you didn't provide a example with your problem there is no way to tell how to improve it.
Wolfgang
participants (4)
-
denis.maier@unibe.ch
-
Hans Hagen
-
Joey McCollum
-
Wolfgang Schuster