Nokogiri Adds a "default" namespace

42 views
Skip to first unread message

Nicholas Canzoneri

unread,
Dec 29, 2010, 11:00:44 AM12/29/10
to nokogi...@googlegroups.com
Hi Folks,

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

The problem comes when I save the new file out, the nodes that I added
are all getting "default" namespace added to them, when I don't want
that.

I put a sample of my code and input files on a stackoverflow question.
Let me know if you need any more information.
http://stackoverflow.com/questions/4554993/get-nokogiri-to-not-add-default-namespace-when-adding-nodes

Thanks for any help.

--
Nick Canzoneri
ac3...@gmail.com
@ac3522

Jonathan Rochkind

unread,
Dec 29, 2010, 11:15:34 AM12/29/10
to nokogi...@googlegroups.com, Nicholas Canzoneri
Namespaces in nokogiri, combined with manipulation (rather than just
querying) of the xml tree, are really a pain in my experience. Probably
in large part -- I'm guessing with no real experience with libxml raw --
because they are a confusing pain in libxml, and nokogiri with namespace
stuff (unlike in other areas) doesn't really make them prettier for you,
it just exposes them (perhaps not always entirely consistently).

I spent some time trying to figure out how to do some things similar to
what I think you are describing, with limited success. If you post more
details, including a small demonstration of actual XML and what you want
to do it, I might (or might not, I still don't fully understand how to
deal with this in nokogiri) have suggestions from my experience.

Jonathan

Nicholas Canzoneri

unread,
Dec 29, 2010, 11:29:29 AM12/29/10
to nokogi...@googlegroups.com
Here is a sample Jonathan.

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']")
  
    #where to insert in template doc
    template_component_insert_point = template_file.at_css("DirectoryRef[Id='InstallDir']")
    
    #puts template_component_insert_point.methods
    template_component_insert_point.children= components.children()

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


My generated xml:

<?xml version="1.0" encoding="utf-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
   <Product Id="{23E18C28-79B6-4C4A-B40A-9E2AB337197C}" Codepage="1252" Language="1033" Manufacturer="Microsoft" Name="WebSetup" UpgradeCode="{85DD3A3B-4CAD-4288-A612-729555D447B8}" Version="1.0.0">
       <Package Compressed="yes" InstallerVersion="200" Languages="1033" Manufacturer="Microsoft" Platform="x86" />
       <Binary Id="MSVBDPCADLL" SourceFile="generated/binaries\Binary\MSVBDPCADLL" />
       <Binary Id="VSDNETCFG" SourceFile="generated/binaries\Binary\VSDNETCFG" />
           ----snip out generated stuff ------
           ----I want everything in the TARGETDIR node ----
        <Directory Id="TARGETDIR" Name="SourceDir">
            <Component Id="C__AD1817F9C64A42F0A14DDDDC82DFC8D9" Guid="{B8CF0FFD-9681-1951-C6C6-7E4152FE28F4}" KeyPath="yes">
                <CreateFolder Directory="TARGETDIR" />
            </Component>
            ----snip out a lot more component nodes----
            <Component Id="C__06370B56BE12CAF66B15B23C487176A5" Guid="{FD941172-CA4A-8237-B62B-07A136CECCE0}">
                <File Id="_06370B56BE12CAF66B15B23C487176A5" Name="Web.config" KeyPath="yes" ShortName="WEB~1.CON" DiskId="1" Source="generated/binaries\File\_06370B56BE12CAF66B15B23C487176A5"/>
            </Component>
         </Directory>
        -----snip out more generated stuff----
    </Product>
</Wix>


My template file looks like:

<?xml version="1.0" encoding="utf-8"?>
  <Fragment>
    <ComponentGroup Id='MSIComponentGroup'>
    </ComponentGroup>
  </Fragment>
  <Fragment Id='MSIComponents'>
      <DirectoryRef Id='InstallDir'>
      </DirectoryRef>
  </Fragment>
