[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: How to split text element to separate spans?

Subject: Re: How to split text element to separate spans?
From: Mukul Gandhi <gandhi.mukul@xxxxxxxxx>
Date: Mon, 7 Jun 2010 10:58:06 +0530
Re:  How to split text element to separate spans?
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.