[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: xpath index-of function using xslt 3.0

Subject: Re: xpath index-of function using xslt 3.0
From: "Michael Kay michaelkay90@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 3 Oct 2025 17:55:24 -0000
Re:  xpath index-of function using xslt 3.0
There is surely a much simpler implementation, something like

<xsl:function name="my:index-of" as="xs:integer*">
  <xsl:param name="sequence" as="xs:anyAtomicType*/>
  <xsl:param name="search" as="xs:anyAtomicType"/>
  <xsl:sequence select="1 to count($sequence)[
deep-equal(subsequence($sequence, ., 1), $search) ]"/>
</xsl:function>

> On 3 Oct 2025, at 15:25, Martin Honnen martin.honnen@xxxxxx
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:
>
>
>
> Am 03.10.2025 um 14:49 schrieb Mukul Gandhi mukulg@xxxxxxxxxxxxxxxxx
<mailto:mukulg@xxxxxxxxxxxxxxxxx>:
>> 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 <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">
>
> https://www.w3.org/TR/xpath-functions-31/#func-index-of says
>
> The items in the sequence $seq are compared with $search under the rules for
the eq operator. Values of type xs:untypedAtomic are compared as if they were
of type xs:string. Values that cannot be compared, because the eq operator is
not defined for their types, are considered to be distinct. If an item
compares equal, then the position of that item in the sequence $seq is
included in the result.
>
> So if you use = instead of eq you are not implementing the XPath 3.1
function 'index-of'
>
>
>
>>                      <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
>> XSL-List info and archive <http://www.mulberrytech.com/xsl/xsl-list>
>> EasyUnsubscribe <http://lists.mulberrytech.com/unsub/xsl-list/582271> (by
email <>)
>
> XSL-List info and archive <http://www.mulberrytech.com/xsl/xsl-list>
> EasyUnsubscribe <http://lists.mulberrytech.com/unsub/xsl-list/3500899> (by
email <>)

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.