Solved

how to break comma in xsl

Posted on 2004-09-09
2
429 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

752 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