parames3010
parames3010

Reputation: 11

(Xpath) How do I get a specific numeric value?

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

Upvotes: 0

Views: 229

Answers (2)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243449

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

Upvotes: 0

zx485
zx485

Reputation: 29022

You can try this XPath-1.0 expression:

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

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(),'.*&#x0A;\s*&quot;.*(\d+)\s*&quot;.*','$1'))

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

  1. .*&#x0A; matches the whole "::before" line
  2. \s*&quot;.* matches the string " ตอนที่
  3. (\d+) selects one or more numbers later copied to the output with $1
  4. \s*&quot;.* matches the rest of the line " = 50

Upvotes: 1

Related Questions