Parse text email addresses using XPath, NOT //A[startswith(@href, 'mailto:')]

nokogiri, ruby, xpath

Solution

I would like to select a path to element that contains @ inside

Use:

//*[contains(., '@')]

It seems to me that what you actually wanted is to select elements that have a text-node child that contains "@". If this is so, use:

//*[contains(text(), '@')]

XSLT - based verification:

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

 <xsl:template match="/">
     <xsl:copy-of select=
        "//*[contains(text(), '@')] "/>
 </xsl:template>
</xsl:stylesheet>

when this transformation is applied on the following XML document:

<html>
 <body>
  <a href="xxx.com">xxx.com</a>
  <span>someone@xxx.com</span>
 </body>
</html>

the XPath expression is evaluated and the selected nodes are copied to the output:

<span>someone@xxx.com</span>

Problem

I want to extract email addresses from few different websites. If they are in active link format, I can do this using ``` //A[starts-with(@href, 'mailto:')] ``` But some of them are in just text format `example@domain.com`, not a link, so I would like to select a path to element that contains `@` inside

Original source

Related problems