Re: [xsl] Converting a sequence of xs:string to a map

Subject: Re: [xsl] Converting a sequence of xs:string to a map
From: "Mukul Gandhi mukulg@xxxxxxxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 13 Jan 2026 12:07:32 -0000
Hi Christophe,
    Here's another XSLT 3.0 solution to this,

<?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:map="
http://www.w3.org/2005/xpath-functions/map";
                         xmlns:fn0="http://fn0";
                         exclude-result-prefixes="#all"
                         version="3.0">

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

       <xsl:template match="/">
             <result>
                 <xsl:variable name="map1"
select="fn0:constructMap1(('amount', 'vat-rate', 'total'))" as="map(*)"/>
                 <xsl:for-each select="map:keys($map1)">
                      <item>
                           <pos><xsl:value-of select="."/></pos>
                           <value><xsl:value-of select="map:get($map1,
.)"/></value>
                      </item>
                  </xsl:for-each>
              </result>
        </xsl:template>

        <xsl:function name="fn0:constructMap1" as="map(*)">
             <xsl:param name="seq1" as="xs:string*"/>
             <xsl:iterate select="$seq1">
                   <xsl:param name="map1" select="map {}" as="map(*)"/>
                   <xsl:on-completion>
                         <xsl:sequence select="$map1"/>
                   </xsl:on-completion>
                   <xsl:variable name="str1" select="." as="xs:string"/>
                   <xsl:variable name="pos1" select="position()"
as="xs:integer"/>
                   <xsl:next-iteration>
                         <xsl:with-param name="map1" select="map:put($map1,
$pos1, $str1)" as="map(*)"/>
                   </xsl:next-iteration>
               </xsl:iterate>
          </xsl:function>

</xsl:stylesheet>

This stylesheet produces following, XSL transformation result,

<?xml version="1.0" encoding="UTF-8"?><result>
  <item>
      <pos>1</pos>
      <value>amount</value>
  </item>
  <item>
      <pos>2</pos>
      <value>vat-rate</value>
  </item>
  <item>
      <pos>3</pos>
      <value>total</value>
  </item>
</result>

This works fine with both, Saxon XSL transformer and Apache Xalan XSLT 3.0
development code.

On Tue, Jan 13, 2026 at 2:18b/AM Christophe Marchand christophe@xxxxxxxxxxxx
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:

> I have a sequence of xs:string : ('amount', 'vat-rate', 'total')
> And I want a map where keys are the labels from the sequence, and values
> are the position of label in sequence : { 'amount': 1, 'vat-rate': 2,
> 'total': 3}
>


--
Regards,
Mukul Gandhi

Current Thread