Nick Canzoneri
Nick Canzoneri

Reputation: 3804

Get Nokogiri to not add "default" namespace when adding nodes

Background:

I want to take some xml from one file, put it in a template file and then save the modified template as a new file. It works, but when I save the file out, all the nodes that I added have a default namespace prepeneded, i.e.

        <default:ComponentRef Id="C__AD1817F9C64A42F0A14DDDDC82DFC8D9"/>
        <default:ComponentRef Id="C__157DD41D70854617A3D6D1E4A39B589F"/>
        <default:ComponentRef Id="C__2E6D8662F38FE62CAFA9F8842A28F510"/>
        <default:ComponentRef Id="C__54E5E2181323D4A5F37293DAA87B4230"/>

Which I want to be just:

        <ComponentRef Id="C__AD1817F9C64A42F0A14DDDDC82DFC8D9"/>
        <ComponentRef Id="C__157DD41D70854617A3D6D1E4A39B589F"/>
        <ComponentRef Id="C__2E6D8662F38FE62CAFA9F8842A28F510"/>
        <ComponentRef Id="C__54E5E2181323D4A5F37293DAA87B4230"/>

The following is my ruby code:

file = "wixmain/generated/DarkOutput.wxs"
template = "wixmain/generated/MsiComponentTemplate.wxs"
output = "wixmain/generated/MSIComponents.wxs"

dark_output = Nokogiri::XML(File.open(file))
template_file = Nokogiri::XML(File.open(template))

#get stuff from dark output
components = dark_output.at_css("Directory[Id='TARGETDIR']")
component_ref = dark_output.at_css("Feature[Id='DefaultFeature']")

#where to insert in template doc
template_component_insert_point = template_file.at_css("DirectoryRef[Id='InstallDir']")
template_ref_insert_point = template_file.at_css("ComponentGroup[Id='MSIComponentGroup']")

template_component_insert_point.children= components.children()
template_ref_insert_point.children= component_ref.children()

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

Update

Example of my template file:

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

Upvotes: 1

Views: 1253

Answers (2)

Nick Canzoneri
Nick Canzoneri

Reputation: 3804

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!

Upvotes: 6

the Tin Man
the Tin Man

Reputation: 160571

I think you are missing a sample of the template file. Also, is the sample from the input complete?

Nokogiri is either finding the default: namespace during its parsing of one of the two files, and you are inheriting it, or maybe it is not happy with the sample during parsing and is unable to parse cleanly, and as a result somehow adding the default: namespace. You can check the emptiness of the errors array after parsing the dark_output and template_file to see if Nokogiri is happy.

dark_output = Nokogiri::XML(File.open(file))
template_file = Nokogiri::XML(File.open(template))

if (dark_output.errors.any? || template_file.errors.any?)
  [... do something here ...]
end

For the fastest answer, you might want to take this question directly to the developers via the Nokogiri-Talk mail-list.

Upvotes: 0

Related Questions