Как искать по значению атрибута - PullRequest
10 голосов
/ 01 августа 2011

У меня есть следующий документ 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 ]

19 голосов
/ 01 августа 2011

Я никогда не использую синтаксис xpath, потому что он никогда не был для меня интуитивно понятным, но при использовании интерфейса селектора css это должно работать:

elems = doc.search 'elements[type="foo:elementType1"]'
5 голосов
/ 01 августа 2011

РЕДАКТИРОВАТЬ: Используя урезанную (SO символьные ограничения) копию XML, предоставленную OP, это, кажется, работает правильно, используя 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
...