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 10:58:06 +0530
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.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