XSLT Recursive Processing and Lists

I have the following in my XML

<list>dept1,dept2,dept3</list>

I know with <xsl:value-of select="substring-before(., ',')"/> I can pull dept1 out from the list.  I also know that through some form of recursive processing I can pull dept2 and dept3 as well.  What I can't figure out, despite doing some looking around, is how to approach this.  Any ideas?

Thanks in advance.
Goddess6942Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Geert BormansInformation ArchitectCommented:
Hi Goddess6942,

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output indent="yes"/>
    <xsl:template match="/">
        <list>
            <xsl:call-template name="processList">
                <xsl:with-param name="myList" select="//list"/>
            </xsl:call-template>
        </list>
    </xsl:template>
    <xsl:template name="processList">
        <xsl:param name="myList"/>
        <xsl:choose>
            <xsl:when test="contains($myList, ',')">
                <item><xsl:value-of select="normalize-space(substring-before($myList, ','))"/></item>
               <xsl:call-template name="processList">
                   <xsl:with-param name="myList" select="normalize-space(substring-after($myList, ','))"></xsl:with-param>
               </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <item><xsl:value-of select="normalize-space($myList)"/></item>
            </xsl:otherwise>
        </xsl:choose>
       
    </xsl:template>
</xsl:stylesheet>


Cheers!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Geert BormansInformation ArchitectCommented:
Goddess6942,

The Stylesheet has a named template (which is a bit like a function)
you can pass a parameter myList to this template, I select the value "//list"

The template checks there is a "," in the argument
if there is, the part before the "," is put in an item (after normalising the space = stripping space before and after)
The part after the comma, is passed again to the template... this is the recursive part

If there is no comma left any more, the remainder is itemised

you can of course easily replace the <item> elements with different constructs

cheers

Geert
0
Goddess6942Author Commented:
Thanks so much Gertone, you rock!
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Geert BormansInformation ArchitectCommented:
welcome
0
Goddess6942Author Commented:
Gertone,

Is there a way to match a variable to a list of items?  So far the recursion works great but I've noticed if I'm trying to match a variable to an of the items returned by the recursion, the variable only gets compared to the first list item.  Here's the code I have:

      <xsl:template name="processSelectList">
            <xsl:param name="deptList"/>
            <xsl:param name="selectValue"/>

        <xsl:choose>
            <xsl:when test="contains($deptList, '|')">
                     <xsl:if test="normalize-space(substring-before($deptList, '|')) = $selectValue">w00t!</xsl:if>
               <xsl:call-template name="processSelectList">
                   <xsl:with-param name="deptList" select="normalize-space(substring-after($deptList, '|'))"></xsl:with-param>
               </xsl:call-template>  
            </xsl:when>
            <xsl:otherwise>
                        <xsl:if test="normalize-space($deptList) = $selectValue">w00t!</xsl:if>
            </xsl:otherwise>
        </xsl:choose>
      </xsl:template>

Any ideas?
0
Geert BormansInformation ArchitectCommented:
I think you are passing this variable with the first call to the template
and you don't pass it with the other calls.
Is that correct?

If it is, here is the changed part of the when clause
          <xsl:when test="contains($deptList, '|')">
                  <xsl:if test="normalize-space(substring-before($deptList, '|')) = $selectValue">w00t!</xsl:if>
               <xsl:call-template name="processSelectList">
                   <xsl:with-param name="deptList" select="normalize-space(substring-after($deptList, '|'))"></xsl:with-param>
                   <xsl:with-param name="selectValue" select="$selectValue"/>
               </xsl:call-template>  
            </xsl:when>
I just copy the value from the one that is passed in via the first call

or make it global, put the selectValue definition on top and remove it from the template

cheers

Geert
0
Goddess6942Author Commented:
Gertone, that did it!!!  Thanks so much!
0
Geert BormansInformation ArchitectCommented:
welcome
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.