开发者

How to search by attribute value

I have the following XML doc:

<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>

I need to grab only the elements which have type = "foo:elementType1". I tried this, but I'm getting empty results.

elems = doc.search("//elements[@type='foo:elemen开发者_高级运维tType1']")

What am I missing?


I never use the xpath syntax because it's never been intuitive for me but using the css selector interface this should work:

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


EDIT: Using a trimmed down (SO character constraints) copy of the XML provided by OP, this appears to work properly using 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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