Assigning Property Values Conditionally

Top  Previous  Next

$vof() macro can also be used to assign values to the element properties conditionally. Before making an example for that lets add new nodes under the <employee> node as follows.

 

<?xml version="1.0" encoding="UTF-8"?>

<root>

<employee>

  <NAME>John Watson</NAME>

  <GENDER>M</GENDER>

  <AGE>24</AGE>

</employee>

</root>

 

Here is our example:

$vof(/employee/GENDER = 'M' ? 'Mr.' ; 'Ms.')

 
The result will be 'Mr.' if the gender of the employee is 'M' and 'Ms.' otherwise. Remember that $vof() is an Application Studio macro and it is expanded to the native XSL codes during compile time.

The Application Studio will expand the $vof(/employee/GENDER = 'M' ? 'Mr.' ; 'Ms.') definition to the following native XSL code.

 

<xsl:choose>

<xsl:when test="/root/employee/GENDER = 'M'"><xsl:value-of select="'Mr.'"/></xsl:when>

<xsl:otherwise><xsl:value-of select="'Ms.'"/></xsl:otherwise>

</xsl:choose>

 

Now, lets combine this with some others for a better example.

$vof(/employee/GENDER = 'M' ? 'Mr.' ; 'Ms.') $vof(/employee/NAME) is

 $vof(/employee/AGE < 25 ? 'very ')young.

 

Result:

Mr. John Watson is

 very young

 

The usages of the $vof() Function

The $vof() function can be used in many different ways. Here are some samples:

$vof(AGE)

Returns the value of the XML node AGE

$vof(AGE;DEFAULT_AGE)

Returns the value of the XML Node AGE. If there is no such node or the value of the node is blank then returns the value of the DEFAULT_AGE node.

$vof(AGE < 25 ? 'young')

Returns 'young' if the value of the XML node AGE is less than 25, otherwise returns blank.

$vof(GENDER = 'M' ? 'Mr.' ; 'Ms.')

Returns 'Mr.' if the value of the XML node GENDER node is 'M', 'Ms.' otherwise.

$vof(A>B?(A>C?'A';'C');(B>C?'B';'C'))

Prints the name of the XML Node whose value is the biggest.