# Extension Module - Ft.Xml.XPath.BuiltInExtFunctions

    4XPath-specific extension functions

    Copyright 2005 Fourthought, Inc. (USA).
    Detailed license and copyright information: http://4suite.org/COPYRIGHT
    Project home, documentation, distributions: http://4suite.org/

    Extension functions are implemented by a module, such as this one,
    that defines an ExtFunctions global dictionary that maps (namespace,
    local-name) string tuples to a corresponding Python function. The
    function must take a Context object as the first argument, and any
    additional arguments accepted will correspond to the arguments passed
    in. See other API docs to see how to make use of modules that contain
    ExtFunctions.

## Namespaces

The namespace for extensions defined in this document are:

Prefix | Namespace
--- | ---
`f` | `http://xmlns.4suite.org/ext`

The prefix is given for this document only. Any other prefix can be used within a particular stylesheet.

## Functions

Function | Syntax
--- | ---
f:base-uri | unknown f:base-uri(arg)
f:bin-path | unknown f:bin-path()
f:cos | unknown f:cos(x)
f:decode | unknown f:decode(object, encoding)
f:degrees-to-rads | unknown f:degrees-to-rads(x)
f:encode | unknown f:encode(object, encoding)
f:ends-with | unknown f:ends-with(outer, inner)
f:escape-xml | unknown f:escape-xml(text)
f:fact | unknown f:fact(x)
f:generate-uuid | unknown f:generate-uuid()
f:if | unknown f:if(cond, v1, v2)
f:import-string | unknown f:import-string(object)
f:indent | unknown f:indent(text, levels, indentstring)
f:join | unknown f:join(nodeset, delim)
f:match | unknown f:match(pattern, arg)
f:normalize-eol | unknown f:normalize-eol(text)
f:ospath2uri | unknown f:ospath2uri(path)
f:parse-date | unknown f:parse-date(date, format)
f:parse-xml | unknown f:parse-xml(src, parameters)
f:pytime-to-exslt | unknown f:pytime-to-exslt(t)
f:random | unknown f:random(max, forceInt)
f:range | unknown f:range(lo, hi)
f:replace | unknown f:replace(old, new, arg)
f:resolve-path | unknown f:resolve-path(base, rel)
f:resolve-url | unknown f:resolve-url(base, rel)
f:sha-hash | unknown f:sha-hash(text)
f:share-path | unknown f:share-path()
f:sin | unknown f:sin(x)
f:strftime | unknown f:strftime(format, date)
f:uri2ospath | unknown f:uri2ospath(uri)
f:version | unknown f:version()
f:wrap | unknown f:wrap(text, width)

## Functions

### Function - f:base-uri

#### Function Syntax

unknown f:base-uri(arg)

Returns the base URI of the first node in the given node-set, or of the context node if no argument is given. If the given node-set is empty, an empty string is returned.

### Function - f:bin-path

#### Function Syntax

unknown f:bin-path()

Returns the system-dependent path of Fourthought binaries

### Function - f:cos

#### Function Syntax

unknown f:cos(x)

math.cos

### Function - f:decode

#### Function Syntax

unknown f:decode(object, encoding)

f:decode mirrors the Python decode function/method. It takes a foreign object that is a Python byte string, and an encoding, and returns another foreign object which is a Unicode object.

### Function - f:degrees-to-rads

#### Function Syntax

unknown f:degrees-to-rads(x)

Convert degrees to radians

### Function - f:encode

#### Function Syntax

unknown f:encode(object, encoding)

f:encode mirrors the Python encode function/method. It takes a foreign object that is a Unicode object, and an encoding, and returns another foreign object which is a Python byte string.

### Function - f:ends-with

#### Function Syntax

unknown f:ends-with(outer, inner)

Returns true if the string given in the first argument ends with the substring given in the second argument.

### Function - f:escape-xml

#### Function Syntax

unknown f:escape-xml(text)

Returns the given string with XML markup characters "&", "<" and ">" escaped as "&amp;", "&lt;" and "&gt;", respectively.

### Function - f:fact

#### Function Syntax

unknown f:fact(x)

Factorial

### Function - f:generate-uuid

#### Function Syntax

unknown f:generate-uuid()

Returns a random UUID string.

### Function - f:if

#### Function Syntax

unknown f:if(cond, v1, v2)

If the first argument, when converted to a boolean, is true, returns the second argument. Otherwise, returns the third argument, or if the third argument is not given, returns an empty node-set.

### Function - f:import-string

#### Function Syntax

unknown f:import-string(object)

f:import-string takes a Unicode FO and returns an XPath string. It is an error if the FO contains illegal XML chars. (although eventually this function might be extended to recover from this error)

### Function - f:indent

#### Function Syntax

unknown f:indent(text, levels, indentstring)

f:indent() returns a string with each line of the text indented the given number of levels. For each level, the indent string, normally 2 spaces by default, is prepended to each line.

