Custom Web Publishing Guide

Table Of Contents
Chapter 5
|
Developing FileMaker XSLT stylesheets 77
2. Define the extension component and extension functions, with the code that actually implements your
extension function.
<xalan:component prefix="fmp-ex" functions="getValueColor">
<xalan:script lang="javascript">
function getValueColor(value) {
if (value > 0)
return ("#009900");
else
return ("#CC0000");
}
</xalan:script>
</xalan:component>
This example returns a color value based on an input value. If the input value is greater than 0, the color
returned is green (
"#009900"); otherwise, if the value is less than 0, the color returned is red ("#CC0000").
Note The <xalan:component> element needs to be the child of <xsl:stylesheet> element.
3. Use the extension function inside the stylesheet.
The following examples show how to call an extension function using an XPath statement.
This first example would set the font color to green ("#009900").
<font color=”{fmp-ex:getValueColor(50)}”>The value is 50</font>
This second example would set the font color to red ("#CC0000").
<font color=”{fmp-ex:getValueColor(-500)}”>The value is -500</font>
An extension function example
The simple JavaScript function used in the process above could have been implemented using an
<xsl:choose> statement. But the real power of using a scripting extension is that you can create a function
that cannot be implemented in XSLT or XPath alone.
For example, say that you are building an intranet portal site for your company, and you want to include the
current stock price information on that portal page. While there are XML stock feeds available, generally
they require commercial licenses to access them. However, you can download stock data in a Comma
Separated Values (CSV) document format from Yahoo. The XPath
document() function can import content
from XML sources, but you would need to convert the CSV content into XML. One solution is to use
JavaScript to download the CSV stock price information, parse the file, and extract the data.
This URL shows the syntax for retrieving a stock quote from Yahoo as a CSV file:
http://quote.yahoo.com/d/quotes.csv?s=<ticker>&f=l1gh&e=.csv
where <ticker> represents the stock symbol that you are trying to retrieve data for.
The data returned is three numbers separated by commas, such as:
31.79,31.17,32.12
where the first value is the last trade price, the second value is the daily low, and the third value is the daily
high.