Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

XSLT Recursive Processing and Lists

Posted on 2006-04-19
8
Medium Priority
?
228 Views
Last Modified: 2008-03-17
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.
0
Comment
Question by:Goddess6942
  • 5
  • 3
8 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 2000 total points
ID: 16487915
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
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 16487956
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
 

Author Comment

by:Goddess6942
ID: 16488255
Thanks so much Gertone, you rock!
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 60

Expert Comment

by:Geert Bormans
ID: 16488302
welcome
0
 

Author Comment

by:Goddess6942
ID: 16488900
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
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 16489103
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
 

Author Comment

by:Goddess6942
ID: 16489954
Gertone, that did it!!!  Thanks so much!
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 16489992
welcome
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
Integration Management Part 2
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses
Course of the Month20 days, 21 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question