Re: [xsl] Extracting text between nodes

Subject: Re: [xsl] Extracting text between nodes
From: "Sam Byland" <shbyland@xxxxxxxxxxx>
Date: Wed, 13 Feb 2008 16:16:21 -0500
Wow. Kind of rare when I check this list and find an unanswered question :-)


<td><font><b>Title</b><br/>Some Text<i> and some more italic text</i><b> maybe even some more</b><a href="http://whatever.com";>And an anchor</a></font></td>


I want to extract only the text between the first <br/> tag and the last <a> anchor tag, without the anchor's text but including all text in child elements such as the <i> and <b> - "Some Text and some more italic text maybe even some more"

How can I do it with xsl?

There are a couple ways you can do this -- the best way will depend on how your HTML will vary, or will it always be like your example. I would just use a template rule as follows:


<xsl:template match="a | b[position() = 1]"/>

A stylesheet with only the above template rule in it will output the following:

_____________

Some Text and some more italic text

maybe even some more
_____________


i.e. by default the text will be copied to the output, so I only specified those elements where you did *not* want the text copied to the output to "do nothing". In this case, any a element, and only the first b element, will not get their text copied to the output....


Hope that helps,

...sam

Current Thread