0

We have string array in XML look like:

<arr name="CategoryName">
   <str>Movies</str>
   <str>Movies</str>
   <str>Movies</str>
   <str>Movies</str>
   <str>Movies</str>
   <str>DVD</str>
   <str>By Languages</str>
   <str>By Title</str>
   <str>By Decades</str>
   <str>By Genre</str>
   <str>2000's</str>
   <str>Drama</str>
   <str>English</str>
   <str>QRST</str>
</arr>

and my output from xslt transformation look like:

<category>MoviesMoviesMoviesMoviesMoviesDVDBy LanguagesBy TitleBy DecadesBy Genre2000'sDramaEnglishQRST</category>

but i want to each element with comma separated like:

<category>Movies, Movies, Movies, Movies, Movies, DVD, By Languages, By Title, By Decades, By Genre, 2000's,Drama,English,QRST</category>

And my XSLT code look like:

<xsl:variable name="CategoryArray" select ="arr[@name = 'CategoryName']"/>

 <xsl:for-each select="$CategoryArray">
        <category><xsl:value-of select="concat($CategoryArray,', ')"/></category>
    </xsl:for-each>

but its not working so pls give me solution for how make array value with comma separated.

thanks in advance.

2 Answers 2

2

You can replace this:

<xsl:for-each select="$CategoryArray">
    <category><xsl:value-of select="concat($CategoryArray,', ')"/></category>
</xsl:for-each>

with this:

<category><xsl:value-of select="$CategoryArray" separator=", "/></category>
Sign up to request clarification or add additional context in comments.

4 Comments

Don't tell us things aren't working, tell us how they fail. Are you sure you are using an XSLT 2.0 processor? (Lots of people seem to think that saying version="2.0" in the stylesheet will magically give them a 2.0 processor).
I face very similar issue, can you advise How can make sure I'm using xslt 2.0? is there any way to findout that?
How can be sure i am using xlst 2.0? can you pls tell me how to check version of xslt?
You can check the version using system-property('xsl:version').
1

It's a little hard to tell what your code is doing without seeing how $CategoryArray is defined. However, there are 2 main problems with your code:

  1. The xsl:for-each needs to go inside the category element, because you only want one of these elements for every array
  2. Using your code, the list will end with a comma. To do this, you have to process the first item separately.

You can use this XSLT to do this:

<category>
  <xsl:if test="arr/str">
    <!-- Process the first item separately -->
    <xsl:value-of select="arr/str[1]"/>
    <!-- Process the rest of the items, skipping the first using position() -->
    <xsl:for-each select="arr/str[position() != 1]">
      <xsl:value-of select="concat(', ',.)"/>
    </xsl:for-each>
  </xsl:if>
</category>

4 Comments

I see. Your code will concat all elements and apply a single comma at the very end (not sure why you're not seeing it, it should be there)
This is XSLT 2.0, so there are much easier solutions.
@MichaelKay Right, I didn't notice the tag. I just assumed it was 1.1, as the support for 2.0 is so bad (in my experience, at least...)
Depends where your experience is. It's true there are many environments were 2.0 is not readily available (e.g. PHP). But I'm surprised you mention 1.1: that's completely dead. You're using 1.0 if you're a laggard, 2.0 if you're mainstream, 3.0 if you're bleeding edge.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.