</Wix>

And the output that I get looks like (note the default prepended to the Component nodes):

<?xml version="1.0" encoding="utf-8"?>
  <Fragment>
    <ComponentGroup Id='MSIComponentGroup'>
    </ComponentGroup>
  </Fragment>
  <Fragment Id='MSIComponents'>
      <DirectoryRef Id='InstallDir'>
              <default:Component Id="C__AD1817F9C64A42F0A14DDDDC82DFC8D9" Guid="{B8CF0FFD-9681-1951-C6C6-7E4152FE28F4}" KeyPath="yes">
                <CreateFolder Directory="TARGETDIR"/>
              </default:Component>
              <default:Component Id="C__06370B56BE12CAF66B15B23C487176A5" Guid="{FD941172-CA4A-8237-B62B-07A136CECCE0}">
                <File Id="_06370B56BE12CAF66B15B23C487176A5" Name="Web.config" KeyPath="yes" ShortName="WEB~1.CON" DiskId="1" Source="generated/binaries\File\_06370B56BE12CAF66B15B23C487176A5"/>
              </default:Component>
      </DirectoryRef>
  </Fragment>
</Wix>

Jonathan Rochkind

unread,
Dec 29, 2010, 11:36:50 AM12/29/10
to nokogi...@googlegroups.com, Nicholas Canzoneri
Okay, that's weird as heck, since it seems to be actually generating invalid namespaced XML, yeah?  "default" is not a declared namespace prefix, and is not valid to use as a namespace prefix unless it's declared, as far as I know there's no built-in allowance to use the string "default" to mean the default namespace, you just leave off a namespace for that.

So yeah, it would seem to me something is going wrong. Never ran into that one before myself, it gave me enough of a headache trying to figure out the stuff I did run into, sorry.
--
You received this message because you are subscribed to the Google Groups "nokogiri-talk" group.
To post to this group, send email to nokogi...@googlegroups.com.
To unsubscribe from this group, send email to nokogiri-tal...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nokogiri-talk?hl=en.

Mike Dalessio

unread,
Dec 29, 2010, 12:15:33 PM12/29/10
to nokogi...@googlegroups.com
Hi,

Please file a bug for this. You can see submission guidelines at http://bit.ly/nokohelp 

A complete, self-contained running example, along with Nokogiri version info (output from `nokogiri -v`), would go a long way towards helping us quickly diagnose and fix this issue.

Thanks for using Nokogiri!

 

Jonathan Rochkind

unread,
Dec 29, 2010, 12:19:14 PM12/29/10
to nokogi...@googlegroups.com
There is probably a workaround if you mess with the namespaces of the component nodes before inserting them into the new document (and/or remove the xmlns decleration from the "dark output" file before even parsing it with nokogiri, followed by then messing with the namespaces of the component nodes into the new document), but I can't say exactly what it is.  But it's that kind of sort of aimless cargo cult messing around I had to do to get certain namespacey things to work in my cases.

Nicholas Canzoneri

unread,
Dec 29, 2010, 3:49:34 PM12/29/10
to nokogi...@googlegroups.com
Thanks for the help guys.
I made a bug report here: https://github.com/tenderlove/nokogiri/issues#issue/391
I'll post any workarounds if I find any.

Nick Canzoneri
ac3...@gmail.com
@ac3522

Nicholas Canzoneri

unread,
Jan 4, 2011, 12:16:59 PM1/4/11
to nokogi...@googlegroups.com
Follow up.

As Jonathan suggested, removing the xmlns attribute from my generated output fixes the problem. 

But that isn't ideal, since I don't want to have to modify a file before using it, so when opening the document in nokogiri I used the remove_namespaces! method.

My ruby code is now the following, with changes in bold:

dark_output = Nokogiri::XML(File.open(file))
dark_output.remove_namespaces!
Reply all
Reply to author
Forward
0 new messages