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

Re: [xsl] Testing for following text()


Subject: Re: [xsl] Testing for following text()
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Wed, 31 Oct 2007 13:20:44 -0400

At 2007-10-31 17:59 +0100, Sascha Mantscheff wrote:
How can I test if there is a text node following and restrict the test
to a certain parent element?

By comparing ancestors.


In the example I'd like to test if there is text following after the
<s/> element up to the enclosing end tag of the <b> element.

So, then, this <b> ancestor has to be common between the node you are at, and the nodes being inspected for being non-empty.


This test should fire at the first s element because of the following
BBB text, but not at the second s element in spite of the following AAA
text.
The structure below the b element may contain any number of nesting
levels and element.

Which is why one is dealing with the ancestor.


I'd like to include the test in a template which tackles the s element,
like <xsl:template match="s[some-test]">

The solution below addresses this, by first filtering following text nodes for having the common ancestor, and then filtering those for being non-white-space-only.


It is using XSLT 1.0 ... you don't say which version you are using.

Note that on large data sets this may be slow because of the use of the following:: axis.

I hope this helps.

. . . . . . . . . . Ken

t:\ftemp>type sascha.xml
<a>
  aaa
  <b>
      bbb
      <c>
          ccc
          <s/>
      </c>
      BBB
  </b>
  AAA
  <b>
      bbb
      <c>
          ccc
          <s/>
      </c>
  </b>
   AAA
</a>

t:\ftemp>type sasha.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">

<xsl:output method="text"/>

<xsl:template match="/">
  <xsl:for-each select="//s">
    <xsl:value-of select="position()"/>: <xsl:text/>
    <xsl:choose>
      <xsl:when test="following::text()[generate-id(ancestor::b[1])=
                                        generate-id(current()/ancestor::b[1])]
                                       [normalize-space()]">yes</xsl:when>
      <xsl:otherwise>no</xsl:otherwise>
    </xsl:choose>
    <xsl:text>
</xsl:text>
  </xsl:for-each>
</xsl:template>

</xsl:stylesheet>
t:\ftemp>xslt sascha.xml sasha.xsl con
1: yes
2: no

t:\ftemp>

--
Comprehensive in-depth XSLT2/XSL-FO1.1 classes: Austin TX,Jan-2008
World-wide corporate, govt. & user group XML, XSL and UBL training
RSS feeds:     publicly-available developer resources and training
G. Ken Holman                 mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0    +1(613)489-0999 (F:-0995)
Male Cancer Awareness Jul'07  http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal


Current Thread
Keywords