removing namespaces from nodes

916 views
Skip to first unread message

_Kevin

unread,
Jan 10, 2010, 9:28:35 AM1/10/10
to nokogiri-talk
Is there a way to completely remove namespaces from an xml node? I
see methods for adding them, but none for removing them.

Iñaki Baz Castillo

unread,
Jan 10, 2010, 2:22:39 PM1/10/10
to nokogi...@googlegroups.com
El Domingo, 10 de Enero de 2010, _Kevin escribió:
> Is there a way to completely remove namespaces from an xml node? I
> see methods for adding them, but none for removing them.

It's not easy. Imagine this case:

<root>
<child xmlns="urn:ns.animals" xmlns:kk="urn:ns.cats">
<dog />
<kk:cat />
</child>
</root>

When Nokogiri parses this XML it assigns to <dog> a XML namespace definition
with "nil" as key and "urn:ns.animals" as href. And it assigns to <cat> a XML
namespace definition with "kk" as prefix and "urn:ns.cats" as href.

So then, what does it mean to delete the namespace with prefix "kk"?
It would involve inspecting *all* the elements contained in the <child> node
and reseting the namespace definitions for all of them (inckluding attributes
with namespace), and after that the "xmlns" declaration could be removed from
the <child> element.

Not sure why a XML namespace should be removed, IMHO it doesn't make sense.
However if you want to avoid playing with namespaces there is some option in
Nokogiri to just "forget" them and act as if the XML document has no
namespaces.

Regards.


--
Iñaki Baz Castillo <i...@aliax.net>

Aaron Patterson

unread,
Jan 10, 2010, 2:53:38 PM1/10/10
to nokogi...@googlegroups.com
On Sun, Jan 10, 2010 at 6:28 AM, _Kevin <kevin....@gmail.com> wrote:
> Is there a way to completely remove namespaces from an xml node?  I
> see methods for adding them, but none for removing them.

It's not very easy in the current release (1.4.1). With the released
version, you can call "remove_namespaces!" on the document to remove
*all* namespaces, but not a namespace on a particular node.

I've made XML::Node#namespace= accept nil as a parameter, so if you
get the nightly build, you can use that:

http://github.com/tenderlove/nokogiri/commit/52994fd3cbdcf77a3b88d0fac41bff706c735e79

Otherwise, this will be in the 1.4.2 release.

--
Aaron Patterson
http://tenderlovemaking.com/

_Kevin

unread,
Jan 10, 2010, 10:33:06 PM1/10/10
to nokogiri-talk
Thanks guys. The reason I needed this in the first place was because
nokogiri was incorrectly assigning namespaces to some nodes when it
should not have....

something like
<aa:tag>
<foo></foo>
</aa:tag>

was getting parsed to

<aa:tag>
<aa:foo></aa:foo>
</aa:tag>

Having looked in the issue tracker, it seems that this may be related
to a bug which 1.4.2 may fix. Hopefully with the fix, there will be
no need to remove namespaces.

-Kevin

On Jan 10, 2:53 pm, Aaron Patterson <aaron.patter...@gmail.com> wrote:


> On Sun, Jan 10, 2010 at 6:28 AM, _Kevin <kevin.olbr...@gmail.com> wrote:
> > Is there a way to completely remove namespaces from an xml node?  I
> > see methods for adding them, but none for removing them.
>
> It's not very easy in the current release (1.4.1).  With the released
> version, you can call "remove_namespaces!" on the document to remove
> *all* namespaces, but not a namespace on a particular node.
>
> I've made XML::Node#namespace= accept nil as a parameter, so if you
> get the nightly build, you can use that:
>

>  http://github.com/tenderlove/nokogiri/commit/52994fd3cbdcf77a3b88d0fa...

Mike Dalessio

unread,
Jan 10, 2010, 11:46:42 PM1/10/10
to nokogiri-talk
Hi Kevin,

I'd love to see your full example for where Nokogiri is "incorrectly assigning namespaces to some nodes". We are currently considering some changes to namespace behavior, but not much is actually planned for 1.4.2.

We have one opinionated user who's been giving us free advice, but I'd like other people to give us some input before we make these changes.

-m


--
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
mi...@csa.net

Iñaki Baz Castillo

unread,
Jan 11, 2010, 3:45:47 AM1/11/10
to nokogi...@googlegroups.com
El Lunes, 11 de Enero de 2010, _Kevin escribió:
> Thanks guys. The reason I needed this in the first place was because
> nokogiri was incorrectly assigning namespaces to some nodes when it
> should not have....
>
> something like
> <aa:tag>
> <foo></foo>
> </aa:tag>
>
> was getting parsed to
>
> <aa:tag>
> <aa:foo></aa:foo>
> </aa:tag>

I assume this is a fragment you are inserting in a XML document, right?

Does the XML document root node have a default xml namespace declaration? this
is:

<root xmlns="urn:default.ns" xmlns:aa="urn.aa.ns">

If so then Nokogiri would respect the unprefixed <foo> node. If not, Nokogiri
will force it to have a valid (any valid) namespace.

Cuuld you show the original XML indicating the exact node in which you want to
insert the above fragment?

Depending on how it looks you could also try again with this fork:

http://github.com/ibc/nokogiri/tree/namespaces
(ensure you use the "namespaces" branch in this fork)

Reply all
Reply to author
Forward
0 new messages