[xsl] xpath index-of function using xslt 3.0

Subject: [xsl] xpath index-of function using xslt 3.0
From: "Mukul Gandhi mukulg@xxxxxxxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 3 Oct 2025 12:49:21 -0000
Hi all,
     I've just been trying to implement XPath 3.1 function 'index-of', with
XSLT 3.0 code using xsl:iterate instruction, and came up with following XSL
stylesheet,

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                         xmlns:xs="http://www.w3.org/2001/XMLSchema";
                         xmlns:fn0="http://fn0";
                         exclude-result-prefixes="#all"
                         version="3.0">

    <xsl:output method="xml" indent="yes"/>

    <xsl:variable name="var1" select="('abc1', 'abc2', 'abc1', 'abc5',
'abc4')" as="xs:string*"/>

     <xsl:template match="/">
          <result>
              <xsl:for-each select="('abc1', 'abc4')">
                   <index srch="{.}"><xsl:value-of
select="fn0:indexOf($var1, .)"/></index>
             </xsl:for-each>
         </result>
     </xsl:template>

     <xsl:function name="fn0:indexOf" as="xs:integer*">
          <xsl:param name="strList" as="xs:string*"/>
          <xsl:param name="srch1" as="xs:string"/>
          <xsl:iterate select="$strList">
               <xsl:param name="result" select="()" as="xs:integer*"/>
               <xsl:on-completion>
                     <xsl:sequence select="$result"/>
               </xsl:on-completion>
               <xsl:if test=". = $srch1">
                     <xsl:next-iteration>
                           <xsl:with-param name="result" select="($result,
position())"/>
                     </xsl:next-iteration>
                </xsl:if>
            </xsl:iterate>
        </xsl:function>

</xsl:stylesheet>

The above cited stylesheet, produces following XSL transformation result,

<?xml version="1.0" encoding="UTF-8"?><result>
  <index srch="abc1">1 3</index>
  <index srch="abc4">5</index>
</result>

This works fine with both Saxon-HE 12.5 XSLT 3.0 processor and Apache
Xalan's XSLT 3.0 development code.

Thought that, I should share this XSL stylesheet example, for comments from
anyone interested within this XSL list.


-- 
Regards,
Mukul Gandhi

Current Thread