Log out?

XSLT Tutorial – Conditional Logic

Welcome back to the XCS eiConsole XSLT Data Transformation tutorial series. In this 10 minute tutorial we’ll further explore how conditional logic can be implemented in XSLT. This tutorial builds on concepts discussed in the introductory episode. By the end of this lesson you’ll be able to use the XSL if tag to test for a boolean condition, use the XSL choose, XSL when, and XSL otherwise structures to conditionally check for more than one condition, use the same three structures to emulate if/then/else logic, and conditionally add static text to the output document.

We’ll begin with the mapping that we created in the introduction tutorial. On the left hand side we have FishXML loaded from the FishXML schema. On our Target we have our SimpleFishList XML which we’d like to update to include a new element.


To do this, use the Open target format icon…


And choose the XML Format builder. Remove any XML files that are in the list, and then click Add.


We’ll choose SimpleFishList2. Click Open…


And then Read Format.


You’ll note that this version of the SimpleFishList is the same as before, but we’ve added a Comment. What we’ll do in this exercise is populate that Comment conditionally based on whether or not a Fish has a Family or Order in common with the Pilotfish.


The first thing we need to do is learn a little bit more about the Pilotfish, so let’s run a test transformation that will just output the Pilotfish Fish information. To do this, click on the test condition of the existing if.


Double click on that node. are nodes which have attributes and child nodes. generally they pretty interchangeable. using a good analogy you can think of it this way: node is like fruit an element apple. apple kind class=”lexicon-term”>node, select all of the exiting text and click Delete.


We’ll replace this with the new condition CommonName = ‘Pilotfish’ in single quotes. Then hit Enter.


Click on the Testing tab, and if its not loaded already, load the FishXML Source file, and then Execute.


Here we can see information about the Pilotfish – it’s Family, scientific name, and Order.


Let’s return to the mapping tab and add a condition. This condition will output only those Fish that are in the same Family as the Pilotfish. The first thing that we need to do here is remove our condition. To do this, we’ll first move the ListItem out from underneath the if Node. Click the ListItem node, and drag it until a gray bar appears between the if node and the select node of the for-each.


Once you’ve done this successfully, the ListItem should appear as a peer of the if node.


Now, select the if node, right click, and choose Delete.


Now, once again our transformation will give us a list including every Fish.


Next we want to add the Comment node to our mapping. Navigate to the Comment node in the Target tree and drop it on its parent, the ListItem. You’ll note that a new Comment node will appear between the name and classification.


Next we need to add our condition. Under the XSLT Structures tab choose the Flow Control sub-tab and drag the if node on top of the comment.


Once the blue if node appears, double click on the test attribute and enter in our desired condition – which in this case is Family = ‘Carangidae’. Once you’re done, hit Enter.


Now, every time the Family is set to that value we’ll want to output that this Fish is in the same order as the Pilotfish. To do this we’ll want to add a text node underneath our condition. Select the Output sub-tab of the XSLT Structures tab and choose the text tool. Drop it on the if node and a Text node should appear.


When we double click this we can enter in the text that we’d like to see: This fish is in the same family as the Pilotfish. Click Enter.


Once complete we can click on the Testing tab and run our transformation. We’ll see many of the comments are empty.


But some will have the comment populated: This fish is in the same family as the Pilotfish.


Let’s say we’d also like to add some text if the Fish is in the same Order as the Pilotfish. To do this, we can copy our existing if condition. Select the if node and choose Copy Mapping Component from the context menu.


Then, click on the Comment node under which we’d like to see the new if, right click and choose Paste Mapping component.


You’ll see a copy of the XSLT structure will appear.


Next, we’ll double click on the condition and add something similar, this time checking for the Order: Order = ‘Perciformes’.


Edit the Text to read: This fish is in the same order as the Pilotfish. Click Enter.


We’ll test and you can see some comments where we have Fish in the same Order as the Pilotfish…


And some where it’s both in the same Family and the same Order. Note, however, that this is redundant because we know that if a Fish is in the same Family, it’s going to be in the same Order.


So, to remedy this, let’s only output the more specific message. We’ll want to do this by using the equivalent of a switch or a case statement. The XSLT equivalent of these are the choose, when, and otherwise elements.


Select the choose tag and drag it on top of the Comment.


The choose will be the container for each of our different cases. We have two cases here, so we’ll drag two when conditions on top of the choose.


We’ll want the same tests that we had in the if, so we can copy and paste.


We’ll also want the same Text, so we can drag the existing Text node. are nodes which have attributes and child nodes. generally they pretty interchangeable. using a good analogy you can think of it this way: node is like fruit an element apple. apple kind class=”lexicon-term”>nodes out from underneath the if and on top of the corresponding whens.


Finally, we can delete the ifs.


Rerunning our transformation again, we can now see that we don’t get any duplicate messages. Something can be in the same Order or the same Family as the Pilotfish. However, if it’s in the same Family we don’t duplicate the message about the Order.


Lastly, let’s look at how we would emulate an else condition. The XSLT equivalent of an else is the otherwise tag. Take the otherwise tool and drag it onto the choose.


Next, navigate to the Output sub-tab and drag a text tool on top of the otherwise.


Double click that node and add the text that we’d like to use in all otherwise conditions: This fish doesn’t have much in common with the Pilotfish. Hit Enter.


Returning to the Testing Mode again, we can see now that every ListItem has a populated Comment.


This concludes our XSLT tutorial on the use of XSLT conditional structures, such as XSL if, XSL choose, XSL when, and XSL otherwise.

    to post comments

    This is a unique website which will require a more modern browser to work! Please upgrade today!

    Thanks!

    Our editors are notified.

    Close