Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

how to break comma in xsl

Posted on 2004-09-09
2
Medium Priority
?
433 Views
Last Modified: 2008-01-09
hi

in xml i got node will come like this

<ROWSET>
<ROW>
<REASON_FOR_REJECTION>1.Overhead line is not existing in front of the premises,2.The distance from the pole to the serice is more than 30 meters.,3.Another service is existing in the same premises with arrears.</REASON_FOR_REJECTION>
</ROW>
</ROWSET>

In the above node for every comma i want to put <br>

the output should come like this:

1.Overhead line is not existing in front of the premises
2.The distance from the pole to the serice is more than 30 meters.
3.Another service is existing in the same premises with arrears

0
Comment
Question by:chaitu chaitu
2 Comments
 
LVL 21

Expert Comment

by:MogalManic
ID: 12014619
You are going to need a <xsl:template> that iterates through the string and returns a nodeset.  To do this you will need to implement recursion.  The algorithm will be something like this:
  1) Call template with String, delimiter(,)
  2) Split string into two parts:
       a) subString before first delimiter
       b) subString after first delimiter
  3) If delimiter is NOT found return String
  4) Otherwise,
       a) return String 1st String (2a)
       b) Recursively call template using 2nd String and delimeter

Here is my first stab:

<xsl:template name='SplitString>
  <xsl:param name='str/>
  <xsl:param name='delim'>,</xsl:param>

  <xsl:variable name='left' select='substring-before($str, $delim)'/>
  <xsl:variable name='right' select='substring-after($str, $delim)'/>
  <xsl:choose>
    <xsl:when test='string-length($left)=0>
        <xsl:value-of select='$str'/>
    </xsl:when>
    <xsl:otherwise>
         <xsl:value-of select='$left><br/>
          <xsl:call-template name='SplitString'>
              <xsl:with-param name='str' select='$right'/>
              <xsl:with-param name='delim' select='$delim'/>
          </xsl:call-template>
    </xsl:otherwise>
</xsl:template>
 
0
 
LVL 5

Accepted Solution

by:
conorj earned 1000 total points
ID: 12014651
To do this, or any string replacement where it isn't a one-to-one mapping, you need to use recursion. The following should do what you want.

rgds,
Conor.

XSLT:
.......
<xsl:template match="REASON_FOR_REJECTION">
    <xsl:call-template name="replace.text.with.elem">
        <xsl:with-param name="text" select="text()" />
        <xsl:with-param name="replace" select="','" />
        <xsl:with-param name="with" select="'br'" />
    </xsl:call-template>
</xsl:template>

<xsl:template name="replace.text.with.elem">
    <xsl:param name="text" select="''" />
    <xsl:param name="replace" select="''" />
    <xsl:param name="with" select="''" />
    <xsl:choose>
        <xsl:when test="contains($text, $replace)">
            <xsl:value-of select="substring-before($text, $replace)" />
            <xsl:element name="{$with}" />
            <xsl:call-template name="replace.text.with.elem">
                <xsl:with-param name="text" select="substring-after($text, $replace)" />
                <xsl:with-param name="replace" select="$replace" />
                <xsl:with-param name="with" select="$with" />
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$text" />
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>
........
0

Featured Post

Independent Software Vendors: 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!

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 …
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…

963 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