XSLT brain teaser

Thought I would share a little bug I had today. It proves 2 points…
Think simple (don’t over complicate things)
Use test first programming (in particular UTF-X if using XSLT)

Ok so here goes, I had a variable called address which could contain either “na”, “n/a” or a range of values that would determine how to address a person like “Mr”, “Mrs”, “Dr” etc. In the case where the address value was neither “na” or “n/a” the output should include the persons address (Mr, Mrs etc). Seems simple enough again right?

Here is the line I used to do the test

<xsl:when test="(not(contains($the_address, $n_forward_slash_a))) or (not(contains($the_address, $n_a)))">

This seems to make sense right we don’t want the name to be prefixed by either “n/a” or “na”. It is at this point I should mention that the above code did not work.

WHY?

The reason why the code did not work is because we’re using the not operator which negates the evaluation. I should have figured this out earlier (following my rule above about not over complicating things) but instead I got lost in the syntax of XSL trying all sorts of code rearranging to accomplish the same task. I was certain that it was the syntax and not the logic. This is where UTF-X came in. It enabled me to set up three separate tests using three different address values “n/a”, “na” and “Dr”. This reminds me of the game show brain teaser where stepping through the scenarios in a logical fashion gave the correct answer; trying to think using other methods like probability to solve the brain teaser just made it more confusing but, I digress.

The solution

<xsl:when test="(not(contains($the_address, $n_forward_slash_a))) and (not(contains($the_address, $n_a)))">

I solved the problem by simply adding an “and” instead of an “or”. Again the thing to remember here is that both test statements are negated by the not operator, I shudder to think how slow and complicated not only noticing this but solving this would have been without test first programming.
Example using n/a with the OR operator (1 is true 0 is false)

not n/a match not na match Outcome
0 1 1

Example using na with the OR operator (1 is true 0 is false)

not n/a match not na match Outcome
1 0 1

Example using n/a with the AND operator(1 is true 0 is false)

not n/a match not na match Outcome
0 1 0

Example using n/a with the AND operator (1 is true 0 is false)

not n/a match not na match Outcome
1 0 0


About these ads

One thought on “XSLT brain teaser

  1. After posting this it was suggested that I could wrap both statements in a single “not” operator and therefore use the “or” as initially intended. Thoughts?
    Will test that idea later to get a print out of the results.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s