Custom XPath functions: Difference between revisions
forms>Erikbruchez No edit summary |
forms>Erikbruchez No edit summary |
||
Line 94: | Line 94: | ||
* Seems like send, load, toggle, setindex, and setfocus should be forbidden? Only data manipulator actions. | * Seems like send, load, toggle, setindex, and setfocus should be forbidden? Only data manipulator actions. | ||
''' Inspiration ''' | ''' Inspiration: ''' | ||
* http://www.exslt.org/func/elements/function/index.html | * http://www.exslt.org/func/elements/function/index.html |
Revision as of 17:14, 20 February 2008
Custom XPath Functions
It is possible to add XPath functions to an form using the processor's capabilities.
For example, a set of XForms actions could be given a function name and invoked from XPath.
The evaluation context for XPaths in the actions would be set to an implicit empty instance node, and the instance() function would not work for XPaths within an XForms function definition. This prevents function side-effects.
The function parameters would appears as XPath variables, and more variables could be declared.
<function name="foo" as="number"> <param name="p" as="number"/> <param name="q" as="number"/> <return value="$p+$q"/> </function>
This would be invoked in an XPath simply as xflocal:foo(p, q)
Spec example: Need something like decimal-string() function
<function name="dollar-round" as="string"> <param name="num" as="number"/> <variable name="num100str" as="string" value="string(round(num * 100))"/> <variable name="trunc" as="string" value="substring-before($num100str, '.')"/> <variable name="intpart" as="string" value="substring($trunc, 0, string-length($trunc)-2)"/> <variable name="fracpart" as="string" value="substring($trunc, string-length($intpart), 2)"/> <return value="concat($intpart, '.', $fracpart)"/> </function>
Spec example: Need a sumproduct() function
<function name="sumproduct" as="number"> <param name="p" as="nodeset"/> <param name="q" as="nodeset"/> <variable name="counter" as="number">1</variable> <variable name="accumulator" as="number">0</variable> <action if="count($p) = count($q)"> <action while="$counter < count($p)"> <setvalue name="accumulator" value="$accumulator + ($p[$counter] * $q[$counter])"/> <setvalue name="counter" value="$counter + 1"/> </action> </action> <return value="$accumulator"/> </function> ... <output context="/purchaseOrder/items" value="xflocal:sumproduct(item/price, item/quantity)"/>
The set of 'price' children of each 'item' comprise the first parameter.
The set of 'quantity' children of each 'item' comprise the second parameter.
For each pair of price and quantity elements under an item, the product is computed. The result is the accumulated sum of the products.
Notes:
- Variables can be declared anywhere in the sequence.
- The name attribute is used in setvalue because ref must return a node
- Avoided putting local instance data for now for basic use cases
- Functions can be recursive
- Function names can be assigned a namespace by using a qname
- Function names that are NCName are in the namespace http://www.w3.org/2002/xforms/local-function#
- Functions declared and implemented in other languages (e.g. script, Java) are made available to XPath in the namespace http://www.w3.org/2002/xforms/local-function#
Questions:
- How do we allow a function to build up nodesets to return? How do we let those nodesets contain new nodes not in the original tree from which the input parameter nodesets are drawn? How do we then restrict functions from modifying the node structures of the nodes passed as input?
- Do we want optional parameters?
- Should we apply 'if' to return? Is return an action or part of the function syntax?
- Should local instance be allowed now? If so, could insert nodesets from params and manipulate. Could return nodeset from params, but return local instance node is a problem.
- Seems like send, load, toggle, setindex, and setfocus should be forbidden? Only data manipulator actions.
Inspiration:
- http://www.exslt.org/func/elements/function/index.html
- http://www.exslt.org/func/elements/script/index.html
- http://www.w3.org/TR/xslt20/#stylesheet-functions
- http://www.w3.org/TR/xquery/#FunctionDeclns