Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 通过类属性的部分匹配获取所有元素_Ruby_Xpath_Nokogiri - Fatal编程技术网

Ruby 通过类属性的部分匹配获取所有元素

Ruby 通过类属性的部分匹配获取所有元素,ruby,xpath,nokogiri,Ruby,Xpath,Nokogiri,我正在尝试使用Nokogiri来显示URL的结果。(基本上是抓取一个URL) 我有一些类似于以下内容的HTML: Matty Matthew 苏西 所以我需要找到所有以单词“matt”开头的元素。我需要做的是保存元素的值和元素名,以便下次引用它。。所以我需要捕捉 “Matty”和“” “马修”和“” 我还没有弄清楚如何捕获元素HTML,但是到目前为止,我已经为元素做了一些工作(它不工作!) doc=Nokogiri::HTML(打开(url)) tmp=“” doc.xpath(“[clas

我正在尝试使用Nokogiri来显示URL的结果。(基本上是抓取一个URL)

我有一些类似于以下内容的HTML:

Matty

Matthew

苏西

所以我需要找到所有以单词“matt”开头的元素。我需要做的是保存元素的值和元素名,以便下次引用它。。所以我需要捕捉

“Matty”和“

” “马修”和“

我还没有弄清楚如何捕获元素HTML,但是到目前为止,我已经为元素做了一些工作(它不工作!)

doc=Nokogiri::HTML(打开(url))
tmp=“”
doc.xpath(“[class*=matt”)。每个do |项|
tmp+=item.text
结束
@testy2=tmp

这应该让您开始:

doc.xpath('//p[starts-with(@class, "matt")]').each do |el|
  p [el.attributes['class'].value, el.children[0].text]
end
["mattFacer", "Matty"]
["mattSmith", "Matthew"]
使用

/*/p[starts-with(@class, 'matt')] | /*/p[starts-with(@class, 'matt')]/text() 
<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:for-each select=
  "/*/p[starts-with(@class, 'matt')]
  |
   /*/p[starts-with(@class, 'matt')]/text()
  ">
  <xsl:copy-of select="."/>
  <xsl:text>&#xA;</xsl:text>
  </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>
<p class="mattFacer">Matty</p>
Matty
<p class="mattSmith">Matthew</p>
Matthew
这将选择作为XML文档顶部元素的子元素的任何
p
元素,其
class
属性的值以
“matt”
开头,以及任何此类
p
元素的任何文本节点子元素

根据此XML文档进行评估时(未提供!):

<html>
    <p class="mattFacer">Matty</p>
    <p class="mattSmith">Matthew</p>
    <p class="suzieSmith">Suzie</p>
</html>
<p class="mattFacer">Matty</p>
Matty
<p class="mattSmith">Matthew</p>
Matthew

Matty

Matthew

苏西

选择以下节点(每个节点位于单独的行上),并可通过位置访问:

<html>
    <p class="mattFacer">Matty</p>
    <p class="mattSmith">Matthew</p>
    <p class="suzieSmith">Suzie</p>
</html>
<p class="mattFacer">Matty</p>
Matty
<p class="mattSmith">Matthew</p>
Matthew

Matty

马蒂

Matthew

马修
这里是一个快速的XSLT验证

/*/p[starts-with(@class, 'matt')] | /*/p[starts-with(@class, 'matt')]/text() 
<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:for-each select=
  "/*/p[starts-with(@class, 'matt')]
  |
   /*/p[starts-with(@class, 'matt')]/text()
  ">
  <xsl:copy-of select="."/>
  <xsl:text>&#xA;</xsl:text>
  </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>
<p class="mattFacer">Matty</p>
Matty
<p class="mattSmith">Matthew</p>
Matthew
当应用于同一XML文档(如上)时,此转换的结果是所选节点的预期正确顺序

/*/p[starts-with(@class, 'matt')] | /*/p[starts-with(@class, 'matt')]/text() 
<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:for-each select=
  "/*/p[starts-with(@class, 'matt')]
  |
   /*/p[starts-with(@class, 'matt')]/text()
  ">
  <xsl:copy-of select="."/>
  <xsl:text>&#xA;</xsl:text>
  </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>
<p class="mattFacer">Matty</p>
Matty
<p class="mattSmith">Matthew</p>
Matthew

Matty

马蒂

Matthew

马修
公认的答案很好,但另一种方法是使用,它允许您通过正则表达式进行匹配(无需熟悉XPATH函数):

doc.attr_匹配('class',/^matt/)。收集do |项|
[item.attributes['class'].value,item.text]
结束

yep-这似乎很有用!!谢谢!有没有方法搜索类名的任何部分?如果是

我该如何搜索?基本上它可以是任何内容,但我想抓住matt部分!谢谢你的帮助:)XPath有许多函数,比如

开头,例如
包含
substring
。好问题,+1。有关完整、简短且简单的一行XPath表达式解决方案,请参见我的答案。:)打字
css(“p[class*=matt]”