adellam
adellam

Reputation: 831

applying the XPath function 'substring-after'

from the given html :

 <span class="flag_16 left_16 armenia_16_left"> First League</span>

how i can get the (armenia) string only or at least (armenia_16_left).

thanks in advance.

Upvotes: 2

Views: 2614

Answers (1)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243579

Use this XPath 1.0 expression:

substring-before(substring-after(substring-after(/span /@class, ' '), ' '), '_')

In XPath 2.0 one can simply use:

tokenize(tokenize(/span /@class, ' ')[last()], '_')[1]

XSLT-based verification:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

 <xsl:template match="/">
  "<xsl:value-of select=
   "substring-before(substring-after(substring-after(/span /@class, ' '), ' '), '_')
   "/>"
 </xsl:template>
</xsl:stylesheet>

when this transformation is applied on the provided XML document:

<span class="flag_16 left_16 armenia_16_left"> First League</span>

the Xpath expression is evaluated and the result is copied to the output:

"armenia"

When this XSLT 2.0 transformation is applied on the same XML document (above):

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

 <xsl:template match="/">
  "<xsl:sequence select=
   "tokenize(tokenize(/span /@class, ' ')[last()], '_')[1]"/>"
 </xsl:template>
</xsl:stylesheet>

again the same correct result is produced:

"armenia"

Upvotes: 2

Related Questions