如何按属性值搜索

我有以下XML文档:

  foo1   foo2   data1 data2     foo3   foo4     

我只需要抓取type = "foo:elementType1"的元素。 我尝试过这个,但是我得到了空洞的结果。

 elems = doc.search("//elements[@type='foo:elementType1']") 

我错过了什么?

编辑:使用OP提供的XML的精简(SO字符约束)副本,这似乎使用Nokogiri v1.5.0正常工作:

 require 'nokogiri' xml = <   AND   OP_LOGIN2 Answer 1 true true false false false OP_LOGIN2 LOGIN_FIELD TEXT 20 40    LOGIN Account # true false false false false LOGIN LOGIN_FIELD TEXT 20 40    PASSWORD PIN true false false false false PASSWORD LOGIN_FIELD PASSWORD 20 40      OP_OPTIONS1 Question 1 true true false false false  first three digits of your Social Security last four digits of your Social Security your favorite food your favorite sport your mother's maiden name your pet's name What was your first car your school mascot When did you graduate from school Where did you grow up Where do you work Where were you born Who is your hero   What are the first three digits of your Social Security? What are the last four digits of your Social Security? What is your favorite food? What is your favorite sport? What is your mother's maiden name? What is your pet's name? What was your first car? What was your school mascot? When did you graduate from school? Where did you grow up? Where do you work? Where were you born? Who is your hero?  OP_OPTIONS1 LOGIN_FIELD OPTIONS 20 40    OP_LOGIN1 Custom Question 1 true true false false false OP_LOGIN1 LOGIN_FIELD TEXT 20 40        OP_OPTIONS2 Question 2 true true false false false  first three digits of your Social Security last four digits of your Social Security your favorite food your favorite sport your mother's maiden name your pet's name What was your first car your school mascot When did you graduate from school Where did you grow up Where do you work Where were you born Who is your hero   What are the first three digits of your Social Security? What are the last four digits of your Social Security? What is your favorite food? What is your favorite sport? What is your mother's maiden name? What is your pet's name? What was your first car? What was your school mascot? When did you graduate from school? Where did you grow up? Where do you work? Where were you born? Who is your hero?  OP_OPTIONS2 LOGIN_FIELD OPTIONS 20 40    OP_LOGIN3 Custom Question 2 true true false false false OP_LOGIN3 LOGIN_FIELD TEXT 20 40      OP_LOGIN4 Answer 2 true true false false false OP_LOGIN4 LOGIN_FIELD TEXT 20 40      OP_OPTIONS3 Question 3 true true false false false  first three digits of your Social Security last four digits of your Social Security your favorite food your favorite sport your mother's maiden name your pet's name What was your first car your school mascot When did you graduate from school Where did you grow up Where do you work Where were you born Who is your hero   What are the first three digits of your Social Security? What are the last four digits of your Social Security? What is your favorite food? What is your favorite sport? What is your mother's maiden name? What is your pet's name? What was your first car? What was your school mascot? When did you graduate from school? Where did you grow up? Where do you work? Where were you born? Who is your hero?  OP_OPTIONS3 LOGIN_FIELD OPTIONS 20 40    OP_LOGIN5 Custom Question 3 true true false false false OP_LOGIN5 LOGIN_FIELD TEXT 20 40      OP_LOGIN6 Answer 3 true true false false false OP_LOGIN6 LOGIN_FIELD TEXT 20 40      OP_OPTIONS4 Question 4 true true false false false  first three digits of your Social Security last four digits of your Social Security your favorite food your favorite sport your mother's maiden name your pet's name What was your first car your school mascot When did you graduate from school Where did you grow up Where do you work Where were you born Who is your hero   What are the first three digits of your Social Security? What are the last four digits of your Social Security? What is your favorite food? What is your favorite sport? What is your mother's maiden name? What is your pet's name? What was your first car? What was your school mascot? When did you graduate from school? Where did you grow up? Where do you work? Where were you born? Who is your hero?  OP_OPTIONS4 LOGIN_FIELD OPTIONS 20 40    OP_LOGIN7 Custom Question 4 true true false false false OP_LOGIN7 LOGIN_FIELD TEXT 20 40      OP_LOGIN8 Answer 4 true true false false false OP_LOGIN8 LOGIN_FIELD TEXT 20 40      OP_OPTIONS5 Question 5 true true false false false  first three digits of your Social Security last four digits of your Social Security your favorite food your favorite sport your mother's maiden name your pet's name What was your first car your school mascot When did you graduate from school Where did you grow up Where do you work Where were you born Who is your hero   What are the first three digits of your Social Security? What are the last four digits of your Social Security? What is your favorite food? What is your favorite sport? What is your mother's maiden name? What is your pet's name? What was your first car? What was your school mascot? When did you graduate from school? Where did you grow up? Where do you work? Where were you born? Who is your hero?  OP_OPTIONS5 LOGIN_FIELD OPTIONS 20 40    OP_LOGIN9 Custom Question 5 true true false false false OP_LOGIN9 LOGIN_FIELD TEXT 20 40      OP_LOGIN10 Answer 5 true true false false false OP_LOGIN10 LOGIN_FIELD TEXT 20 40       XML doc = Nokogiri::XML(xml) doc.xpath("//elements[@type='ns5:FieldInfoSingle']").each do |element| puts element.xpath("name").text end 

我从不使用xpath语法,因为它对我来说从来都不直观,但使用css选择器接口应该可以工作:

 elems = doc.search 'elements[type="foo:elementType1"]'