Solved

how to break comma in xsl

Posted on 2004-09-09
2
418 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
Comment Utility
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
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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 Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now