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 8148695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:41:47+00:00 2026-06-06T14:41:47+00:00

Using XSL 1.0, I found a nice tokenize function. Now I need to for-each

  • 0

Using XSL 1.0, I found a nice tokenize function. Now I need to for-each over the resulting tokens. I am very new to XSL.

The php equivalent of what I need:

$in = 'a,b,c,d';
$tokens = explode (',', $in);
foreach ($tokens as $token) {
  echo $token;
}

Here’s what I have so far. This line will output ‘a,b,c,d’-

<xsl:value-of select="@CommaSeparated" />

This will run that string through the tokenize function-

  <xsl:call-template name="tokenize">
    <xsl:with-param name="pText" select="@CommaSeparated"/>
  </xsl:call-template>

And the tokenize function. I understand what this does, just not the format of the data it spits out-

<xsl:template name="tokenize">
  <xsl:param name="pText"/>

  <xsl:if test="string-length($pText)">
    <tag>
      <xsl:value-of select=
       "substring-before($pText, ',')"/>
    </tag>

    <xsl:call-template name="tokenize">
      <xsl:with-param name="pText" select=
       "substring-after($pText, ',')"/>
    </xsl:call-template>
  </xsl:if>
</xsl:template>
  • 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-06-06T14:41:48+00:00Added an answer on June 6, 2026 at 2:41 pm

    Firstly, the tokenize function are using is not actually correct. In theory, it should spit out a list of tag elements (strictly speaking a “result tree fragment”) like so

    <tag>a</tag>
    <tag>b</tag>
    <tag>c</tag>
    <tag>d</tag>
    

    But it is actually missing out the last element

    <tag>a</tag>
    <tag>b</tag>
    <tag>c</tag>
    <tag></tag>
    

    You are probably better off finding another tokenize function here (there must be surely working ones here in StackOverflow).

    But, in answer your question about using xsl:for-each over this, you might be tempted to do something like this…

      <xsl:variable name="tags">
        <xsl:call-template name="tokenize">
           <xsl:with-param name="pText" select="@CommaSeparated"/>
         </xsl:call-template> 
      </xsl:variable>
    
      <xsl:for-each select="$tags/tag">
         <xsl:copy-of select="." />
      </xsl:for-each>
    

    That is to say, store the list of tags in a variable, and then loop over them. However, if you try this in XSLT1.0 you would get an error “Expression must evaluate to a node-set.”. To get around this, you need to use an extension function. EXSLT is probably the most common. You would declare this in your XSLT like so

    <xsl:stylesheet 
        version="1.0" 
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
        xmlns:exsl="http://exslt.org/common" 
        exclude-result-prefixes="exsl">
    

    You could them simply change the xsl:for-each as follows:

    <xsl:for-each select="exsl:node-set($tags)/tag">
    

    So, given the following XML

    <a CommaSeparated="a,b,c,d"></a>
    

    And the following XSLT

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exsl="http://exslt.org/common" exclude-result-prefixes="exsl">
        <xsl:output method="xml" indent="yes"/>
    
        <xsl:template match="/a">
            <xsl:variable name="tags">
                <xsl:call-template name="tokenize">
                    <xsl:with-param name="pText" select="@CommaSeparated"/>
                </xsl:call-template>
            </xsl:variable>
            <xsl:for-each select="exsl:node-set($tags)/tag">
                <xsl:copy-of select="."/>
            </xsl:for-each>
        </xsl:template>
    
        <xsl:template name="tokenize">
            <xsl:param name="pText"/>
            <xsl:if test="string-length($pText)">
                <tag>
                    <xsl:value-of select="substring-before($pText, ',')"/>
                </tag>
                <xsl:call-template name="tokenize">
                    <xsl:with-param name="pText" select="substring-after($pText, ',')"/>
                </xsl:call-template>
            </xsl:if>
        </xsl:template>
    </xsl:stylesheet>
    

    The following is output (with the last tag blank due to the bugged tokenize function you are using)

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

Sidebar

Related Questions

I'm using a system which consists of many complex XSL transforms which each work
I'm using Saxon-EE 9.3.0.4 for xsl transformations and found that when <xsl:result-document> is used
I am having problem transforming xml to xml using xsl. I found out where
I need to transform one XML document into another using XSLT (for now from
I am using xsl-fo to render a PDF. I need to create a table
I have been using XSL-FO and FOP Engine to generate PDF documents for required
I'm trying to transform one XML format to another using XSL. Try as I
How can I retrieve the XML source file name using XSL 1.0 code?
I am trying to remove the attribute xmlns=http://webdev2003.test.com from the following xml using xsl/xslt,
XSLT / XML: convert apostrophe to a space My XML <Name>KUEN'S INTERNATIONAL</Name> USING XSL

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.