2013-06-14 1 views
0

다음 XML을 구문 분석하여 Nokogiri XML 구문 분석기를 사용하여 행운을 내지 않고 //ns2:Point/ns2:pos에서 Lat Long 조합을 추출하려고합니다. 그러나 잘 사용Nokogiri를 사용하여 XML 요소의 값을 추출 할 수 없습니다

doc = Nokogiri::XML(response.body); 
pos = doc.xpath('//ns2:Point/ns2:pos'); 

나는 지오 코딩 된 주소 요소에 액세스 할 수 있습니다 : 나는 다음과 같은 시도 할 때

<?xml version="1.0" encoding="UTF-8"?> 
<ns1:XLS ns1:lang="en" rel="5.2.sp03" version="1.0" xmlns:ns1="http://www.opengis.net/xls"> 
    <ns1:ResponseHeader sessionID="wrx-rails1370997540"/> 
    <ns1:Response numberOfResponses="1" requestID="10" version="1.0"> 
     <ns1:GeocodeResponse> 
      <ns1:GeocodeResponseList numberOfGeocodedAddresses="1"> 
       <ns1:GeocodedAddress> 
        <ns2:Point xmlns:ns2="http://www.opengis.net/gml"> 
         <ns2:pos>38.898331 -77.117273</ns2:pos> 
        </ns2:Point> 
        <ns1:Address countryCode="US"> 
         <ns1:StreetAddress> 
          <ns1:Building number="4400"/> 
          <ns1:Street>Lee Hwy</ns1:Street> 
         </ns1:StreetAddress> 
         <ns1:Place type="CountrySubdivision">VA</ns1:Place> 
         <ns1:Place type="CountrySecondarySubdivision">Arlington</ns1:Place> 
         <ns1:Place type="MunicipalitySubdivision">Arlington</ns1:Place> 
         <ns1:PostalCode>22207</ns1:PostalCode> 
        </ns1:Address> 
        <ns1:GeocodeMatchCode accuracy="1.0" matchType="ADDRESS POINT LOOKUP"/> 
        <ns1:SpatialKeys> 
         <ns1:SpatialKey priority="0" val="1663355010"/> 
         <ns1:SpatialKey priority="1" val="2563322400"/> 
         <ns1:SpatialKey priority="2" val="3325185160"/> 
         <ns1:SpatialKey priority="3" val="3784086306"/> 
         <ns1:SpatialKey priority="4" val="4033029320"/> 
         <ns1:SpatialKey priority="5" val="4162373938"/> 
         <ns1:SpatialKey priority="6" val="4228264524"/> 
         <ns1:SpatialKey priority="7" val="4261514387"/> 
         <ns1:SpatialKey priority="8" val="4278215460"/> 
         <ns1:SpatialKey priority="9" val="4286585033"/> 
         <ns1:SpatialKey priority="10" val="4290774578"/> 
         <ns1:SpatialKey priority="11" val="4292870540"/> 
         <ns1:SpatialKey priority="12" val="4293918819"/> 
         <ns1:SpatialKey priority="13" val="4294443032"/> 
         <ns1:SpatialKey priority="14" val="4294705158"/> 
         <ns1:SpatialKey priority="15" val="4294836224"/> 
        </ns1:SpatialKeys> 
       </ns1:GeocodedAddress> 
      </ns1:GeocodeResponseList> 
     </ns1:GeocodeResponse> 
    </ns1:Response> 
</ns1:XLS> 

는 내가 다시 빈 상태 (empty)의 배열이에 관해서는

doc.xpath('//ns1:GeocodeResponseList/ns1:GeocodedAddress') 

모든 단서를 내가 여기에 실종 됐어. 어떤 이유로 싫어하는 네임 스페이스가 바뀌 었습니까? 다음과 같이

내 환경은 다음과 같습니다 노코 기리 1.5.9 자바 레일 3.2.11 JRuby를 1.7.4 윈도우 7 상자를

답변

0

노코 기리가 하나 기대되는 XML 네임 스페이스를 찾을 수 있기 때문에 당신은 첫 번째 표현식을 찾을 수 있습니다 . ns2 네임 스페이스는 우리가 일반적으로 찾을 수있는 곳이 아니기 때문에 Nokogiri는 무엇을해야할지 모릅니다.

이 문제를 처리 할 수있는 방법은 여러 가지가 있습니다. 첫 번째는 문서에서 네임 스페이스를 수집하고 검색 할 때 Nokogiri로 전달하는 것입니다. 노코 기리가 XML의 루트 네임 스페이스에 대해이 작업을 자동으로 수행하지만,이 문서 전체에서 뿌려하고하지 않을 경우, 우리는 모든 곳에서 검색하도록 지시해야 다음에 전달할 :

namespaces = doc.collect_namespaces 
namespaces # => {"xmlns:ns1"=>"http://www.opengis.net/xls", "xmlns:ns2"=>"http://www.opengis.net/gml"} 
pos = doc.xpath('//ns2:Point/ns2:pos', namespaces); 
pos # => [#<Nokogiri::XML::Element:0x3fe8c608ab30 name="pos" namespace=#<Nokogiri::XML::Namespace:0x3fe8c608aacc prefix="ns2" href="http://www.opengis.net/gml"> children=[#<Nokogiri::XML::Text:0x3fe8c608e1b8 "38.898331 -77.117273">]>] 

대체는 노코 기리을 알리는 것입니다 문서에서 모든 네임 스페이스를 제거합니다. 당신은하고 싶은 당신은 어떤 문서의 다양한 네임 스페이스에있는 태그 이름 사이의 충돌이 없는지 확인 인 경우 :

:이 remove_namespaces!의 사용에 대해 할 말이있다

doc.remove_namespaces! 
pos = doc.xpath('//Point/pos', namespaces); 
pos # => [#<Nokogiri::XML::Element:0x3fe8c608ab30 name="pos" children=[#<Nokogiri::XML::Text:0x3fe8c608e1b8 "38.898331 -77.117273">]>] 

Nokogiri documentation

하지만 저는 게으르며 네임 스페이스를 다루고 싶지 않습니다!

게으른 == 효율적이므로 판단 할 필요가 없습니다. :)

네임 스페이스가있는 XML 문서가 있지만이를 완전히 무시하고 (예 : 팀 브레이가 작성한 적이없는 것처럼 쿼리하는 경우) XML :: 문서에서 remove_namespaces를 호출하여 모든 네임 스페이스를 제거 할 수 있습니다 . 물론 문서에 같은 이름이지만 다른 네임 스페이스가있는 노드가 있으면 이제는 모호합니다. 하지만 당신은 게으르다! 당신은 상관 없어!

관련 문제