2011-08-01 150 views
9

搜索我有以下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']") 

我在想什麼?

+0

可能重複[?如何找到使用引入nokogiri通過名稱的元素(http://stackoverflow.com/questions/2565327 /怎麼找-n-element-by-name-using-nokogiri) – outis

回答

5

編輯: 使用下調(SO字符的限制)由OP提供的XML的副本,這似乎利用引入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

嗯應該可以工作,但它不適用於我的實際XML,也許這是一個命名空間問題? [這裏](http://jsfiddle.net/Gt7xR/)是XML。我想要的是所有'type = ns5:FieldInfoSingle'的元素。 – CodeWombat

+0

如果遇到名稱空間問題,'#collect_namespaces'方法可以快速識別名稱空間。如果您匆忙或不關心名稱空間,則可以在解析的文檔上使用'#remove_namespaces!'方法。 –

+0

原來我的XML在轉到Nokogiri之前正在進行轉換,謝謝! – CodeWombat

17

我從來沒有使用XPath語法因爲它從來沒有直觀的對我,而且使用CSS選擇器接口這應該工作:

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