### Function - f:join

#### Function Syntax

unknown f:join(nodeset, delim)

Concatenates the string-values of the nodes in the given node-set, inserting the delimiter given in the optional second argument in between each string-value. The delimiter defaults to a space. See also: EXSLT's str:concat()

### Function - f:match

#### Function Syntax

unknown f:match(pattern, arg)

Returns true if the string given in the optional second argument (or the string-value of the context node if no second argument is given) matches the regular expression given in the first argument. See also: EXSLT's regexp:test() This function does differ from XSLT 2.0 match() function

### Function - f:normalize-eol

#### Function Syntax

unknown f:normalize-eol(text)

Normalizes end-of-line characters in input string, returning the normalized string. Normalization involves replacing " ", " " or " " with " "

### Function - f:ospath2uri

#### Function Syntax

unknown f:ospath2uri(path)

Returns the given OS path as a URI. The result varies depending on the underlying operating system.

### Function - f:parse-date

#### Function Syntax

unknown f:parse-date(date, format)

This function is similar to EXSLT's date:parse-date() except that it uses Python rather than Java conventions for the date formatting.

### Function - f:parse-xml

#### Function Syntax

unknown f:parse-xml(src, parameters)

f:parse-xml() parses the string-value of the given object as XML and returns a node-set whose sole item is the resulting parsed document's root node. The XML must be a well-formed document. src - the string or object to be parsed as XML. parameters - the name of a parameter set for the operation. The parameters argument is ignored for now. In the future, it will provide a way to specify a base URI for the resolution of relative URIs in entity declarations and XIncludes. Also for now, if the XML contains an encoding declaration, the declaration must specify UTF-8. An example: <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:f="http://xmlns.4suite.org/ext" version="1.0" > <xsl:output method="text"/> <xsl:variable name="doc" select="'&lt;spam>eggs&lt;monty>python&lt;/monty>&lt;/spam>'"/> <xsl:template match="/"> <xsl:value-of select="f:parse-xml($doc)/spam/monty"/> </xsl:template> </xsl:stylesheet> ...run against any XML source should yield: python See also: XSLT (not XPath) extension function f:serialize-xml()

### Function - f:pytime-to-exslt

#### Function Syntax

unknown f:pytime-to-exslt(t)

Takes a Python time value as a number and returns a date/time as if from EXSLT date-time() t - a time stamp number, as from Python's time.time() if omitted, use the current time

### Function - f:random

#### Function Syntax

unknown f:random(max, forceInt)

Returns a random number between 0 and max (max defaults to 1). The first optional argument is a different value for max, and the second argument is a flag that, if set, causes the random number to be an integer. See also: EXSLT's math:random()

### Function - f:range

#### Function Syntax

unknown f:range(lo, hi)

Returns a node-set consisting of text nodes encapsulating integers in the numeric range bounded by the given low and high values.

### Function - f:replace

#### Function Syntax

unknown f:replace(old, new, arg)

Returns the third argument string, which defaults to the string-value of the context node, with occurrences of the substring given in the first argument replaced by the string given in the second argument. See also: EXSLT's str:replace()

### Function - f:resolve-path

#### Function Syntax

unknown f:resolve-path(base, rel)

Resolves a Posix-style path, such as the path portion of a URL, against a base. Similar to f:resolve-url, but allows the base to be just a path, not necessarily a full URL.

### Function - f:resolve-url

#### Function Syntax

unknown f:resolve-url(base, rel)

Returns the relative URL ref given in the second argument resolved against the base given in the first argument.

### Function - f:sha-hash

#### Function Syntax

unknown f:sha-hash(text)

Returns a SHA message digest of the given string, as a string of several groups of hex digits separated by '-'. See http://www.itl.nist.gov/fipspubs/fip180-1.htm for info on SHA.

### Function - f:share-path

#### Function Syntax

unknown f:share-path()

Returns the system-dependent path to modifiable data

### Function - f:sin

#### Function Syntax

unknown f:sin(x)

math.sin

### Function - f:strftime

#### Function Syntax

unknown f:strftime(format, date)

Returns the given ISO 8601 UTC date-time formatted according to the given format string as would be used by Python's time.strftime(). If no date-time string is given, the current time is used.

### Function - f:uri2ospath

#### Function Syntax

unknown f:uri2ospath(uri)

Returns the given URI as an OS path. The result varies depending on the underlying operating system.

### Function - f:version

#### Function Syntax

unknown f:version()

Returns the 4Suite version number as a string.

### Function - f:wrap

#### Function Syntax

unknown f:wrap(text, width)

f:wrap() returns a string with the text reflowed so that each line fits within the given width. Existing linefeeds are preserved, but spaces are considered inter-word separators that can be collapsed. To reflow without preserving existing linefeeds, strip them first, e.g. with translate(text, '&#10;', ''). http://lists.fourthought.com/pipermail/4suite-dev/2002-December/000878.html