



















|
| |
At times, you may want to call your own custom C functions from a stylesheet. For these situations, Xalan-C++ supports the creation and use of extension functions.
You can think of extension functions as extending the core library of functions that XPath provides. Like the
XPath functions, an extension function returns an XObject, which may contain a value of any of the five XSLT
data types: node-set, result-tree-fragment, string, boolean, or number.
You can send arguments to an extension function in the form of XPath expressions, literals (for string, boolean, and number), the values returned by other functions, and XSL variables or parameters set to any of the preceding.
For an example that implements, installs, and uses three extension functions, see the
External Functions sample.
 | Xalan-C++ does not yet support extension elements. Support for extension elements will be added in the near future. |
|
 |  |  |  | Installing an extension function |  |  |  |  |
| |
When you set up an XSLT processor, you instantiate an XSLTProcessorEnvSupportDefault object, which provides
methods for installing (and uninstalling) extension functions:
- installExternalFunctionGlobal() is a static method for making a function available globally
- installExternalFunctionLocal() makes the function available to the XSLTProcessorEnvSupport instance in which the function is
installed.
These methods include arguments for the namespace, the function name, and the function implementation.
 | These XSLTProcessorEnvSupportDefault methods call XPathEnvSupportDefault methods of the same name which provide
the actual implementation. |
When you install an extension function, the function inhabits the namespace you designate. For information about XML namespaces, see Namespaces in XML.
The following code fragment installs the square root function defined above as a global function, and binds it to the extension-function name "square-root" in the namespace "https://ExternalFunction.xalan-c.xml.apache.org" so it can be accessed from stylesheets. Keep in mind that the function name does not need to be the same as the name of the function class, and that a function name may be used more than once provided that each function with that name is installed in a different namespace.
 |  |  |  | // You have created a header file for FunctionSquareRoot.
#include <MyFunctions/FunctionSquareRoot.hpp>
// The namespace...
const XalanDOMString
theNamespace("https://ExternalFunction.xalan-c.xml.apache.org");
// Install the function in the global space so all processor
// instances can use it.
XSLTProcessorEnvSupportDefault::installExternalFunctionGlobal(
theNamespace,
"square-root",
FunctionSquareRoot()); |  |  |  |  |
 | We do not recommend this, but you can also install a function directly into the XPath function table, in which
case it is not really an extension function. The XPath parser treats it like any other XPath function, and no namespace declaration or prefix is involved. |
For an example that installs a global extension function, a local extension function, and adds a function to the
XPath function table, see the External Functions sample.
|
 |  |  |  | Using an extension function |  |  |  |  |
| |
To use the extension function in a stylesheet, you must do the following:
- Declare the extension function namespace.
xmlns:prefix=URI
The prefix identifies the namespace, and URI matches the namespace specified when the function
is installed.
By default, namespace declarations are included in the transformation output. To exclude namespaces from the output,
use (NOT YET IMPLEMENTED)
exclude-result-prefixes="prefix-1 prefix-2 ..."
in the stylesheet element or
xsl:exclude-result-prefixes="prefix-1 prefix-2 ..."
in a literal result element or extension element.
- Call the extension function in the same manner you would call an XPath function. The function name you use in the stylesheet is a Qualified Name (QName) made up of the prefix you delcared in step 1 and the function name you specified when you installed the function.
You can use XPath expressions, literals (for string, boolean, and number), and values returned by other functions to
specify function arguments.
Suppose, for example, you are working with XML documents containing area elements like
<area value="397"/> , where the value attribute identifies the area of a square.
The following stylesheet declares the square-root function namespace (the prefix is up to you), instructs
the processor not to copy the namespace declaration to the result tree, and uses the square-root function to return
the square root of //area/@value:
 |  |  |  | <?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="https://www.w3.org/1999/XSL/Transform"
version="1.0"
xmlns:external="https://ExternalFunction.xalan-c.xml.apache.org"
exclude-result-prefixes="external">
<xsl:template match="//area">
<out>
The area of the square is
<xsl:value-of select="@value"/> square units.
The length of each side is
<xsl:value-of select="external:square-root(@value)"/> units
</out> |  |  |  |  |
This stylesheet converts <area value="397"/> into the following output:
 |  |  |  | <out>
The area of the square is
397 square units.
The length of each side is
19.9249 units.
</out> |  |  |  |  |
For a slightly more complex variation on this example,
see the External Functions sample.
 |  |  |  | Passing Nodes to a function |  |  |  |  |
| |
Please keep in mind that all LocationPath expressions return a node-set, even if the expression only
returns a single attribute or a text node (node-sets with one member). You can use the XSLT string() function
to convert a node-set value to string, and the number() function to convert a node-set value to number (a double).
If you want to pass a node-set to an extension function, set up the function to accept a XNodeSet.
Suppose, for example, you have a ProcessNodes function class that uses
const NodeRefListBase& theNodeList = args[0]->nodeset();
in the execute() method to get a reference to the node-set.
Assuming you install the function as "ProcessNodes" and use the "node-ext" prefix in a stylesheet to refer to the ProcessNodes function namespace, any of the following function calls are syntactically possible:
<!--Process the current node-->
<xsl:variable name="success" select="node-ext:ProcessNodes(.)"/>
<!--Process all nodes in current context-->
<xsl:variable name="success" select="node-ext:ProcessNodes(*)"/>
<!-- Process all nodes -->
<xsl:variable name="success" select="node-ext:ProcessNodes(/*)"/>
<!--Process the foo/baz nodes in current context -->
<xsl:variable name="success" select="node-ext:ProcessNodes(foo/baz)"/>
<!--Process the/foo/baz and /bar/saz nodes -->
<xsl:variable name="success" select="node-ext:ProcessNodes(/foo/baz | /bar/saz)"/>
The NodeRefListBase is in fact a list of references into the XML document, so keep in mind that getNextSibling(),
for example, gets you the next sibling in the document, which may not be the next Node in the node-set.
|
|
|
|