ruby - Cannot analyze XML with Nokogiri -


i have xml file i’m trying analyze nokogiri:

    <?xml version="1.0" encoding="iso-8859-15"?> <ehd:ehd ehd_version="1.40" xmlns:ehd="urn:ehd/001" xmlns="urn:ehd/icd/001">   <ehd:header>     <ehd:document_type_cd v="icd" dn="icd-stammdatei" s="1.2.276.0.76.5.100"/>     <ehd:service_tmr v="2013-07-01..2013-12-31"/>   </ehd:header>   <ehd:body>     <icd_stammdaten>       <kapitel_liste>         <kapitel>           <nummer v="1"/>           ....... 

normally node doing:

doc = nokogiri::xml(params[:file]) puts doc.css('nummer') 

now tried:

doc = nokogiri::xml(params[:file]) puts doc.css('ehd:document_type_cd') 

to output:

<ehd:document_type_cd v="icd" dn="icd-stammdatei" s="1.2.276.0.76.5.100"/> 

but somehow no output! how can be?

use xpath when dealing xml.

when there namesapce in xml,then below trick using nokogiri::xml::document#remove_namespaces! make life easy :

require 'nokogiri'  doc = nokogiri::xml::document.parse <<-eot  <?xml version="1.0" encoding="iso-8859-15"?> <ehd:ehd ehd_version="1.40" xmlns:ehd="urn:ehd/001" xmlns="urn:ehd/icd/001">   <ehd:header>     <ehd:document_type_cd v="icd" dn="icd-stammdatei" s="1.2.276.0.76.5.100"/>     <ehd:service_tmr v="2013-07-01..2013-12-31"/>   </ehd:header>   eot  doc.remove_namespaces! puts doc.at_xpath('//document_type_cd') # >> <document_type_cd v="icd" dn="icd-stammdatei" s="1.2.276.0.76.5.100"/> 

or,if enough namespaced xml,then below :

require 'nokogiri'  doc = nokogiri::xml::document.parse <<-eot  <?xml version="1.0" encoding="iso-8859-15"?> <ehd:ehd ehd_version="1.40" xmlns:ehd="urn:ehd/001" xmlns="urn:ehd/icd/001">   <ehd:header>     <ehd:document_type_cd v="icd" dn="icd-stammdatei" s="1.2.276.0.76.5.100"/>     <ehd:service_tmr v="2013-07-01..2013-12-31"/>   </ehd:header>   eot  puts doc.at_xpath('//ehd:document_type_cd','document_type_cd') # >> <ehd:document_type_cd v="icd" dn="icd-stammdatei" s="1.2.276.0.76.5.100"/> 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -

php - Accessing static methods using newly created $obj or using class Name -