Subject: Re: [xsl] xpath index-of function using xslt 3.0 From: "Martin Honnen martin.honnen@xxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> Date: Fri, 3 Oct 2025 14:25:36 -0000 |
Am 03.10.2025 um 14:49 schrieb Mukul Gandhi mukulg@xxxxxxxxxxxxxxxxx: > Hi all, > B B B 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" > B B B B B B B B B B B B B xmlns:xs="http://www.w3.org/2001/XMLSchema" > B B B B B B B B B B B B B xmlns:fn0="http://fn0" > B B B B B B B B B B B B B exclude-result-prefixes="#all" > B B B B B B B B B B B B B version="3.0"> > > B B <xsl:output method="xml" indent="yes"/> > > B B <xsl:variable name="var1" select="('abc1', 'abc2', 'abc1', 'abc5', > 'abc4')" as="xs:string*"/> > > B B B <xsl:template match="/"> > B B B B B <result> > B B B B B B B <xsl:for-each select="('abc1', 'abc4')"> > B B B B B B B B B B <index srch="{.}"><xsl:value-of > select="fn0:indexOf($var1, .)"/></index> > B B B B B B B </xsl:for-each> > B B B B B </result> > B B B </xsl:template> > > B B B <xsl:function name="fn0:indexOf" as="xs:integer*"> > B B B B B <xsl:param name="strList" as="xs:string*"/> > B B B B B <xsl:param name="srch1" as="xs:string"/> > B B B B B <xsl:iterate select="$strList"> > B B B B B B B B <xsl:param name="result" select="()" as="xs:integer*"/> > B B B B B B B B <xsl:on-completion> > B B B B B B B B B B B <xsl:sequence select="$result"/> > B B B B B B B B </xsl:on-completion> > B B B B B B B B <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 theB XPath 3.1 function 'index-of' > B B B B B B B B B B B <xsl:next-iteration> > B B B B B B B B B B B B B B <xsl:with-param name="result" > select="($result, position())"/> > B B B B B B B B B B B </xsl:next-iteration> > B B B B B B B B </xsl:if> > B B B B B B </xsl:iterate> > B B B B </xsl:function> > > </xsl:stylesheet> > > The above cited stylesheet, produces following XSL transformation result, > > <?xml version="1.0" encoding="UTF-8"?><result> > B <index srch="abc1">1 3</index> > B <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 <>)
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
[xsl] xpath index-of function using, Mukul Gandhi mukulg@ | Thread | Re: [xsl] xpath index-of function u, Michael Kay michaelk |
[xsl] xpath index-of function using, Mukul Gandhi mukulg@ | Date | Re: [xsl] xpath index-of function u, Michael Kay michaelk |
Month |