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

Re: Split element with mixed content

Subject: Re: Split element with mixed content
From: Abel Braaksma <abel.online@xxxxxxxxx>
Date: Thu, 02 Aug 2007 18:49:50 +0200
Re:  Split element with mixed content
Kevin Bird wrote:
What is the best method for splitting an element that has mixed content?

Intriguing: what is 'best' can be an interesting debate ;)


<root>
    <p>The quick <b>brown</b> fox jumped over the lazy dog.</p>
</root>

In the above example I want to split on the word 'over'.

And your problem is, obviously, that you want to treat it as string, but then you would loose the inner elements. Here's one go at your problem, but it is not really trivial and I am certain this can be done more easily.


I took your idea of mixed content a bit further. Here's the input:

<p>this
   <b>
       quick
   </b>
   brown fox
   <span>
       <style>
           jumps
           <b>
               very far over the beautiful
           </b>
           and stunningly
       </style>
       resilient,
   </span>
   utterly lazy
   <q>
       dog
   </q>
</p>


And following are the, I admit rather wieldy, templates. I don't have the time to further pretty print them, so you probably have to make something of it after the mailer messes it... I am sure that the code can be written much easier, but this is the best I could come up with in a short period of time (and yes, the parentheses in the substring-before xpath are on purpose, and yes, you can replace self::node() with .) :


<xsl:template match="p">
   <xsl:apply-templates select="node() | @*" mode="#current" />
</xsl:template>

<xsl:template match="text()[contains(., 'over')]">
   <xsl:apply-templates select="ancestor::p" mode="split-before" >
       <xsl:with-param name="split-by" select="current()" tunnel="yes" />
   </xsl:apply-templates>
   <xsl:apply-templates select="ancestor::p" mode="split-after" >
       <xsl:with-param name="split-by" select="current()" tunnel="yes" />
   </xsl:apply-templates>
</xsl:template>

<xsl:template match="node()" mode="split-before">
<xsl:param name="split-by" tunnel="yes"/>
<xsl:copy>
<xsl:apply-templates select="node()[not(preceding::node()[. is $split-by])][not(self::node() is $split-by)]" mode="#current" />
<xsl:copy-of select="node()[self::node() is $split-by]/(substring-before(., 'over'), 'over')" />
</xsl:copy>
</xsl:template>


<xsl:template match="node()" mode="split-after">
<xsl:param name="split-by" tunnel="yes"/>
<xsl:copy>
<xsl:copy-of select="node()[self::node() is $split-by]/substring-after(., 'over')" />
<xsl:apply-templates select="node()[not(following::node()[. is $split-by])][not(self::node() is $split-by)]" mode="#current" />
</xsl:copy>
</xsl:template>



<xsl:template match="text()" priority="0" />





Cheers & happy coding,


-- Abel Braaksma

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.