#### <xsl:if>

Sometimes we need conditional processing rules. We might want create a list of sender and recipients with a defined value for the attribute id. In the given example this is only valid for the (unique) sender and the recipient <to id="eve">Eve Intruder</to>. We assume this set of persons shall be inserted into a relational database table Customer consisting of two NOT NULL columns id an name. Thus both attributes must be specified and we must exclude <from> or <to> nodes with undefined id attributes:

 Define a file local variable newline. Dealing with text output frequently requires the insertion of newlines. Due to the syntax of the xsl:text elements this tends to clutter the code. Iterate over the set of the sender node and all recipient nodes. The attribute value of test will be evaluated as a boolean. In this example it evaluates to true if the attribute id is defined for the context node. Since we are inside the xsl:for-each block all context nodes are either of type  or  and thus may have an id attribute. The id attributes value is copied to the output. The “@” character in select="@id" tells the XSL processor to read the value of an attribute with name id rather then the content of a nested subelement like in I am nested!. As stated earlier the dot “.” denotes the current context element. In this example simply the #PCDATA content is copied to the output. The “\$” sign in front of newline tells the XSL processor to access the variable newline previously defined in ❶ rather then interpreting it as the name of a sub element or an attribute.

As expected the recipient entry Adam Hacker does not appear due to the fact that no id attribute is defined in its <to> element:

INSERT INTO Customer (id, name) VALUES ('goik', 'Martin Goik')
INSERT INTO Customer (id, name) VALUES ('eve', 'Eve intruder')

No. 53

##### The XPath functions position() and last()
 Q: Revisiting our recipient list in Figure 955, “List of recipient nodes iteration. ” we are interested avoiding the trailing comma: Adam Hacker,Eve Intruder, We may use a xsl:if to insert a comma for all but the very last recipient node. This can be achieved by using the XSL functions position() and last(). Hint: The arithmetic operator “<” may be used in XSL to compare two integer numbers. However it must be escaped as < in order to XML markup clashes. A: We have to exclude the comma for the last node of the recipient list. If we have e.g. 10 recipients the function position() will return values integer values starting at 1 and ending with 10. So for the last node the comparison 10 < 10 will evaluate to false:  ,  Avoiding xsl:if In Figure 958, “Exporting SQL statements. ” we used the XPath value “from|to” to select the desired sender and recipient nodes. Inside the xsl:for-each block we permitted only those nodes which have an id attribute. These two steps may be combined into a single XPath expression obsoleting the xsl:if. A: We simply need a modified XPath in the for-each:  INSERT INTO Customer (id, name) VALUES (' ', ' ')