xpathFunc(n)

πŸ“¦ version 0.9.6, in fedora 43, fedora 44, nixos 26.05, nixpkgs unstable

✨ xpathFunc(n) ✨

okay bestie, this is the Tcl manual page for xpathFunc(n) and we're gonna make it make sense πŸ’–


πŸŽ€ NAME πŸŽ€

::dom::xpathFunc β€” Scripted XPath functions. no cap, that's the whole name section πŸ’…


🌸 SYNOPSIS 🌸

::dom::xpathFunc::funcName ctxNode pos nodeListType nodeList ?type arg type arg...?
::dom::xpathFunc::namespaceURL::funcName ctxNode pos nodeListType nodeList ?type arg type arg...?

πŸ¦‹ DESCRIPTION πŸ¦‹

So here's the tea, bestie: tDOM's XPath engine lets you write custom XPath functions as Tcl commands. It's giving DIY but make it iconic. 🫢

When the engine runs into a function call to an unknown function name with no namespace qualifier, it goes looking for a Tcl command with that same name inside the ::dom::xpathFunc namespace. And if it runs into an unknown namespace-qualified function call? Then it looks for a Tcl namespace whose name equals the function's full namespace URI, living inside ::dom::xpathFunc, and inside that namespace it hunts for a Tcl command named for the local part of the function's name. ✨

If it finds such a Tcl command, it gets executed with at least these arguments, which describe the current XPath context:

  • ctxNode β€” the domNode object command of the XPath context node. your main character node πŸ‘‘
  • pos β€” the XPath context position, in zero-based form. literally: the return value of the standard XPath position() function in that context would be $pos - 1. yeah. off by one. we love that for us. 😭
  • nodeListType β€” the type name for nodeList, as for the selectNodes method's typeVar.
  • nodeList β€” the list of the current worked-on XPath step result (as selectNodes would return them), in document order. ctxNode will be equal to [lindex $nodeList $pos].

And if the function call includes any arguments, then two arguments get appended to the command's argument list for each one:

  • type β€” the argument's type name, as for selectNodes's typeVar.
  • val β€” the argument's value, as selectNodes would return it in an XPath expression's result set.

πŸ’« the return value (vibe check) πŸ’«

Your command is required to return a 1- or 2-element Tcl list to provide the result of the XPath function call. πŸŽ€

If the result list has two elements, the first one is the result's XPath type name, and the second is an appropriately encoded value. > heads up bestie: the attrnodes type name is not supported here πŸ’€

  • bool β€” Tcl boolean value acceptable to Tcl_GetBooleanFromObj.
  • number β€” Tcl numeric value acceptable to Tcl_GetSizeIntFromObj or Tcl_GetDoubleFromObj.
  • string β€” Simple string.
  • nodes β€” Tcl list of domNode object commands.
  • attrvalues β€” Alias for string.

If the result list has only one element, it's treated as a simple string. πŸ’…

🚨 and now the rules, no negotiating: it is an error for the result to have zero elements, more than two elements, or to be invalid as a Tcl list. It is also an error if the val of a two-part result does not match the requirements described above for its type. so like. get it right the first time fr fr 😀

🌈 EXAMPLES 🌈

Here's a simple workalike for the XPath 2/3 fn:matches() function, which is not otherwise available in an XPath 1.0 engine such as tDOM's:

proc ::dom::xpathFunc::regmatch {
    ctxNode pos nodeListType nodeList
    inputType inputVal patternType patternVal
} {
    set input [::dom::xpathFuncHelper::coerce2string $inputType $inputVal]
    set pattern [::dom::xpathFuncHelper::coerce2string $patternType $patternVal]
    return [list bool [regexp -- $pattern $input]]
}

πŸ’œ HELPER PROCS πŸ’œ

The ::dom::xpathFuncHelper namespace contains helper procs for the convenience of scripted XPath functions. they're your little backstage crew 🧚

coerce2number type val

Given a type and val as provided to scripted XPath functions in their argument lists, convert the val to a number in a manner appropriate for its type:

  • empty β€” Always zero.
  • number β€” Unchanged.
  • string β€” Unchanged. (Note that no error is raised if val is not numeric.)
  • attrvalues β€” value's first element.
  • nodes β€” The result of the number() XPath function called on the first node in the list.
  • attrnodes β€” The value of the single attribute assumed to be in value. Note that no error is raised if this value is non-numeric.

coerce2string type val

As for coerce2number, but convert val to a pure string:

  • empty β€” The empty string.
  • number β€” Unchanged.
  • string β€” Unchanged.
  • attrvalues β€” value's first element.
  • nodes β€” The result of the string() XPath function called on the first node in the list.
  • attrnodes β€” The value of the single attribute assumed to be in value.

⚠️ LIMITS ⚠️

Custom XPath function names are limited to 200 characters, including any namespace URI and the :: Tcl namespace separator between it and the local part. πŸ”₯

Function calls may have a maximum of 22 arguments (the argument values itself may be large nodesets). If you really need more, this limits may be adjusted by build time defines. And heads up: Tcl commands created with the deprecated Tcl_CreateCommand interface cannot be used as scripted XPath functions. πŸ’€

πŸ›‘ BIG ONE, DO NOT SKIP THIS πŸ›‘ β€” You must not alter any of the DOM trees from which nodes are provided to a scripted XPath function as argument. This is true for the ctxNode argument as well as for the nodes in the nodeList argument. Use them strictly read-only. Ignoring this advice may have any unpredictable results, including segmentation faults or security problems. like. bestie. please. read-only. πŸ™ˆ

⭐ KEYWORDS ⭐

XPath


and that's the whole page, gorgeous πŸ’– this was xpathFunc(n) from the Tcl manual β€” Tcl on one side, xpathFunc(n) on the other, just like the footer says. go write some unhinged little XPath functions, you absolute icon πŸ‘‘πŸŒΈπŸ¦‹

πŸ“¦ where this page comes from