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
--~------------------------------------------------------------------
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>
--~--