Subject: Re: [xsl] 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 |
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 |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] xpath index-of function u, Martin Honnen martin | Thread | Re: [xsl] xpath index-of function u, Mukul Gandhi mukulg@ |
Re: [xsl] xpath index-of function u, Martin Honnen martin | Date | Re: [xsl] xpath index-of function u, Mukul Gandhi mukulg@ |
Month |