[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

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


Subject: Re: [xsl] How to split text element to separate spans?
From: Mukul Gandhi <gandhi.mukul@xxxxxxxxx>
Date: Mon, 7 Jun 2010 18:35:07 +0530

I think, the answer I posted would help you to get started, to produce
the desired output.

I would want you to proceed yourself from here onward. Please let us
know if anything is not clear from XSLT language perspective, and I'm
sure folks here would answer the questions.

On Mon, Jun 7, 2010 at 5:06 PM, Israel Viente <israel.viente@xxxxxxxxx>
wrote:
> 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>
>>> B  B  B  B  B  B <br />
>>> B  B  B  B  B  B  <span class="smaller">text2 text3.</span>
>>> B  B  B  B  B  B <br />
>>> B  B  B  B  B  B <span class="smalleritalic">no</span> <span
>>> class="smaller">problems.</span>
>>> B  B  B  B  B  B <br />
>>> B  B  B  B  B  B <br />
>>> B  B  B  B  B  B </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">B B <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.mukul@xxxxxxxxx>
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"
>> B  B  B  B  B  B  B  B  B  B  B  version="2.0">
>>
>> B <xsl:output method="xml" indent="yes" />
>>
>> B <xsl:template match="p">
>> B  B  B <p>
>> B  B  B  B  B <xsl:copy-of select="@*" />
>> B  B  B  B  B <xsl:apply-templates select="span" />
>> B  B  B </p>
>> B </xsl:template>
>>
>> B <xsl:template match="span">
>> B  B  <xsl:variable name="attrs" select="@*" />
>> B  B  <xsl:for-each-group select="text() | *" group-starting-with="br">
>> B  B  B  B  B <xsl:choose>
>> B  B  B  B  B  B  B <xsl:when
>> test="not(normalize-space(string(string-join(current-group(),''))) =
>> '')">
>> B  B  B  B  B  B  B  B  <span>
>> B  B  B  B  B  B  B  B  B  B <xsl:copy-of select="$attrs" />
>> B  B  B  B  B  B  B  B  B  B <xsl:copy-of
>> select="normalize-space(string(string-join(current-group(),' ')))" />
>> B  B  B  B  B  B  B  B  </span>
>> B  B  B  B  B  B  B  B  <br/>
>> B  B  B  B  B  B  B </xsl:when>
>> B  B  B  B  B  B  B <xsl:otherwise>
>> B  B  B  B  B  B  B  B  B <xsl:if test="position() != last()">
>> B  B  B  B  B  B  B  B  B  B  <br/>
>> B  B  B  B  B  B  B  B  B </xsl:if>
>> B  B  B  B  B  B  B </xsl:otherwise>
>> B  B  B  B  B </xsl:choose>
>> B  B  B </xsl:for-each-group>
>> B </xsl:template>
>>
>> </xsl:stylesheet>
>>
>> On Sun, Jun 6, 2010 at 9:12 PM, Israel Viente <israel.viente@xxxxxxxxx>
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
>>> B  B  B  B  B  B <br />
>>> B  B  B  B  B  B  text2
>>> B  B  B  B  B  B text3.
>>> B  B  B  B  B  B <br />
>>> B  B  B  B  B  B </span> <span class="smalleritalic">no</span> <span
>>> class="smaller">problems.
>>> B  B  B  B  B  B <br />
>>>
>>>
>>> B  B  B  B  B  B <br />
>>> B  B  B  B  B  B </span></p>
>>>
>>> Desired output:
>>>
>>> <p dir="ltr"><span class="smaller">text1</span>
>>> B  B  B  B  B  B <br />
>>> B  B  B  B  B  B  <span class="smaller">text2 text3.</span>
>>> B  B  B  B  B  B <br />
>>> B  B  B  B  B  B <span class="smalleritalic">no</span> <span
>>> class="smaller">problems.</span>
>>> B  B  B  B  B  B <br />
>>> B  B  B  B  B  B <br />
>>> B  B  B  B  B  B </p>
>>> B 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


Current Thread
Keywords