3.4.3 XSL Function Extensions

The following examples demonstrate the use of a selection of function extensions. XSL function extensions are specified in section 2.4.3.

The following example XML is used as the target for the given XSL snippets in the first four examples:

SampleXML1.xml

 <my:myFields>
       <my:group1>
             <my:group2>
                   <my:Values>6</my:Values>
             </my:group2>
             <my:group2>
                   <my:Values>12</my:Values>
             </my:group2>
 <my:group2>
                   <my:Values>15</my:Values>
             </my:group2>
 </my:group1>
       <my:Average>11</my:Average>
       <my:Maximum>15</my:Maximum>
       <my:Minimum>6</my:Minimum>
       <my:Date>2008-02-04</my:Date>
       <my:Seconds>3</my:Seconds>
       <my:ZipCode>98052x</my:ZipCode>
 </my:myFields>

1. Conditional formatting with xdMath:Avg

xdMath:Avg can be used to conditionally change the style attribute of an HTML tag.

 <xsl:attributename="style">WIDTH: 130px;
     <xsl:choose>
         <xsl:when test="my:Average = xdMath:Avg(my:group1/my:group2)">FONT-WEIGHT: bold</xsl:when>
     </xsl:choose>
 </xsl:attribute>

The xdMath:Avg function is used to calculate the average of the values in the my:group2 nodes, which is 6+12+15 / 3 = 11. Because this value is equal to the value of the my:Average node, the test evaluates to true and xsl:when outputs FONT-WEIGHT: bold and makes the font bold.

2. Conditional formatting with xdDate:AddDays, xdDate:Today

This example is similar to the first example in that it shows how to perform conditional formatting using XSL function extensions.

 <xsl:attributename="style">
     <xsl:choose>
         <xsl:when test = "my:Date = xdDate:AddDays(xdDate:Today() , 3)">FONT-WEIGHT: bold; COLOR: #ff0000
         </xsl:when>
     </xsl:choose>
 </xsl:attribute>

The xsl:when clause is used to test if the date in the my:Date node is three days past today. xdDate:Today() is used to get today’s date and the output is passed in to the AddDays function. AddDays adds three days to today’s date and outputs the date three days past today. In the SampleXML1.xml snippet, my:Date is "2008-02-04". If today is 2008-02-01, test evaluates to true and the style attribute has the value "FONT-WEIGHT: bold; COLOR: #ff0000".

3. Outputting time with xdDate:AddSeconds

 <xsl:value-of select="xdDate:AddSeconds(xdDate:Now(), my:Seconds)"/>

The xsl:value-of clause outputs the value returned by the expression in the select attribute. xdDate:AddSeconds returns the time equivalent to now plus the number of seconds indicated in my:Seconds. Xsl:value-of outputs the resulting time.

4. Conditional formatting with xdUtil:Match

 <spanclass="xdTextBox" hideFocus="1" title="" xd:CtrlId="CTRL12" xd:xctname="PlainText" tabIndex="0" xd:binding="my:ZipCode">
     <xsl:if test="function-available('xdXDocument:GetDOM')">
         <xsl:attributename="style">
             <xsl:choose>
                 <xsl:when test = "not( xdUtil:Match( string(my:ZipCode), &quot;\d\d\d\d\d&quot; ) )">COLOR: #ff0000; TEXT-DECORATION: line-through
                 </xsl:when>             
             </xsl:choose>
             </xsl:attribute>
          <xsl:value-of select="my:ZipCode"/>
     </xsl:if>
 </span>

In this example, the XSL representation of a text box control is given. This textbox is used to enter zip codes. To make users aware of the case where they enter an invalid zip code, the xdUtil:Match function is used to check that the entered value conforms to the zip code pattern. The first parameter of the xdUtil:Match function takes the value of the my:ZipCode node. The second parameter is the regular expression "\d\d\d\d\d", which means five consecutive digits. If the given zip code is not composed of five digits, the textbox has the style "COLOR: #ff0000; TEXT-DECORATION: line-through", which makes it more visible to the user that the entered zip code is not valid. In SampleXML1.xml, my:ZipCode is "98052x", which does not match the given regular expression. Therefore, test evaluates to TRUE and the text box control’s style attribute has the given style attributes.

The following example XML is used as the target for the given XSL snippets in the next example.

SampleXML2.xml

 <root>
       <value>12</value>
       <value>14</value>
       <value>20</value>
 </root>.

1. Getting data from a secondary data source using xdXDocument:GetDOM

This example uses SampleXML2.xml as the secondary data source of a form template (.xsn) file. This secondary data source is registered with a data source name "example". Data is retrieved from this data source for use within the XSL.

The example makes the font of a text box control bold if the average of the values in the secondary data source is less than 15.

 <spanclass="xdTextBox"hideFocus="1"title=""tabIndex="0"xd:binding="my:field2"xd:xctname="PlainText"xd:CtrlId="CTRL3">
     <xsl:if test="function-available('xdXDocument:GetDOM')">
         <xsl:attribute name="style">WIDTH: 130px;
             <xsl:choose>
                 <xsl:when test="xdMath:Avg( xdXDocument:getDOM(&quot;example&quot;)/root/value )">FONT-WEIGHT: bold; caption: Conditional Formatting 1
 </xsl:when>
             </xsl:choose>
         </xsl:attribute>
         <xsl:value-ofselect="my:field2"/>
     </xsl:if>
 </span>

In the preceding XSL snippet, secondary data source content is queried using xdXDocument:getDom and then an XPath expression is built on it. This XPath expression returns a node-set containing all the value nodes in the "example" data source. Then xdMath:Avg is used to calculate the average.

The following example XML is used as the target for the given XSL snippets in the next example.

SampleXML3.xml

 <my:myFields>
       <my:group1>
             <my:group2>
                   <my:Count>3</my:Count>
                   <my:Value>4</my:Value>
                   <my:Total>12</my:Total>
             </my:group2>
             <my:group2>
                   <my:Count>12</my:Count>
                   <my:Value>20</my:Value>
                   <my:Total>240</my:Total>
             </my:group2>
             <my:group2>
                   <my:Count>4</my:Count>
                   <my:Value>8</my:Value>
                   <my:Total>12</my:Total>
             </my:group2>
       </my:group1>
 </my:myFields>

1. Using xdMath:Eval

 <span class="xdExpressionBox xdDataBindingUI" title="" xd:CtrlId="CTRL9" xd:xctname="ExpressionBox" tabIndex="-1" xd:disableEditing="yes" style="WIDTH: 145px">
 <xsl:if test="function-available('xdXDocument:GetDOM')">
 <xsl:value-of select="sum(xdMath:Eval(my:group1/my:group2, &quot;xdMath:Nz(my:Count) * xdMath:Nz(my:Value)&quot;))"/>
     </xsl:if>
 </span>

This XSL example is an expression box control. The value of the expression box is calculated from SampleXML3.xml using xdMath:Eval. The XPath expression passed to xdMath:Eval returns a node-set containing all the my:group2 nodes under my:group1. The my:group2 nodes contain the child elements my:Count and my:Value. The expression passed in as the second parameter to the xdMath:Eval calculates the sum of the multiplication of the nodes from the first parameter. xdMath:Eval calculates this multiplication for every group2 node and returns the result as a node-set.

Count

Value

3

4

12

20

4

8

The output is 3*4 + 12 * 20 + 4*8 = 284.