Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6010153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:04:32+00:00 2026-05-23T02:04:32+00:00

I would like to implement a split function/template in XSLT which takes as input

  • 0

I would like to implement a split function/template in XSLT which takes as input a string and a delimiter and returns a split array of the string..

Or rather I’d like the ability to do something along the lines of:

<xsl:call-template name="F">
    <xsl:with-param name="input" select="'a,b,c,d,e'"/>
    <xsl:with-param name="replacement">
    <option value='$x'>$x</option>
</xsl:with-param>
</xsl:call-template>

which will give me

<option value='a'>a</option><option value='b'>b</option><option value='c'>c</option><option value='d'>d</option><option value='e'>e</option>

Question targeted at XSLT 1.0 (but i don’t mind learning how XSLT 2.0 does it too)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T02:04:32+00:00Added an answer on May 23, 2026 at 2:04 am

    I. XSLT 2.0 solution

    <xsl:stylesheet version="2.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
     <xsl:template match="/">
      <xsl:variable name="vStr" select="'a,b,c,d,e'"/>
    
      <xsl:for-each select="tokenize($vStr, ',')">
       <option value="{.}"><xsl:value-of select="."/></option>
      </xsl:for-each>
     </xsl:template>
    </xsl:stylesheet>
    

    when this transformation is applied on ant XML document (not used), the wanted, correct result is produced:

    <option value="a">a</option>
    <option value="b">b</option>
    <option value="c">c</option>
    <option value="d">d</option>
    <option value="e">e</option>
    

    Explanation: Use of the standard XPath 2.0 function tokenize().

    II. XSLT 1.0: Using the FXSL 1.x str-split-to-words function/template

    This XSLT 1.0 transformation:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:ext="http://exslt.org/common"
     exclude-result-prefixes="ext">
      <xsl:import href="strSplit-to-Words.xsl"/>
      <xsl:output indent="yes" omit-xml-declaration="yes"/>
    
        <xsl:template match="/">
          <xsl:variable name="vwordNodes">
            <xsl:call-template name="str-split-to-words">
              <xsl:with-param name="pStr" select="/"/>
              <xsl:with-param name="pDelimiters"
                              select="', '"/>
            </xsl:call-template>
          </xsl:variable>
    
          <xsl:apply-templates select=
           "ext:node-set($vwordNodes)/*"/>
        </xsl:template>
    
        <xsl:template match="word">
         <option value="{.}"><xsl:value-of select="."/></option>
        </xsl:template>
    </xsl:stylesheet>
    

    when applied on this XML document:

    <t>a,b,c d,e</t>
    

    produces the wanted, correct result:

    <option value="a">a</option>
    <option value="b">b</option>
    <option value="c">c</option>
    <option value="d">d</option>
    <option value="e">e</option>
    

    Do note The pDelimiters parameter (as its name says) can hold more than one delimiting character — in this case we use both ',' and ' '.

    III. XSLT 1.0 solution using hand-written recursive named template:

    This transformation:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
     <xsl:template match="text()" name="tokenize">
      <xsl:param name="pText" select="."/>
      <xsl:param name="pDelim" select="','"/>
    
      <xsl:if test="string-length($pText) > 0">
        <xsl:variable name="vToken" select=
        "substring-before(concat($pText,','), ',')"/>
    
        <option value="{$vToken}">
          <xsl:value-of select="$vToken"/>
        </option>
    
        <xsl:call-template name="tokenize">
         <xsl:with-param name="pText" select=
          "substring-after($pText,',')"/>
        </xsl:call-template>
      </xsl:if>
     </xsl:template>
    </xsl:stylesheet>
    

    when applied on this XML document:

    <t>a,b,c,d,e</t>
    

    produces the wanted, correct result:

    <option value="a">a</option>
    <option value="b">b</option>
    <option value="c">c</option>
    <option value="d">d</option>
    <option value="e">e</option>
    

    IV. Passing to the tokenize template in III above as a parameter a template/function to process each token

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:f="http://fxsl.sf.net" exclude-result-prefixes="f">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
     <f:processToken/>
    
     <xsl:variable name="vFunc" select=
      "document('')/*/f:processToken[1]"/>
    
     <xsl:template match="text()" name="tokenize">
      <xsl:param name="pText" select="."/>
      <xsl:param name="pDelim" select="','"/>
      <xsl:param name="pProcessFunc" select="$vFunc"/>
    
      <xsl:if test="string-length($pText) > 0">
        <xsl:variable name="vToken" select=
        "substring-before(concat($pText,','), ',')"/>
    
        <xsl:apply-templates select="$pProcessFunc">
         <xsl:with-param name="arg1" select="$vToken"/>
        </xsl:apply-templates>
    
        <xsl:call-template name="tokenize">
         <xsl:with-param name="pText" select=
          "substring-after($pText,',')"/>
        </xsl:call-template>
      </xsl:if>
     </xsl:template>
    
     <xsl:template match="f:processToken">
      <xsl:param name="arg1"/>
        <option value="{$arg1}">
          <xsl:value-of select="$arg1"/>
        </option>
     </xsl:template>
    </xsl:stylesheet>
    

    when this transformation is applied to the same XML document as in III, the same, wanted, correct result is produced:

    <option value="a">a</option>
    <option value="b">b</option>
    <option value="c">c</option>
    <option value="d">d</option>
    <option value="e">e</option>
    

    Now, if we substitute the last template with this one:

     <xsl:template match="f:processToken">
      <xsl:param name="arg1"/>
        <p>
          <xsl:value-of select="$arg1"/>
        </p>
     </xsl:template>
    

    again the desired result is produced:

    <p>a</p>
    <p>b</p>
    <p>c</p>
    <p>d</p>
    <p>e</p>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement a function which would load a server-stored CSV file, and
I would like to implement a JTable which will only be edited and updated
I would like to implement something similar to a c# delegate method in PHP.
I would like to implement a data access object pattern in C++, but preferably
I would like to implement a command line interface for a Java application. This
I would like to implement an interactive evolutionary algorithm for generating music (probably just
I would like to implement a producer/consumer scenario that obeys interfaces that are roughly:
I would like to implement a post build event that performs the following actions
I would like to implement the method User.calculate_hashed_password . I'm trying to use the
I would like to implement something similar to 37Signals's Yellow Fade effect. I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.