[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

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


Subject: Re: [xsl] How do I capture the text "around" a node?
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Tue, 24 Sep 2002 14:47:51 +0100

Hi Peter,

> I'm beating my head against a wall here, and haven't been able to
> work out how to capture the text around a node.
>
> Here is the snippet of xml:
>
> <book isbn="88888">
> This book <title>Lord of the rings</title> is often misquoted.
> ...
> </book>
>
> Now I have tried to work out how, but failed miserably and would
> appreciate some help, or even better an explaination as to why/how
> to do this.
>
> Here is my attempt to do it:
>
> <xsl:template match="book">
>
>         <fo:block font-size="12pt" font-family="serif" line-height="16pt">
>
> <xsl:variable name="txtFirst" select='substring-before( /book/text(), "(" )' />
>             <xsl:value-of select="$txtFirst" />
>             <xsl:value-of select="title" />
>             <xsl:text>After this is something:</xsl:text>
>             <xsl:value-of select="/child::title/child::text()" />
>
>      </fo:block>
>
> </xsl:template>
>
> When I do just variable name="txt" select="text()" i only get all
> text up to the title tag. How can I get the text after the title
> node?

You could use:

  <xsl:apply-templates select="title/following-sibling::text()" />

in other words, get the title element child of the book element, and
from there get the following siblings of the title element.

But perhaps it would be easier to use templates and do something like:

<xsl:template match="book">
  <fo:block font-size="12pt" font-family="serif" line-height="16pt">
    <xsl:apply-templates />
  </fo:block>
</xsl:template>

<xsl:template match="title">
  <xsl:value-of select="." />
  <xsl:if test="following-sibling::text()">
    <xsl:text>After this is something:</xsl:text>
  </xsl:if>
</xsl:template>

In other words, process the content of the book element; when you come
across a 'title' element then add the string "After this is
something:" if it has any text following it.

Cheers,

Jeni

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


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



Current Thread