7.4 XPath Functions

7.4 XPath Functions

XSL uses the XPath notations as listed in Chapter 1. This section lists some of the XPath 1.0 functions with descriptions that include similarities to FileMaker Pro functions. Example usage of the XPath functions with the XSL element <xsl:value-of> is presented. Not all XPath functions may be properly supported by the web browsers, so test them carefully. The functions are:

last()—This function has a numeric result. Most often used in the predicate of the XPath expression, this function returns the stringvalue of the last element: <xsl:value-of select="fm:ROW[last()]" />. This function is similar to the FileMaker Pro function last(repeatingOrRelatedField).

position()—This function has a numeric result. This function is also used in the XPath predicate. It returns the numeric position of the current node as the template goes through all the elements in document order. More similar to the FileMaker Pro function Status(CurrentRecordNumber), this XPath function uses particular fields/ elements and the position of a child element in a parent element.

<!-- return the position of the DATA element -->
<xsl:value-of select="fm:DATA[position()]" />
<!-- test the current position against a value -->
<xsl:if test="fm:DATA[position()=2]">

count(node-set)—This function has a numeric result. This XPath function is similar to the FileMaker Pro functions Status(CurrentFoundCount), Status(CurrentRecordCount), or Count(RepeatingOrRelatedField).

<xsl:value-of select="count(fm:ROW)" />

id(uniqueIDNum)—This function has a node-set result. Elements that have a unique ID can be selected by a special attribute, ID. This function returns the nodes with a match.

<!-- return the value of any element with the unique ID of "abc" -->
<xsl:value-of select=id('abc') />

The ID attribute is not used with FMPXMLRESULT or FMPDSORESULT. However, each ROW (record) does have a unique ID with the attribute RECORDID. You can use this attribute value to find a particular record. Often the key() function is used with the unique identifiers.

<xsl:value-of select="fm:ROW/@recordid=3894" />

local-name(node-set)—This function has a string result. This function returns the name of the current element without the namespace URI, if any. The parameter node-set is optional. The FileMaker Pro function Status(CurrentFieldName) is similar to the XPath function local-name().

<!-- return the value "FirstName" or the name of the element -->
<xsl:value-of select="local-name(fm:FirstName)" />

namespace-uri(node-set)—This function has a string result. Related to the previous function, this XPath function returns the string of the namespace URI associated with an element. Namespaces are more fully described in sections 7.11 and 7.12. Elements may have a namespace attribute (ns, xmlns), which binds it to that element and all its child elements and attributes.

There is no FileMaker Pro equivalent, but if you imaged each field bound to layouts, the layout would be the location of the field. That way, a field formatted on a layout is unique from the same field on another layout formatted a different way.

name(node-set)—This function has a string result. This XPath function is related to the last two functions. An element with a local name and a namespace would be the expanded name of the element. The parameter node-set is optional in this function, so the current node is implied if the parameter is empty. Since the FMPXMLRESULT and FMPDSO- RESULT do not have namespace attributes, this function would return the name of the current node in an XSL document.

<!-- return the name of the element and the namepace URI.-->
<xsl:value-of select="name(FirstName)" />

The name() and local-name() XPath functions can be used with FMPDSORESULT and FMPXMLRESULT to return the list of field names used in the XML result. FMPDSORESULT has the field names as the element names, and FMPXMLRESULT has the field names in the attribute of the children of the <METADATA> element.

<!-- return the list of fields with FMPDSORESULT -->
<!-- put this code snippet inside an HTML page
<xsl:for-each select="fm:ROW[1]/*">
      <xsl:value-of select="name()" /><br />
<!-- return the list of fields with FMPXMLRESULT -->
<xsl:for-each select="fm:METADATA/fm:FIELD">
      <xsl:value-of select="@NAME" /><br />

string(object)—This function has a string result. This XPath function is used to convert other object types, such as numbers and booleans, to string types. There is no exact function in FileMaker Pro, although the functions NumToText(), DateToText(), and TimeToText() might be similar.

<xsl:value-of select="string('123') />

concat(string, string, )—This function has a string result. A comma-delimited list of values and literals can be used to combine strings. Variables may also be used in this XPath function. FileMaker Pro allows concatenation in the Specify Calculation dialog with the "&" symbol and by using merge fields on a layout.

<!-- similar to "firstname"&"& "lastname" in a calculation -->
<!-- or "<fname> <lname>" in a merge field -->
<xsl:value-of select="concat($fname, ' ', $lname)" />

starts-with(string, text)—This function returns a Boolean result. This function returns "true" if the text string is at the beginning of the first parameter string.

Exact(Left(string, Length(text)), text) is a FileMaker Pro function that is similar. The Exact() function is used in this example because the XPath function is case-sensitive.

contains(string, text)—This function returns a Boolean result. The FileMaker Pro function PatternCount(string, text) > 1 is similar to this XPath function.

substring-before(string, text)—This function has a substring result. This XPath function returns the substring of the string that precedes the first occurrence of the text string in the first parameter string, or the empty string if the string does not contain the text. The FileMaker Pro functions Left(), Middle(), Right(), Position(), and PatternCount() are often used to extract substrings of text from strings. A similar calculation would be Left("abcde", Position("abcde", "cd", 1, 1) 1).

<xsl:value-of select="substring-before('abcde', 'cd') />
<!-- returns "ab" -->

substring-after(string, text)—This function has a substring result. This function returns the substring of the string that follows the first occurrence of the text string in the first parameter string, or the empty string if the string does not contain the text. The function substring-after(‘abcde’, ‘cd’) returns "e." In FileMaker Pro, this could be Right("abcde", Length("abcde")–(Position("abcde", "cd", 1, 1) + 1) ).

