xsl-list
[Top] [All Lists]

Re: How do I capture the text "around" a node?

2002-09-24 07:50:23
Hi Peter,

Acutally, I should have added a bit more:

<?xml version="1.0" ?>
<book isbn="1234567890">
Its title (<title>The Lord of the Rings</title>)
is often misquoted!
<author>J R R Tolkien</author>
...
</book>
[snip]
What I'm trying to do is actually capture the text using variables
and substring-before and substring after, to get rid of the chars
().

Right. If you don't really care about retaining the structure (the
<title> and <author> elements) in the <book>, then the simplest thing
is just:

<xsl:template match="book">
  <xsl:value-of select="translate(., '()', '')" />
</xsl:template>

If you do want to keep the structure in some way, the best thing to do
is to use xsl:apply-templates and then use templates that remove ()s
from text nodes. So do:

<xsl:template match="text()">
  <xsl:value-of select="translate(., '()', '')" />
</xsl:template>

If you want to make sure that you're only removing a ( at the end of a
text node that's immediately followed by a title element or at the
beginning of a text node that's immediately preceded by a title
element, then do something like:

<xsl:template match="text()[preceding-sibling::*[1][self::title] and
                            starts-with(., ')')]">
  <xsl:value-of select="substring-after(., ')')" />
</xsl:template>

<xsl:template match="text()[following-sibling::*[1][self::title] and
                            not(substring-after(., '('))]">
 <xsl:value-of select="substring-before(., '(')" />
</xsl:template>

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list