如何按属性值搜索

时间:2011-08-01 19:16:34

标签: ruby-on-rails xml xpath namespaces nokogiri

我有以下XML文档:

<files>
  <elements xsi:type="foo:elementType1">
    <name>foo1</name>
  </elements>

  <elements xsi:type="foo:elementType1">
    <name>foo2</name>
    <other>
      <elements>
        <data1>data1</data1>
        <data2>data2</data2>
      </elements>
    </other>
  </elements>

  <elements>
    <name>foo3</name>
    <affiliates> 
      <elements xsi:type="foo:elementType1">
        <name>foo4</name>
      </elements>
    </affiliates>
  </elements>

</files>

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

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

我错过了什么?

2 个答案:

答案 0 :(得分:19)

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

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

答案 1 :(得分:5)

修改 使用OP提供的XML的精简(SO字符约束)副本,使用Nokogiri v1.5.0可以正常工作:

require 'nokogiri'
xml = <<XML
<files>
    <elements> 
        <loginForm xsi:type="ns5:Form" xmlns:ns5="http://common.soap.yodlee.com">
            <conjunctionOp>AND</conjunctionOp>
            <componentList>
                <elements xsi:type="ns5:FieldInfoSingle">
                    <name>OP_LOGIN2</name>
                    <displayName>Answer 1</displayName>
                    <isEditable>true</isEditable>
                    <isOptional>true</isOptional>
                    <isEscaped>false</isEscaped>
                    <isOptionalMFA>false</isOptionalMFA>
                    <isMFA>false</isMFA>
                    <valueIdentifier>OP_LOGIN2</valueIdentifier>
                    <valueMask>LOGIN_FIELD</valueMask>
                    <fieldType>TEXT</fieldType>
                    <size>20</size>
                    <maxlength>40</maxlength>
                    <fieldErrorCode xsi:nil="1"/>
                </elements>
                <elements xsi:type="ns5:FieldInfoSingle">
                    <name>LOGIN</name>
                    <displayName>Account #</displayName>
                    <isEditable>true</isEditable>
                    <isOptional>false</isOptional>
                    <isEscaped>false</isEscaped>
                    <isOptionalMFA>false</isOptionalMFA>
                    <isMFA>false</isMFA>
                    <valueIdentifier>LOGIN</valueIdentifier>
                    <valueMask>LOGIN_FIELD</valueMask>
                    <fieldType>TEXT</fieldType>
                    <size>20</size>
                    <maxlength>40</maxlength>
                    <fieldErrorCode xsi:nil="1"/>
                </elements>
                <elements xsi:type="ns5:FieldInfoSingle">
                    <name>PASSWORD</name>
                    <displayName>PIN</displayName>
                    <isEditable>true</isEditable>
                    <isOptional>false</isOptional>
                    <isEscaped>false</isEscaped>
                    <isOptionalMFA>false</isOptionalMFA>
                    <isMFA>false</isMFA>
                    <valueIdentifier>PASSWORD</valueIdentifier>
                    <valueMask>LOGIN_FIELD</valueMask>
                    <fieldType>PASSWORD</fieldType>
                    <size>20</size>
                    <maxlength>40</maxlength>
                    <fieldErrorCode xsi:nil="1"/>
                </elements>
                <elements xsi:type="ns5:FieldInfoChoice">
                    <fieldInfoList>
                        <elements xsi:type="ns5:FieldInfoSingle">
                            <name>OP_OPTIONS1</name>
                            <displayName>Question 1</displayName>
                            <isEditable>true</isEditable>
                            <isOptional>true</isOptional>
                            <isEscaped>false</isEscaped>
                            <isOptionalMFA>false</isOptionalMFA>
                            <isMFA>false</isMFA>
                            <validValues>
                                <elements>first three digits of your Social Security</elements>
                                <elements>last four digits of your Social Security</elements>
                                <elements>your favorite food</elements>
                                <elements>your favorite sport</elements>
                                <elements>your mother's maiden name</elements>
                                <elements>your pet's name</elements>
                                <elements>What was your first car</elements>
                                <elements>your school mascot</elements>
                                <elements>When did you graduate from school</elements>
                                <elements>Where did you grow up</elements>
                                <elements>Where do you work</elements>
                                <elements>Where were you born</elements>
                                <elements>Who is your hero</elements>
                            </validValues>
                            <displayValidValues>
                                <elements>What are the first three digits of your Social Security?</elements>
                                <elements>What are the last four digits of your Social Security?</elements>
                                <elements>What is your favorite food?</elements>
                                <elements>What is your favorite sport?</elements>
                                <elements>What is your mother's maiden name?</elements>
                                <elements>What is your pet's name?</elements>
                                <elements>What was your first car?</elements>
                                <elements>What was your school mascot?</elements>
                                <elements>When did you graduate from school?</elements>
                                <elements>Where did you grow up?</elements>
                                <elements>Where do you work?</elements>
                                <elements>Where were you born?</elements>
                                <elements>Who is your hero?</elements>
                            </displayValidValues>
                            <valueIdentifier>OP_OPTIONS1</valueIdentifier>
                            <valueMask>LOGIN_FIELD</valueMask>
                            <fieldType>OPTIONS</fieldType>
                            <size>20</size>
                            <maxlength>40</maxlength>
                            <fieldErrorCode xsi:nil="1"/>
                        </elements>
                        <elements xsi:type="ns5:FieldInfoSingle">
                            <name>OP_LOGIN1</name>
                            <displayName>Custom Question 1</displayName>
                            <isEditable>true</isEditable>
                            <isOptional>true</isOptional>
                            <isEscaped>false</isEscaped>
                            <isOptionalMFA>false</isOptionalMFA>
                            <isMFA>false</isMFA>
                            <valueIdentifier>OP_LOGIN1</valueIdentifier>
                            <valueMask>LOGIN_FIELD</valueMask>
                            <fieldType>TEXT</fieldType>
                            <size>20</size>
                            <maxlength>40</maxlength>
                            <fieldErrorCode xsi:nil="1"/>
                        </elements>
                    </fieldInfoList>
                </elements>
                <elements xsi:type="ns5:FieldInfoChoice">
                    <fieldInfoList>
                        <elements xsi:type="ns5:FieldInfoSingle">
                            <name>OP_OPTIONS2</name>
                            <displayName>Question 2</displayName>
                            <isEditable>true</isEditable>
                            <isOptional>true</isOptional>
                            <isEscaped>false</isEscaped>
                            <isOptionalMFA>false</isOptionalMFA>
                            <isMFA>false</isMFA>
                            <validValues>
                                <elements>first three digits of your Social Security</elements>
                                <elements>last four digits of your Social Security</elements>
                                <elements>your favorite food</elements>
                                <elements>your favorite sport</elements>
                                <elements>your mother's maiden name</elements>
                                <elements>your pet's name</elements>
                                <elements>What was your first car</elements>
                                <elements>your school mascot</elements>
                                <elements>When did you graduate from school</elements>
                                <elements>Where did you grow up</elements>
                                <elements>Where do you work</elements>
                                <elements>Where were you born</elements>
                                <elements>Who is your hero</elements>
                            </validValues>
                            <displayValidValues>
                                <elements>What are the first three digits of your Social Security?</elements>
                                <elements>What are the last four digits of your Social Security?</elements>
                                <elements>What is your favorite food?</elements>
                                <elements>What is your favorite sport?</elements>
                                <elements>What is your mother's maiden name?</elements>
                                <elements>What is your pet's name?</elements>
                                <elements>What was your first car?</elements>
                                <elements>What was your school mascot?</elements>
                                <elements>When did you graduate from school?</elements>
                                <elements>Where did you grow up?</elements>
                                <elements>Where do you work?</elements>
                                <elements>Where were you born?</elements>
                                <elements>Who is your hero?</elements>
                            </displayValidValues>
                            <valueIdentifier>OP_OPTIONS2</valueIdentifier>
                            <valueMask>LOGIN_FIELD</valueMask>
                            <fieldType>OPTIONS</fieldType>
                            <size>20</size>
                            <maxlength>40</maxlength>
                            <fieldErrorCode xsi:nil="1"/>
                        </elements>
                        <elements xsi:type="ns5:FieldInfoSingle">
                            <name>OP_LOGIN3</name>
                            <displayName>Custom Question 2</displayName>
                            <isEditable>true</isEditable>
                            <isOptional>true</isOptional>
                            <isEscaped>false</isEscaped>
                            <isOptionalMFA>false</isOptionalMFA>
                            <isMFA>false</isMFA>
                            <valueIdentifier>OP_LOGIN3</valueIdentifier>
                            <valueMask>LOGIN_FIELD</valueMask>
                            <fieldType>TEXT</fieldType>
                            <size>20</size>
                            <maxlength>40</maxlength>
                            <fieldErrorCode xsi:nil="1"/>
                        </elements>
                    </fieldInfoList>
                </elements>
                <elements xsi:type="ns5:FieldInfoSingle">
                    <name>OP_LOGIN4</name>
                    <displayName>Answer 2</displayName>
                    <isEditable>true</isEditable>
                    <isOptional>true</isOptional>
                    <isEscaped>false</isEscaped>
                    <isOptionalMFA>false</isOptionalMFA>
                    <isMFA>false</isMFA>
                    <valueIdentifier>OP_LOGIN4</valueIdentifier>
                    <valueMask>LOGIN_FIELD</valueMask>
                    <fieldType>TEXT</fieldType>
                    <size>20</size>
                    <maxlength>40</maxlength>
                    <fieldErrorCode xsi:nil="1"/>
                </elements>
                <elements xsi:type="ns5:FieldInfoChoice">
                    <fieldInfoList>
                        <elements xsi:type="ns5:FieldInfoSingle">
                            <name>OP_OPTIONS3</name>
                            <displayName>Question 3</displayName>
                            <isEditable>true</isEditable>
                            <isOptional>true</isOptional>
                            <isEscaped>false</isEscaped>
                            <isOptionalMFA>false</isOptionalMFA>
                            <isMFA>false</isMFA>
                            <validValues>
                                <elements>first three digits of your Social Security</elements>
                                <elements>last four digits of your Social Security</elements>
                                <elements>your favorite food</elements>
                                <elements>your favorite sport</elements>
                                <elements>your mother's maiden name</elements>
                                <elements>your pet's name</elements>
                                <elements>What was your first car</elements>
                                <elements>your school mascot</elements>
                                <elements>When did you graduate from school</elements>
                                <elements>Where did you grow up</elements>
                                <elements>Where do you work</elements>
                                <elements>Where were you born</elements>
                                <elements>Who is your hero</elements>
                            </validValues>
                            <displayValidValues>
                                <elements>What are the first three digits of your Social Security?</elements>
                                <elements>What are the last four digits of your Social Security?</elements>
                                <elements>What is your favorite food?</elements>
                                <elements>What is your favorite sport?</elements>
                                <elements>What is your mother's maiden name?</elements>
                                <elements>What is your pet's name?</elements>
                                <elements>What was your first car?</elements>
                                <elements>What was your school mascot?</elements>
                                <elements>When did you graduate from school?</elements>
                                <elements>Where did you grow up?</elements>
                                <elements>Where do you work?</elements>
                                <elements>Where were you born?</elements>
                                <elements>Who is your hero?</elements>
                            </displayValidValues>
                            <valueIdentifier>OP_OPTIONS3</valueIdentifier>
                            <valueMask>LOGIN_FIELD</valueMask>
                            <fieldType>OPTIONS</fieldType>
                            <size>20</size>
                            <maxlength>40</maxlength>
                            <fieldErrorCode xsi:nil="1"/>
                        </elements>
                        <elements xsi:type="ns5:FieldInfoSingle">
                            <name>OP_LOGIN5</name>
                            <displayName>Custom Question 3</displayName>
                            <isEditable>true</isEditable>
                            <isOptional>true</isOptional>
                            <isEscaped>false</isEscaped>
                            <isOptionalMFA>false</isOptionalMFA>
                            <isMFA>false</isMFA>
                            <valueIdentifier>OP_LOGIN5</valueIdentifier>
                            <valueMask>LOGIN_FIELD</valueMask>
                            <fieldType>TEXT</fieldType>
                            <size>20</size>
                            <maxlength>40</maxlength>
                            <fieldErrorCode xsi:nil="1"/>
                        </elements>
                    </fieldInfoList>
                </elements>
                <elements xsi:type="ns5:FieldInfoSingle">
                    <name>OP_LOGIN6</name>
                    <displayName>Answer 3</displayName>
                    <isEditable>true</isEditable>
                    <isOptional>true</isOptional>
                    <isEscaped>false</isEscaped>
                    <isOptionalMFA>false</isOptionalMFA>
                    <isMFA>false</isMFA>
                    <valueIdentifier>OP_LOGIN6</valueIdentifier>
                    <valueMask>LOGIN_FIELD</valueMask>
                    <fieldType>TEXT</fieldType>
                    <size>20</size>
                    <maxlength>40</maxlength>
                    <fieldErrorCode xsi:nil="1"/>
                </elements>
                <elements xsi:type="ns5:FieldInfoChoice">
                    <fieldInfoList>
                        <elements xsi:type="ns5:FieldInfoSingle">
                            <name>OP_OPTIONS4</name>
                            <displayName>Question 4</displayName>
                            <isEditable>true</isEditable>
                            <isOptional>true</isOptional>
                            <isEscaped>false</isEscaped>
                            <isOptionalMFA>false</isOptionalMFA>
                            <isMFA>false</isMFA>
                            <validValues>
                                <elements>first three digits of your Social Security</elements>
                                <elements>last four digits of your Social Security</elements>
                                <elements>your favorite food</elements>
                                <elements>your favorite sport</elements>
                                <elements>your mother's maiden name</elements>
                                <elements>your pet's name</elements>
                                <elements>What was your first car</elements>
                                <elements>your school mascot</elements>
                                <elements>When did you graduate from school</elements>
                                <elements>Where did you grow up</elements>
                                <elements>Where do you work</elements>
                                <elements>Where were you born</elements>
                                <elements>Who is your hero</elements>
                            </validValues>
                            <displayValidValues>
                                <elements>What are the first three digits of your Social Security?</elements>
                                <elements>What are the last four digits of your Social Security?</elements>
                                <elements>What is your favorite food?</elements>
                                <elements>What is your favorite sport?</elements>
                                <elements>What is your mother's maiden name?</elements>
                                <elements>What is your pet's name?</elements>
                                <elements>What was your first car?</elements>
                                <elements>What was your school mascot?</elements>
                                <elements>When did you graduate from school?</elements>
                                <elements>Where did you grow up?</elements>
                                <elements>Where do you work?</elements>
                                <elements>Where were you born?</elements>
                                <elements>Who is your hero?</elements>
                            </displayValidValues>
                            <valueIdentifier>OP_OPTIONS4</valueIdentifier>
                            <valueMask>LOGIN_FIELD</valueMask>
                            <fieldType>OPTIONS</fieldType>
                            <size>20</size>
                            <maxlength>40</maxlength>
                            <fieldErrorCode xsi:nil="1"/>
                        </elements>
                        <elements xsi:type="ns5:FieldInfoSingle">
                            <name>OP_LOGIN7</name>
                            <displayName>Custom Question 4</displayName>
                            <isEditable>true</isEditable>
                            <isOptional>true</isOptional>
                            <isEscaped>false</isEscaped>
                            <isOptionalMFA>false</isOptionalMFA>
                            <isMFA>false</isMFA>
                            <valueIdentifier>OP_LOGIN7</valueIdentifier>
                            <valueMask>LOGIN_FIELD</valueMask>
                            <fieldType>TEXT</fieldType>
                            <size>20</size>
                            <maxlength>40</maxlength>
                            <fieldErrorCode xsi:nil="1"/>
                        </elements>
                    </fieldInfoList>
                </elements>
                <elements xsi:type="ns5:FieldInfoSingle">
                    <name>OP_LOGIN8</name>
                    <displayName>Answer 4</displayName>
                    <isEditable>true</isEditable>
                    <isOptional>true</isOptional>
                    <isEscaped>false</isEscaped>
                    <isOptionalMFA>false</isOptionalMFA>
                    <isMFA>false</isMFA>
                    <valueIdentifier>OP_LOGIN8</valueIdentifier>
                    <valueMask>LOGIN_FIELD</valueMask>
                    <fieldType>TEXT</fieldType>
                    <size>20</size>
                    <maxlength>40</maxlength>
                    <fieldErrorCode xsi:nil="1"/>
                </elements>
                <elements xsi:type="ns5:FieldInfoChoice">
                    <fieldInfoList>
                        <elements xsi:type="ns5:FieldInfoSingle">
                            <name>OP_OPTIONS5</name>
                            <displayName>Question 5</displayName>
                            <isEditable>true</isEditable>
                            <isOptional>true</isOptional>
                            <isEscaped>false</isEscaped>
                            <isOptionalMFA>false</isOptionalMFA>
                            <isMFA>false</isMFA>
                            <validValues>
                                <elements>first three digits of your Social Security</elements>
                                <elements>last four digits of your Social Security</elements>
                                <elements>your favorite food</elements>
                                <elements>your favorite sport</elements>
                                <elements>your mother's maiden name</elements>
                                <elements>your pet's name</elements>
                                <elements>What was your first car</elements>
                                <elements>your school mascot</elements>
                                <elements>When did you graduate from school</elements>
                                <elements>Where did you grow up</elements>
                                <elements>Where do you work</elements>
                                <elements>Where were you born</elements>
                                <elements>Who is your hero</elements>
                            </validValues>
                            <displayValidValues>
                                <elements>What are the first three digits of your Social Security?</elements>
                                <elements>What are the last four digits of your Social Security?</elements>
                                <elements>What is your favorite food?</elements>
                                <elements>What is your favorite sport?</elements>
                                <elements>What is your mother's maiden name?</elements>
                                <elements>What is your pet's name?</elements>
                                <elements>What was your first car?</elements>
                                <elements>What was your school mascot?</elements>
                                <elements>When did you graduate from school?</elements>
                                <elements>Where did you grow up?</elements>
                                <elements>Where do you work?</elements>
                                <elements>Where were you born?</elements>
                                <elements>Who is your hero?</elements>
                            </displayValidValues>
                            <valueIdentifier>OP_OPTIONS5</valueIdentifier>
                            <valueMask>LOGIN_FIELD</valueMask>
                            <fieldType>OPTIONS</fieldType>
                            <size>20</size>
                            <maxlength>40</maxlength>
                            <fieldErrorCode xsi:nil="1"/>
                        </elements>
                        <elements xsi:type="ns5:FieldInfoSingle">
                            <name>OP_LOGIN9</name>
                            <displayName>Custom Question 5</displayName>
                            <isEditable>true</isEditable>
                            <isOptional>true</isOptional>
                            <isEscaped>false</isEscaped>
                            <isOptionalMFA>false</isOptionalMFA>
                            <isMFA>false</isMFA>
                            <valueIdentifier>OP_LOGIN9</valueIdentifier>
                            <valueMask>LOGIN_FIELD</valueMask>
                            <fieldType>TEXT</fieldType>
                            <size>20</size>
                            <maxlength>40</maxlength>
                            <fieldErrorCode xsi:nil="1"/>
                        </elements>
                    </fieldInfoList>
                </elements>
                <elements xsi:type="ns5:FieldInfoSingle">
                    <name>OP_LOGIN10</name>
                    <displayName>Answer 5</displayName>
                    <isEditable>true</isEditable>
                    <isOptional>true</isOptional>
                    <isEscaped>false</isEscaped>
                    <isOptionalMFA>false</isOptionalMFA>
                    <isMFA>false</isMFA>
                    <valueIdentifier>OP_LOGIN10</valueIdentifier>
                    <valueMask>LOGIN_FIELD</valueMask>
                    <fieldType>TEXT</fieldType>
                    <size>20</size>
                    <maxlength>40</maxlength>
                    <fieldErrorCode xsi:nil="1"/>
                </elements>
            </componentList>
        </loginForm>
    </elements>
</files>
XML

doc = Nokogiri::XML(xml)

doc.xpath("//elements[@type='ns5:FieldInfoSingle']").each do |element|
  puts element.xpath("name").text
end