substring(string, start, length)—This function has a string result. The XPath function returns the substring of a string starting with the position specified by start with length specified in the third parameter. <xsl:value-of select-"substring(‘abcde’, 1, 2)" /> returns "ab." The third parameter is optional and if not specified, is assumed to be the end of the string. This is equivalent to the function Middle(text, start, size) in FileMaker Pro.

string-length(string)—This function returns a number. The FileMaker Pro function Length() is similar to this XPath function, which returns the number of characters in the string.

normalize-space(string)—This function returns a string. This XPath function will strip leading and trailing white space. White space is made up of spaces, tabs, carriage returns, and linefeeds. Multiple instances of white space between other characters in the string are reduced to one white space. The FileMaker Pro function Trim() will strip leading and trailing spaces only but does not remove tabs, carriage returns, linefeeds, or reduce multiple white space characters.

<xsl:value-of select="normalize-space('abc
  def ')" />
<!-- return 'abc def' -->

translate(string, findText, replaceText)—This function returns the string with occurrences of characters in the findText string replaced by the character at the corresponding position in the replaceText string. This function is similar to the FileMaker Pro Substitute(string, find, replace) function, but the string gets translated using any of the characters in replaceText as the pattern(s) to replace the character patterns in findText. The first character in findText is found in the string and replaced by the first character in replaceText, etc.

<xsl:value-of select='Translate("abcda", "ab", "CD")' />
<!-- returns "CDcdC" -->

boolean(object)—This function is a Boolean conversion. Any function in FileMaker Pro that returns Boolean results is equivalent to this XPath function.

not(boolean)—This function is a Boolean. This function returns the negative of the previous test. True becomes false and false becomes true. The logical operator not performs a similar function in FileMaker Pro calculations.

true()—This function is a Boolean. This XPath function simply returns the Boolean results of "true". It is used to compare other XPath expressions.

false()—This function is a Boolean and returns the Boolean value "false" when this XPath expression is used.

lang(string)—This function is a Boolean. Sometime the language of a particular element is tested against the language of the XML document. The Boolean "true" is returned if the languages match. You may find an element defined as <p xml:lang="en"> for example. The lang("en") function would return true when tested against this "p" element.

number(object)—This function is a number conversion. The FileMaker Pro function TextToNum() is equivalent to this XPath function.

sum(node-set)—This function has a numeric result. This adds the numeric values of a set of elements and returns the sum. The Sum(repeatingOrRelatedField) function in FileMaker Pro performs similarly.

floor(number)—This function returns an integer. A number value is reduced to the largest integer that is not greater than the number. The FileMaker Pro function Int(1.8) returns "1," as would "floor(1.8)".

ceiling(number)—This function returns an integer. A number is rounded up to the next higher integer, dropping the decimal portion of a number. There is no similar function in FileMaker Pro, but Int(1.8) +1 would be the same as ceiling(1.8) or the integer "2."

round(number)—This function has a numeric result. This XPath function will return the closest integer (up or down) to the argument. The FileMaker Pro function Round(number, precision) is more specific and returns whole numbers, not just integers.

7.41 Additional XSL Functions

There are several other functions in the "XSL Transformations (XSLT), Version 1.0 Recommendation," http://www.w3.org/TR/xslt. XSL uses both XPath and XSLT functions. These additions in the XSLT document are listed below.

Extension functions may be created and used by XSL processors. These functions are processor dependent and may not work for all XSL processors. A prefix is declared and the function called: prefix:function(). A common usage in the Xalan processor is to include JavaScript. The elements <xalan:component> and <xalan:script> contain the JavaScript. The processor must be properly configured to use the JavaScript. See http://www.apache.org/ for more information about the Xalan processor.

<xalan:component prefix="" elements="" functions="">
      <xalan:script lang="javascript">
            function xyz {
                  (your javascript here)

document()—The standard method of transforming XML is to use the stylesheet processing instruction inside the XML document. See section 7.13, "Stylesheet Instruction in XML Documents" and section 7.14, "Stylesheet Processing Instruction from HTTP Requests."

<?xml-stylesheet type="text/xsl" href="Default.xsl"?>

To include other external XSL stylesheets, the elements <xsl:include> and <xsl:import> can be used in the stylesheet. But to include another XML document, the XSL function document(object, node-set) is used. The second parameter, node-set, is optional and may be the element to retrieve. The first object is the URI (location) of the XML source to be included in the current document.

<xsl:variable name="mydoc" select="document(fmpro?-db=myfile.FP5&amp;
  -lay=web&amp;-format=-fmp_xml&amp;-view)" />

Since all documents use the "/" root, this symbol should not be used in templates when reading multiple XML documents.

Multiple XML exports from FileMaker Pro can be used with your stylesheet. This is most useful when you have related data and need to structure the result document without the relationship names. Export the child data and use the stylesheet with the parent export to bring in the child data.

When more than one XML document is used with the stylesheet, it is very important to maintain distinct namespace prefixes for the elements. The XSL element <xsl:namespace-alias> would be helpful if the source is more than one FMPXMLRESULT or FMPDSORESULT document. You can read about namespace-alias in section 7.2.

This may be a better way to use a database with related values. When a portal has too many rows, it is difficult to limit the number of rows in the output. It is also difficult to format the portal rows easily with XSL. One solution can be to use the related file for the XML request and simply show the first occurrence of any parent file fields.