fandang
fandang

Reputation: 607

How to get xpath text from element but exclude some of it based on class?

How can I get the text "Show ME, and Show me too" using xpath expression? I need the "OMIT ME" omitted.

<table>
<tr>
<td>
<span class="omitME">OMIT ME</span>
Show ME, and
<span class="showME"> Show me too</span>
</td>
</tr>
</table>

Upvotes: 0

Views: 1083

Answers (2)

michael.hor257k
michael.hor257k

Reputation: 117073

In XPath, you could use:

/table/tr/td//text()[not(parent::span[@class='omitME'])]

to select all text nodes descendants of the td element, except those whose parent is a span of the 'omitME' class.

Or perhaps:

//text()[not(parent::span[@class='omitME'])]

to select all text nodes descendants of the root node - again, except those whose parent is an "omitted" span.

In XSLT, you would probably use something else, depending on the current context and on the purpose of the exercise.

Upvotes: 4

C. M. Sperberg-McQueen
C. M. Sperberg-McQueen

Reputation: 25034

One simple way:

<xsl:template match="td | span[@class='showME']">
  <xsl:apply-templates/>
</xsl:template>
<xsl:template match="span[@class='omitME']"/>

Upvotes: 1

Related Questions