0

I have questions about libxml-ruby.
There is a xml file "sample.xml".

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<worksheet xmlns="http://***" xmlns:r="http://???">
    <sheetData>
        <row><v>1</v></row>
    </sheetData>
</worksheet>

I want to deal with nodes without specifying default namespace like below.

xml = XML::Document.file('sample.xml')
sheet_data = xml.find_first('sheetData')

Of course, I can do it like below.

NS = {
  main: 'http://***',
  r: 'http://???',
}
sheet_data = xml.find_first('main:sheetData', NS)

But I want to omit string of default namespace.
I tried some properties and methods belongs to XML::Namespace[s], but not effected.

And one more problem when I save a xml file.

ns = XML::Namespace.new(xml.root, 'main', 'http://***')
row = XML::Node.new('row', nil, ns)
sheet_data << row
xml.save("sample.xml")

Published like below.

<row><v>1</v></row>
<main:row/>

I want that it's omitted string of "main:". So I do this, but it's really ugly.

open('sample.xml', 'wb') do |f|
  f.write(xml.to_s.gsub(/(<\/?)main:/, '\1'))
end

Do you have any good idea?

takanopontaro
  • 217
  • 1
  • 2
  • 12
  • Could you tell me the final output,you are looking for? And if possible go with `Nokogiri`,it will help you out.. – Arup Rakshit Nov 03 '13 at 10:44
  • Hi, Arup. My desired result is that no string of default namespace in published file. I hope "" but not "". I knew about Nokogori but don't want to use it very much. – takanopontaro Nov 03 '13 at 12:58

0 Answers0