Solved

how to break comma in xsl

Posted on 2004-09-09
2
430 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
[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
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 250 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

Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

617 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