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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:43:32+00:00 2026-06-14T23:43:32+00:00

I have several information entries that I want to separate with a comma. However,

  • 0

I have several information entries that I want to separate with a comma. However, each entry could be empty, and if the first-appearing entry is empty, then comma should not appear. For example:

if we have four XSLT parameters: name, phone number, address, occupation

and we have

  • Name: John
  • Phone number: 111-111-1111
  • Address: Imaginary street
  • Occupation: Baker

Then final string should be:

John, 111-111-1111, Imaginary street, Baker

if the name and phone number parameters were empty or null, then final string should be:

Imaginary street, Baker

if only phone number null or empty, then final string should be:

John, Imaginary street, Baker

In a language like C#, I would write the code like this:

foreach (EntryObject entry in entryList)
{
    if (firstEntry == true && entry.Type != EntryType.Age && entry.Type != EntryType.Sex)
    {
        finalString += entry.ValueString;
        firstEntry = false;
    }
    else if (firstEntry == false && entry.Type != EntryType.Age && entry.Type != EntryType.Sex)
    { 
        finalString += ", " + entry.ValueString;
    }
}
return finalString;

However, I heard that variables in XSLT are immutable. How should I approach this problem in XSLT?

Edit:
The xml entry would look something like this:

<AddressBook>
    <PersonalInfo>
        <Age>33</Age>
        <Sex>Male</Sex>
        <Name>John</Name>
        <PhoneNumber></PhoneNumber>
        <Address>Imaginary Street</Address>
        <Occupation>Baker</Occupation>
    </PersonalInfo>
</AddressBook>

Note that certain entries could be empty, and I will only use name, phonenumber, address and occupation. Age and Sex should be ignored.

  • 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-14T23:43:33+00:00Added an answer on June 14, 2026 at 11:43 pm

    Use an XPath condition that matches only the non-empy elements (string-length(.)>0 or simply string(.)), and then use the position() function to check if an element is the first or not. Input XML:

    <root>
      <item>
        <name>John</name>
        <phoneNumber>111-111-1111</phoneNumber>
        <address>Imaginary street</address>
        <occupation>Baker</occupation>
      </item>
      <item>
        <name>Jane</name>
        <address>Another street</address>
        <occupation>Decorator</occupation>
      </item>
      <item>
        <address>Unknown</address>
      </item>
    </root>
    

    XSLT:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
      <xsl:template match="item">
        <textItem>
          <xsl:for-each select="*[string(.)]">
            <xsl:if test="position()>1">
              <xsl:text>,</xsl:text>
            </xsl:if>
            <xsl:value-of select="."/>
          </xsl:for-each>
        </textItem>
      </xsl:template>
    
      <xsl:template match="text()">
      </xsl:template>
    
      <xsl:template match="root">
        <root>
          <xsl:apply-templates/>
        </root>
      </xsl:template>
    
    </xsl:stylesheet>
    

    where the first template is the one actually doing the work, <xsl:template match="text()"> removes the text contained in unmatched elements (by default XSLT processor would copy such text to the output) and <xsl:template match="root"> generates the root element of the output document.

    Result:

    <root>
      <textItem>John,111-111-1111,Imaginary street,Baker</textItem>
      <textItem>Jane,Another street,Decorator</textItem>
      <textItem>Unknown</textItem>
    </root>
    

    If you are interested in only some of the fields you just select them using the union operator (|) – e.g. if you want only phone, address and occupation in the example above modify the XSLT to be:

      <xsl:template match="item">
        <textItem>
          <xsl:for-each select="(phoneNumber|address|occupation)[string(.)]">
            <xsl:if test="position()>1">
              <xsl:text>,</xsl:text>
            </xsl:if>
            <xsl:value-of select="."/>
          </xsl:for-each>
        </textItem>
      </xsl:template>
    
      <xsl:template match="text()">
      </xsl:template>
    
      <xsl:template match="root">
        <root>
          <xsl:apply-templates/>
        </root>
      </xsl:template>
    
    </xsl:stylesheet>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Our scenario: We have a main database that stores company-wide information. We have several
I have written a console application in Delphi that queries information from several locations.
Here's the setup: I have several tables that hold information for data objects which
I have several Lua scripts that run experiences and output a lot of information,
I have several laptops in the field that need to daily get information from
I have several timers that poll information off the internet. When I get an
I'm using a UITextfield to input information. I have several UITextfields that need to
I have several web pages created that captures information from user inputs and stores
I have several functions in main that output information. I need to measure their
I have several DAO objects that are used to retrieve information from a database

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.