CodeManX
CodeManX

Reputation: 11915

Get text but exclude node if it has a certain child in a for-each loop in XSLT?

I'm trying to get the following text

Divided into:
Bonaire, Sint Eustatius and Saba (BQ, BES, 535)
Curaçao (CW, CUW, 531)
Sint Maarten (Dutch part) (SX, SXM, 534)

out of this source (excerpt):

<td>
  Divided into:<br />
  <a href="/wiki/Caribbean_Netherlands" title="Caribbean Netherlands">Bonaire, Sint Eustatius and Saba</a> (<tt>BQ</tt>, <tt>BES</tt>, <tt>535</tt>) <sup id="cite_ref-7" class="reference">
    <a href="#cite_note-7">
      <span>[</span>note 4<span>]</span>
    </a>
  </sup><br />
  <a href="/wiki/Cura%C3%A7ao" title="Curaçao">Curaçao</a> (<tt>CW</tt>, <tt>CUW</tt>, <tt>531</tt>)<br />
  <a href="/wiki/Sint_Maarten" title="Sint Maarten">Sint Maarten (Dutch part)</a> (<tt>SX</tt>, <tt>SXM</tt>, <tt>534</tt>)
</td>

This is easily done with <xsl:value select="td[4]"/> (it's the 4th td element, and I'm looping over the surrounded trs).

But I want to exclude the text [note 4], so every a that has span children.

I tried td[4]/node()[not(descendant::span)], but it only left Divided into:. td[4][not(//span)] gives always empty strings.

Upvotes: 0

Views: 185

Answers (1)

deanosaur
deanosaur

Reputation: 611

when you match td[4]/node()[not(descendant::span)] you're matching the forth td that doesn't have a span descendant. Since your td[4] does have a span descendant,you're getting empty results.

What you need is is a template to match the td[4] descendant nodes that does text output:

<xsl:template match="td[4]/node()"> ... <xsl:template>  <!-- match descendant nodes of td[4] -->

and another template to specifically catch the span node:

    <xsl:template match="span | text()[preceding-sibling::span] | text()[following-sibling::span]"/>

Upvotes: 1

Related Questions