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

  • SEARCH
  • Home
  • 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 8464499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:45:51+00:00 2026-06-10T14:45:51+00:00

I am new to stackoverflow and would appreciate any help. It is a XSLT

  • 0

I am new to stackoverflow and would appreciate any help.
It is a XSLT 1.0 problem I have encountered and I’m sure there is a simple solution I just can’t find it.

I have top book element containing many chapters that each have a title:

<?xml version="1.0" encoding="utf-8"?>
<Book>
  <Chapter>
    <Title>ONE</Title>
  </Chapter>
  <Chapter>
    <Title>TWO</Title>
  </Chapter>
  <Chapter>
    <Title></Title>
  </Chapter>
  <Chapter>
    <Title>FOUR</Title>
  </Chapter>
</Book>

I want to transform the above into one element called CSV-Title-List. It seemed easy enough until the case of an empty Title occurred. I get an unexpected comma using the following stylesheet:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="Book">
      <CSV-Title-List>
      <xsl:for-each select="Chapter/Title">
        <xsl:value-of select="."/>
        <xsl:if test="position() &lt; last()">
          <xsl:text>, </xsl:text>
        </xsl:if>
      </xsl:for-each>
      </CSV-Title-List>
    </xsl:template>   
</xsl:stylesheet>

My question is, how can I cleanly check the next Chapter/Title for a null string before outputting the comma with the above stylesheet. There must be a better way? Any suggestions would be appreciated. Thanks to any advice.

  • 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-10T14:45:53+00:00Added an answer on June 10, 2026 at 2:45 pm

    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:strip-space elements="*"/>
    
     <xsl:template match="/*">
      <xsl:apply-templates select="Chapter[normalize-space(Title)]"/>
     </xsl:template>
    
     <xsl:template match="Chapter">
      <xsl:if test="not(position()=1)">,</xsl:if>
      <xsl:value-of select="Title"/>
     </xsl:template>
    </xsl:stylesheet>
    

    when applied on the provided XML document:

    <Book>
      <Chapter>
        <Title>ONE</Title>
      </Chapter>
      <Chapter>
        <Title>TWO</Title>
      </Chapter>
      <Chapter>
        <Title></Title>
      </Chapter>
      <Chapter>
        <Title>FOUR</Title>
      </Chapter>
    </Book>
    

    produces the wanted, correct result:

    ONE,TWO,FOUR
    

    When the same transformation is applied on a different XML document — in which the first /*/Chapter/Title has a white-space-only string value:

    <Book>
      <Chapter>
        <Title>     </Title>
      </Chapter>
      <Chapter>
        <Title>TWO</Title>
      </Chapter>
      <Chapter>
        <Title></Title>
      </Chapter>
      <Chapter>
        <Title>FOUR</Title>
      </Chapter>
    </Book>
    

    again the correct, wanted result is produced:

    TWO,FOUR
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an issue that I would appreciate any help with. Background - I
Hi everyone I am new to StackOverflow and SQL. I am not sure how
I'm new here to stackoverflow, so bear with me. I have a book that
I'm new to SubSonic (and stackoverflow!) and I just wanted to leave a quick
I'm new to programming and stackoverflow and have decided to start by learning objective
Dear stackoverflow members, I have a small problem, I want to replace some characters
I'm playing a little bit with the new StackOverflow API . Unfortunately, my JSON
I am new to stackoverflow as a member, although I follow this a lot
I'm new to StackOverflow and VBA. I am an Expert with all aspects of
Hi i am new to stackoverflow. I am developing an xmpp client application for

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.