Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

xslt loop on a semicolon seperated list

Posted on 2010-08-23
7
Medium Priority
?
681 Views
Last Modified: 2013-11-18
I will have a value that is a semi colon delimited string.
<xsl:value-of select="@Keyword"/>

keyword1;keyword2;keyword3

I want something in my xslt that will look and create list items out of the values that are seperated by semi-colons.

So
keyword1;keyword2;keyword3

Would be
<ul>
  <li>keyword1</li>
  <li>keyword2</li>
  <li>keyword3</li>
</ul>

Thanks
0
Comment
Question by:jackjohnson44
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
7 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 33504594
you will need some recursive processing in XSLT1
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl" version="1.0">
    <xsl:variable name="keyword">keyword1;keyword2;keyword3</xsl:variable>
    <xsl:template match="/">
        <ul>
            <xsl:call-template name="splitter">
                <xsl:with-param name="str" select="$keyword"/>
                <xsl:with-param name="sep" select="';'"/>
            </xsl:call-template>
        </ul>
    </xsl:template>
<xsl:template name="splitter">
    <xsl:param name="str"/>
    <xsl:param name="sep"/>
    <xsl:choose>
        <xsl:when test="contains($str, $sep)">
            <li><xsl:value-of select="substring-before($str, $sep)"/></li>
            <xsl:call-template name="splitter">
                <xsl:with-param name="str" select="substring-after($str, $sep)"/>
                <xsl:with-param name="sep" select="$sep"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <li><xsl:value-of select="$str"/></li>
        </xsl:otherwise>
    </xsl:choose>
    
</xsl:template></xsl:stylesheet>

Open in new window

0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 33504609
of course, it is a lot easier if you could use XSLT2
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="2.0">
    <xsl:variable name="keyword">keyword1;keyword2;keyword3</xsl:variable>
    <xsl:template match="/">
        <ul>
            <xsl:for-each select="tokenize($keyword, ';')">
                <li><xsl:value-of select="."/></li>
            </xsl:for-each>
        </ul>
    </xsl:template>
</xsl:stylesheet>

Open in new window

0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 33504617
Note that I pasted the string in a variable, ofcourse you can call the tokenize() or the named template directly on the attribute
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:jackjohnson44
ID: 33505023
Thanks, but I am not exactly understanding.  I tried your code in mine and it has a problem putting an xsl:template inside another template.  Also, what did you say about tokenize?  Sorry, I am really having trouble with this.

how could I do it with
<xsl:value-of select="@Keyword"/>
<xsl:template name="dvt_1.rowview">
<tr>
<td>			
<table border="1" cellspacing="0" width="100%">
<tr>
<td>
Title: <xsl:value-of select="@Title"/><br />
Description: <xsl:value-of select="@KpiDescription"/><br />
Keyword: <xsl:value-of select="@Keyword"/><br />

<xsl:variable name="keyword">keyword1;keyword2;keyword3</xsl:variable>
<xsl:template match="/">
<ul>
<xsl:for-each select="tokenize($keyword, ';')">
<li><xsl:value-of select="."/></li>
</xsl:for-each>
</ul>
</xsl:template>

Open in new window

0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 2000 total points
ID: 33505268
mmh, am I wrong in assuming you are a real beginner?

templates can not be nested
you should put them next to each-other

This sounds like sharepoint, is it?
So you are bound to XSLT1, you cannot use tokenize() then

Here is how you should organize the templates
    <xsl:template name="dvt_1.rowview">
        <tr>
            <td>			
                <table border="1" cellspacing="0" width="100%">
                    <tr>
                        <td>
                            Title: <xsl:value-of select="@Title"/><br />
                            Description: <xsl:value-of select="@KpiDescription"/><br />
                            Keyword: <br />
                            <ul>
                                <xsl:call-template name="splitter">
                                    <xsl:with-param name="str" select="@Keyword"/>
                                    <xsl:with-param name="sep" select="';'"/>
                                </xsl:call-template>
                            </ul>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </xsl:template>
    
    <xsl:template name="splitter">
        <xsl:param name="str"/>
        <xsl:param name="sep"/>
        <xsl:choose>
            <xsl:when test="contains($str, $sep)">
                <li><xsl:value-of select="substring-before($str, $sep)"/></li>
                <xsl:call-template name="splitter">
                    <xsl:with-param name="str" select="substring-after($str, $sep)"/>
                    <xsl:with-param name="sep" select="$sep"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <li><xsl:value-of select="$str"/></li>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

Open in new window

0
 

Author Comment

by:jackjohnson44
ID: 33505551
Yes SharePoint, YES to beginner (at least with xslt).  I would much rather do this via code instead of xslt.  Thanks, I will try this.
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 33505630
oh, you could do this with javascript inside your XSLT since you are using the microsoft XSLT processor,
but that would be even more tricky
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

There are two main kinds of selectors in CSS: One is base selector like h1, h2, body, table or any existing HTML tags.  For instance, the following rule sets all paragraphs (<p> elements) to red: (CODE) CSS also allows us to define our own custom …
I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

610 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