xsl-list
[Top] [All Lists]

Re: [xsl] How to split text element to separate spans?

2010-06-07 06:36:39
Thank you for your answer Mukul.
It does put the br between the spans but lose the spaces between spans
and replace them with br.

The result of the code you sent gives the following output:

<p dir="ltr"><span class="smaller">text1</span><br /><span
class="smaller">text2 text3.</span><br /><span
class="smalleritalic">no</span><br /><span
class="smaller">problems.</span><br /><br /></p>

The desired one is:
<p dir="ltr"><span class="smaller">text1</span>
           <br />
            <span class="smaller">text2 text3.</span>
           <br />
           <span class="smalleritalic">no</span> <span
class="smaller">problems.</span>
           <br />
           <br />
           </p>

Instead of <span class="smalleritalic">no</span> <span
class="smaller">problems.</span>
I get
<span class="smalleritalic">no</span><br /><span
class="smaller">problems.</span>

*** Note: <br /> instead of space between the spans. ***

Another issue is :
In case of a line like this:
<p dir="ltr">  <span class="regular">"What else?"</span></p>
I get the following:
<p dir="ltr"><span class="regular">"What else?"</span><br /></p>

Lose the indentation spaces a head of the span and have extra br at its end.

I tried to remove the normalize-space in order to preserve the spaces
but it didn't help.

Any idea how to finalize this?

Thanks, Israel

On Mon, Jun 7, 2010 at 8:28 AM, Mukul Gandhi 
<gandhi(_dot_)mukul(_at_)gmail(_dot_)com> wrote:
Given your input document, the following 2.0 stylesheet produces the
desired output:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                      version="2.0">

 <xsl:output method="xml" indent="yes" />

 <xsl:template match="p">
     <p>
         <xsl:copy-of select="@*" />
         <xsl:apply-templates select="span" />
     </p>
 </xsl:template>

 <xsl:template match="span">
    <xsl:variable name="attrs" select="@*" />
    <xsl:for-each-group select="text() | *" group-starting-with="br">
         <xsl:choose>
             <xsl:when
test="not(normalize-space(string(string-join(current-group(),''))) =
'')">
                <span>
                   <xsl:copy-of select="$attrs" />
                   <xsl:copy-of
select="normalize-space(string(string-join(current-group(),' ')))" />
                </span>
                <br/>
             </xsl:when>
             <xsl:otherwise>
                 <xsl:if test="position() != last()">
                    <br/>
                 </xsl:if>
             </xsl:otherwise>
         </xsl:choose>
     </xsl:for-each-group>
 </xsl:template>

</xsl:stylesheet>

On Sun, Jun 6, 2010 at 9:12 PM, Israel Viente 
<israel(_dot_)viente(_at_)gmail(_dot_)com> wrote:
Hi,
I have a problem splitting spans with text elements separated by br,
to different spans with br in between them.

Example Input:

<p dir="ltr"><span class="smaller">text1
           <br />
            text2
           text3.
           <br />
           </span> <span class="smalleritalic">no</span> <span
class="smaller">problems.
           <br />


           <br />
           </span></p>

Desired output:

<p dir="ltr"><span class="smaller">text1</span>
           <br />
            <span class="smaller">text2 text3.</span>
           <br />
           <span class="smalleritalic">no</span> <span
class="smaller">problems.</span>
           <br />
           <br />
           </p>
 Note: I need to create new span and preserve the span class when br
breaks inside a span.

Thanks for any idea.
Israel



--
Regards,
Mukul Gandhi

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--