Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nokogiri adds 'default' Namespace to elements #391

Closed
nickcanz opened this issue Dec 29, 2010 · 13 comments
Closed

Nokogiri adds 'default' Namespace to elements #391

nickcanz opened this issue Dec 29, 2010 · 13 comments
Assignees
Milestone

Comments

@nickcanz
Copy link

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

@flavorjones
Copy link
Member

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

@rkj
Copy link

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
Copy link
Author

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
Copy link

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! :)

@flavorjones
Copy link
Member

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

@yuanhang
Copy link

Any update on this ?

@flavorjones flavorjones self-assigned this Mar 2, 2015
@ylecuyer
Copy link
Contributor

👍 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
@flavorjones
Copy link
Member

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

@flavorjones
Copy link
Member

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

@jrochkind
Copy link
Contributor

I'm still having a problem with this. I will try to isolate and file a separate issue.

If there was a way to remove a namespace from a Nokogiri doc, it would be easy to clean up to work around. But while there is an add_namespace_definition method, there seems to be no way to remove a namespace once it's already been added (in this case an errant default namespace).

@mpsuzuki
Copy link

@jrochkind , have you filed a separate issue? I've in the similar trouble and trying to find the root of the problem is too old nokogiri or still some workarounds for this issue are needed.

@jrochkind
Copy link
Contributor

No, I think I just gave up on my approach, can't recall sorry.

@mpsuzuki
Copy link

thanks! still it's helpful info.

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

Successfully merging a pull request may close this issue.

8 participants