如何按属性值搜索

如何按属性值搜索

本文介绍了如何按属性值搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下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"的元素.我试过了,但是结果却是空的.

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:elementType1']")

我想念什么?

推荐答案

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

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

这篇关于如何按属性值搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 14:36