---------------------
<?xml version="1.0"?>
<foo xmlns="urn:test:default-namespace">
<ns1:bar xmlns:ns1="urn:test:namespace1-uri"
xmlns="urn:test:namespace1-uri">
<baz/>
<ns2:baz xmlns:ns2="urn:test:namespace2-uri"/>
</ns1:bar>
<ns3:hi xmlns:ns3="urn:test:namespace3-uri">
<there/>
</ns3:hi>
</foo>
---------------------
If I do a Xpath as '/foo' with no namespaces then I get no node.
However if I remove the 'xmlns="urn:test:default-namespace"' in the <foo> node
then I get the node.
Is it the expected behaviour?
If so, how could I get the <foo> node without removing the "xmlns" declaration
and without using namespaces in the Xpath query?
Perhaps node.default_namespace is useful for this purpose?
Thanks a lot.
--
Iñaki Baz Castillo <i...@aliax.net>
Humm, but I cannot understand it. Note that if I do the following Xpath query
it also returns an empty node!:
xml.xpath("/foo", {"xmlns"=>"urn:test:default-namespace"})
=> []
Why??
The only way to make it working is by adding "xmlns" to the Xpath, then it
works even if namespaces are not included in the Xpath query:
xml.xpath("/xmlns:foo")
=> result OK!
xml.xpath("/xmlns:foo", {"xmlns"=>"urn:test:default-namespace"})
=> result OK!
But, is really required to use "/xmlns:foo" in the Xpath query? is not valid
just to use "/foo"?
Thanks.
I wanted to expect that the following could work:
@xml.xpath("/foo", {nil=>"urn:test:default-namespace"})
but obviously it gives an error:
"NoMethodError: undefined method `gsub' for nil:NilClass"
So I've a big problem as XCAP protocol allows the client sending a Xpath query
without namespaces, but there is a document default namespace (in the case
above "urn:test:default-namespace") which should be used to inspect the XML
document in the server.
This is, it's not required that the client uses:
xml.xpath("/xmlns:foo", {"xmlns"=>"urn:test:default-namespace"})
and instead it could just use:
xml.xpath("/foo")
and the server must understand than the namespaces used are:
"xmlns"=>"urn:test:default-namespace"
However I cannot "rewrite" the Xpath query sent by the client (since I don't
know how to do it in a proper way).
Is there any solution to get what I need? Thanks a lot.
Yes.
xmlns defines a default namespace. That is, if no *explicit*
namespace is defined for the node, the *implicit* xmlns namespace is
used for the declaring node and all children of that node.
> If so, how could I get the <foo> node without removing the "xmlns" declaration
> and without using namespaces in the Xpath query?
> Perhaps node.default_namespace is useful for this purpose?
No. node.default_namespace gives you the "xmlns" namespace if it
exists. In this case, the word "default" really means explicit vs
implicit. The "default" being the implicit namespace.
--
Aaron Patterson
http://tenderlovemaking.com/
This should not be expected to work. A namespace name cannot be
blank. When you make a query like:
/foo
You are requesting nodes named "foo" that *belong to no namespace*.
Your node belongs to a namespace. In XPath, the namespace prefix is
*required* to indicate that you are looking for a node that belongs to
a namespace.
>
> but obviously it gives an error:
> "NoMethodError: undefined method `gsub' for nil:NilClass"
>
> So I've a big problem as XCAP protocol allows the client sending a Xpath query
> without namespaces, but there is a document default namespace (in the case
> above "urn:test:default-namespace") which should be used to inspect the XML
> document in the server.
> This is, it's not required that the client uses:
> xml.xpath("/xmlns:foo", {"xmlns"=>"urn:test:default-namespace"})
> and instead it could just use:
> xml.xpath("/foo")
> and the server must understand than the namespaces used are:
> "xmlns"=>"urn:test:default-namespace"
>
> However I cannot "rewrite" the Xpath query sent by the client (since I don't
> know how to do it in a proper way).
>
> Is there any solution to get what I need? Thanks a lot.
Possibly. If the client *always* sends non-namespaced queries, you
could remove all namespaces from the document:
http://nokogiri.org/Nokogiri/XML/Document.html#M000385
But if the client mixes queries, then you have a problem. I'm not
sure what to do about that. Tell the client to fix their software?
Yes, I understand, but as I said in XCAP the client is allowed to use a Xpath
query with no namespaces and a default namespace is supposed to be used :(
> > but obviously it gives an error:
> > "NoMethodError: undefined method `gsub' for nil:NilClass"
> >
> > So I've a big problem as XCAP protocol allows the client sending a Xpath
> > query without namespaces, but there is a document default namespace (in
> > the case above "urn:test:default-namespace") which should be used to
> > inspect the XML document in the server.
> > This is, it's not required that the client uses:
> > xml.xpath("/xmlns:foo", {"xmlns"=>"urn:test:default-namespace"})
> > and instead it could just use:
> > xml.xpath("/foo")
> > and the server must understand than the namespaces used are:
> > "xmlns"=>"urn:test:default-namespace"
> >
> > However I cannot "rewrite" the Xpath query sent by the client (since I
> > don't know how to do it in a proper way).
> >
> > Is there any solution to get what I need? Thanks a lot.
>
> Possibly. If the client *always* sends non-namespaced queries, you
> could remove all namespaces from the document:
>
> http://nokogiri.org/Nokogiri/XML/Document.html#M000385
>
> But if the client mixes queries, then you have a problem.
This is not valid for me as the XML I manage have always 2-3 namespaces :(
> I'm not
> sure what to do about that. Tell the client to fix their software?
Unfortunatelly it's not a bug. Perhaps it's a bad designed specification
(XCAP) which "corrupts" the Xpath specification.