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

Subject: Re: [xsl] Handling position in non-atomic sequences
From: "Martin Honnen martin.honnen@xxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Sat, 2 May 2020 10:57:33 -0000
Am 02.05.2020 um 12:52 schrieb Michael MC<ller-Hillebrand mmh@xxxxxxxxx:
Hi folks,

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

Also, if the variable is typed as="element()*" I cannot use the axes
preceding-sibling or following-sibling. I can solve that by not typing the
variable or use  as="document-node()" after adding <xsl:document> to the
variable's sequence constructor.

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

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?

functx has a function
http://www.xsltfunctions.com/xsl/functx_index-of-node.html which might
help, using the `is` operator:

<xsl:function name="functx:index-of-node" as="xs:integer*"
              xmlns:functx="http://www.functx.com";>
  <xsl:param name="nodes" as="node()*"/>
  <xsl:param name="nodeToFind" as="node()"/>

  <xsl:sequence select="
  for $seq in (1 to count($nodes))
  return $seq[$nodes[$seq] is $nodeToFind]
 "/>

</xsl:function>


I have also seen some approaches to use `index-of($seq!generate-id(), generate-id($node))`.

Current Thread