xsl-list
[Top] [All Lists]

Re: [xsl] Handling position in non-atomic sequences

2020-05-02 06:43:41


On 2 May 2020, at 11:52, Michael Müller-Hillebrand mmh(_at_)docufy(_dot_)de 
<xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com> wrote:

Hi folks,

if a sequence stored in a variable consists of elements, we can not use the 
nice function index-of().

You can, but it might not do what you want...

Saxon (PE+, 10.0+) has an extension function

saxon:index-where($seq, $predicate)

so you can do, for example, saxon:index-where($nodes, function($n){$n is 
$required-node})


Also, if the variable is typed as="element()*" I cannot use the axes 
preceding-sibling or following-sibling.

Again, you can, but it might not do what you want. Presumably you want to find 
adjacent members of the sequence. Of course a node might appear three times in 
the same sequence, so just knowing the node and the sequence isn't enough...

I can solve that by not typing the variable or use  as="document-node()" 
after adding <xsl:document> to the variable's sequence constructor.

That will copy all the nodes and change their siblings and ancestry..

My current solution finding the position of the first <h1> in a sequence 
would be something like:

You're talking about a solution, but I don't think you've told us what the 
problem is. What are you trying to achieve?

count($seq/h1[1]/preceding-sibling::*) + 1

with $seq being a sequence typed as document-node().

Are there better methods to get the position of a certain element in the 
sequence?


for $i in (1 to count($nodes)) return if ($nodes[$i] is $required-node) then $i 
else ()

or more concisely

(1 to count($nodes))[subsequence($nodes, . 1) is $required-node]

You could also implement the saxon:index-where() function as

(1 to count($seq))[$predicate(subsequence($nodes, ., 1))]

(subsequence($nodes, $x, 1) is used rather than $nodes[$x] because predicates 
change the context item)

Michael Kay
Saxonica

--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--
<Prev in Thread] Current Thread [Next in Thread>