qt4cg / qtspecs

QT4 specifications
https://qt4cg.org/
Other
28 stars 15 forks source link

Add a function equivalent to xsl:number #1427

Open michaelhkay opened 1 week ago

michaelhkay commented 1 week ago

I propose adding a function to perform node numbering in a manner analogous to xsl:number (but without the formatting aspects, which can be handled using format-integer, and without multi-level numbering).

I envisage a function along the following lines:

node-number($node as element(), 
                         $from as (document-node()|element()), 
                         $count as fn($node as element()) as xs:boolean?)
   as xs:integer

The function returns the number of element nodes that satisfy all the following conditions:

$node defaults to the context node. $from defaults to the parent of $node. $count defaults to a function that returns true for an element that has the same name as $node, false otherwise.

With no arguments, node-number() applied to (say) a <p> element returns the number of preceding-sibling <p> elements plus one. [Not quite. Under this definition, it would also count <p> elements that are descendants of a preceding-sibling element]

The rationale for this proposal is (a) to make the core functionality of xsl:number available in environments other than XSLT, and (b) within XSLT, to make it available in contexts such as a predicate of a match pattern where it is currently difficult or impossible to invoke xsl:number except by wrapping it in a user-defined variable or function.

ChristianGruen commented 1 week ago

If the function had no $from parameter, would the following call be more or less equivalent?

node-number($node[ancestor::node()[. is $from]], $count)

Maybe I wouldn’t ask if I had already worked with xsl:number.

michaelhkay commented 1 week ago

would the following call be more or less equivalent?

No, I don't think so. If you want to number equations within a chapter, you need something like

node-number(., ancestor::chapter)

to determine how many previous equations there are in the same chapter.

Arithmeticus commented 1 week ago

I like this idea. For some of the reasons that motivate this suggestion, I have generally avoided xsl:number, and preferred XPath.

If a zero-arity function is envisioned, then question marks should be added to the parameters.

I'd be curious to see what others thing, but for the default of $from I would prefer ancestor::*[last()]. Basically for the reason expressed in the italics: People could be easily lulled into thinking this gets the count of preceding sibling nodes. If you're getting preceding nodes, not preceding sibling nodes, then might as well get the whole gamut of preceding nodes in the context tree.