Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Nokogiri adds 'default' Namespace to elements #391

Closed
nickcanz opened this Issue Dec 29, 2010 · 9 comments

Comments

Projects
None yet
6 participants

I have a situation where I'm using nokogiri to do the following:

  1. Extract some nodes from a generated xml file
  2. Put extracted nodes into a simple template file
  3. Save the template file out as a new file

My Ruby code

require 'nokogiri'

input = "input.wxs"
template = "template.wxs"
output = "output.wxs"

input_file = Nokogiri::XML(File.open(input))
template_file = Nokogiri::XML(File.open(template))

#get stuff from input file 
stuff = input_file.at_css("Directory[Id='TARGETDIR']")

#where to insert in template file 
insert_point = template_file.at_css("DirectoryRef[Id='InstallDir']")

insert_point.children= stuff.children()

#write out filled template to output file
File.open(output, 'w') { |f| template_file.write_xml_to f }

input file

<?xml version="1.0" encoding="utf-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
    <Product>
        <Package />
        <Directory Id="TARGETDIR" Name="SourceDir">
            <Component>
                <File />
            </Component>
        </Directory>
    </Product>
</Wix>

template file

<?xml version="1.0" encoding="utf-8"?>
<Wix xmlns='http://schemas.microsoft.com/wix/2006/wi'>
  <Fragment Id='MSIComponents'>
      <DirectoryRef Id='InstallDir'>
      </DirectoryRef>
  </Fragment>
</Wix>

Actual Output

<?xml version="1.0" encoding="utf-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
  <Fragment Id="MSIComponents">
      <DirectoryRef Id="InstallDir">
            <default:Component>
                <File/>
            </default:Component>
        </DirectoryRef>
  </Fragment>
</Wix>

Expected output

<?xml version="1.0" encoding="utf-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
  <Fragment Id="MSIComponents">
      <DirectoryRef Id="InstallDir">
            <Component>
                <File/>
            </Component>
        </DirectoryRef>
  </Fragment>
</Wix>

Nokogiri -v

warnings: []

nokogiri: 1.4.4.1
ruby:
version: 1.9.2
platform: i386-mingw32
engine: ruby
libxml:
binding: extension
compiled: 2.7.7
loaded: 2.7.7

Owner

flavorjones commented Dec 29, 2010

Best. Bug report. Evar. I'll take a look tonight.

rkj commented Jan 4, 2011

I've stumbled on the same problem using Node#replace

nokogiri -v

warnings: []

ruby:
engine: ruby
version: 1.8.7
platform: x86_64-linux
libxml:
loaded: 2.7.8
binding: extension
compiled: 2.7.8
nokogiri: 1.4.4

Fast & dirty workaround for me was #gsub ;-)

nickcanz commented Jan 4, 2011

Workaround was to remove the xmlns attribute in the input file.

Or to use the remove_namespaces! method when opening the input file

input_file = Nokogiri::XML(File.open(input))
input_file.remove_namespaces!

dusanb commented Jan 26, 2011

Similar to ac2533, I got around this by adding the node for insertion to a new, temporary Nokogiri doc, then invoked remote_namespaces!.

After that I could add the node without the namespaces to the actual destination XML doc.

Don't really think this issue should be closed though! :)

Owner

flavorjones commented Jan 26, 2011

This ticket should NOT have been closed. I've reopened it.

Any update on this ?

@flavorjones flavorjones self-assigned this Mar 2, 2015

@flavorjones flavorjones removed the flavorjones label Mar 2, 2015

Contributor

ylecuyer commented Nov 15, 2015

👍 Any update on this ? @flavorjones

ylecuyer added a commit to ylecuyer/nokogiri that referenced this issue Nov 15, 2015

ylecuyer added a commit to ylecuyer/nokogiri that referenced this issue Nov 15, 2015

Owner

flavorjones commented Feb 17, 2016

Confirmed this is still an issue with Nokogiri 1.6.8.rc3.

Owner

flavorjones commented Feb 17, 2016

Updated example code:

#! /usr/bin/env ruby

require 'nokogiri'

input = <<EOX
<?xml version="1.0" encoding="utf-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
    <Product>
        <Package />
        <Directory Id="TARGETDIR" Name="SourceDir">
            <Component>
                <File />
            </Component>
        </Directory>
    </Product>
</Wix>
EOX

template = <<EOX
<?xml version="1.0" encoding="utf-8"?>
<Wix xmlns='http://schemas.microsoft.com/wix/2006/wi'>
  <Fragment Id='MSIComponents'>
      <DirectoryRef Id='InstallDir'>
      </DirectoryRef>
  </Fragment>
</Wix>
EOX

input_file = Nokogiri::XML(input)
template_file = Nokogiri::XML(template)

# get stuff from input file, put into template
stuff = input_file.at_css("Directory[Id='TARGETDIR']")
insert_point = template_file.at_css("DirectoryRef[Id='InstallDir']")
insert_point.children = stuff.children()

puts template_file.to_xml

@flavorjones flavorjones added this to the 1.6.8 milestone Feb 17, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment