0

How do I get a specific numeric value?

example image

how can I get only the digit "6" ?

example "ตอนที่ 6" i need only output for "6" or " 6 "

i'm try //li[@class='active']/text() for simple get text, but i not sure how should i do next

Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431

2 Answers2

1

You can try this XPath-1.0 expression:

normalize-space(substring-before(substring-after(//li[@class='active']/text(),'ตอนที่ '),'"'))

You can also use XPath-2.0's fn:replace function to use a RegEx to filter the string:

normalize-space(replace(//li[@class='active']/text(),'.*
\s*".*(\d+)\s*".*','$1'))

This RegEx may have to be adjusted. It consists of several parts:

  1. .*
 matches the whole "::before" line
  2. \s*".* matches the string " ตอนที่
  3. (\d+) selects one or more numbers later copied to the output with $1
  4. \s*".* matches the rest of the line " = 50
zx485
  • 28,498
  • 28
  • 50
  • 59
0

The way to do this, without even knowing what non-digit characters could be there, is using the double-translate method:

    translate(//li[@class='active']/text(),
              translate(//li[@class='active']/text(), '0123456789', ''),
              '')

Explanation:

  1. The inner translate(//li[@class='active']/text(), '0123456789', '') gets all non-digit characters in the first argument.

  2. The outer translate() replaces all those non-digit characters, found in the inner translate() with the empty string.

  3. What remains are only the digit characters


XSLT - based verification:

Given this simplified XML document:

<t>ตอนที่ 6</t>

The following transformation evaluates the correct (solution) XPath expression and outputs the result of this evaluation:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

  <xsl:template match="/*">
    <xsl:value-of select="translate(., translate(., '0123456789', ''), '')"/>
  </xsl:template>
</xsl:stylesheet>

The wanted result is produced:

6
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431