469,142 Members | 1,125 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,142 developers. It's quick & easy.

Different namespace on attribute

Hi,

My xml document uses different namespaces:
<x:root xmlns:x="x:ns:meta/">
<element1 xmlns:adam="http://ns.alfaprint.be/">
</root>

This gives problems when using an XPath query.

I already created 2 namespace managers
XmlNamespaceManager mgr1 = new
XmlNamespaceManager(oXmlDocument.NameTable);
mgr1.AddNamespace("x", "x:ns:meta");
XmlNamespaceManager mgr2 = new XmlNamespaceManager(new NameTable());
mgr2.AddNamespace("adam", "http://ns.alfaprint.be/");

I tried the following 2 things:
XmlNode oXmlNode =
oXmlRoot.SelectSingleNode("//x:Root[@xmlns:adam='http://ns.alfaprint.be/']",
mgr1);
This statement returns oXmlNode to be null;

XmlNode oXmlNode =
oXmlRoot.SelectSingleNode("//x:Root[@xmlns:adam='http://ns.alfaprint.be/']",
mgr2);
This returns an exception telling that the namespace of the root
element is not declared.

Does anyone can help me on this?

Thanks in advance. Kind regards,

Karine Bosch

Feb 3 '06 #1
4 2017


Karine Bosch wrote:

My xml document uses different namespaces:
<x:root xmlns:x="x:ns:meta/">
<element1 xmlns:adam="http://ns.alfaprint.be/">
</root>

This gives problems when using an XPath query.

I already created 2 namespace managers
XmlNamespaceManager mgr1 = new
XmlNamespaceManager(oXmlDocument.NameTable);
mgr1.AddNamespace("x", "x:ns:meta");
I tried the following 2 things:
XmlNode oXmlNode =
oXmlRoot.SelectSingleNode("//x:Root[@xmlns:adam='http://ns.alfaprint.be/']",
mgr1);


Namespace nodes are not attribute nodes in the XPath data model so doing
@xmlns does not make much sense.
I don't get what you are looking for with that query, your element above
is named root in the namespace with URI
x:ns:meta/
yet when you add a namespace to the namespace manager you use a
different URI
x:ns:meta
so that way you can't find any elements in the namespace with URI
x:ns:meta/.
And element and attribute names are case sensitive, so having root in
the markup but Root in the XPath expression can't find that element.
So doing
mgr1.AddNamespace("x", "x:ns:meta/");
and then
oXmlRoot.SelectSingleNode("/x:root")
finds you the root element with local name root in the namespace with
namespace URI x:ns:meta/.

If you want to find all elements with that name at all levels then you
need the XPath
//x:root
but obviously then you might want to use SelectNodes.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Feb 3 '06 #2
ok,

I'll rephrase my question (or be more specific and more accurate).

My xml document uses different namespaces:
<x:root xmlns:x="x:ns:meta/">
<element1>a</element1>
<element1>b</element1>
...
<element1 xmlns:adam="http://ns.alfaprint.be/">x</element1>
<element1>y</element1>
<element1>z</element1>
...
</root>
This gives problems when using an XPath query.

I already created 2 namespace managers
XmlNamespaceManager mgr1 = new
XmlNamespaceManager(oXmlDocument.NameTable);
mgr1.AddNamespace("x", "x:ns:meta/");
XmlNamespaceManager mgr2 = new XmlNamespaceManager(new NameTable());
mgr2.AddNamespace("adam", "http://ns.alfaprint.be/");
I tried the following 2 things:
XmlNode oXmlNode =
oXmlRoot.SelectSingleNode("//x:root[@xmlns:adam='http://ns.alfaprint.be/']",

mgr1);
This statement returns oXmlNode to be null;
XmlNode oXmlNode =
oXmlRoot.SelectSingleNode("//x:root[@xmlns:adam='http://ns.alfaprint.be/']",

mgr2);
This returns an exception telling that the namespace of the root
element is not declared.

To be clear about some points:
- I'm not the author of the xml document. I only have to interprete
some of the nodes. So the structure cannot be changed.
- As the document can contain hundreds of elements, it's clear that I'm
looking to extract that one node that is of interest for me by using a
SelectSingleNode. Indeed I can loop through the whole document but that
would mean a performance decrease.

Does anyone can help me on this, please?

Thanks in advance. Kind regards,
Karine Bosch

Feb 4 '06 #3


Karine Bosch wrote:

My xml document uses different namespaces:
<x:root xmlns:x="x:ns:meta/">
<element1>a</element1>
<element1>b</element1>
...
<element1 xmlns:adam="http://ns.alfaprint.be/">x</element1>
<element1>y</element1>
<element1>z</element1>
...
</root>
I see two namespace declared but only one being used as only the x:root
element is in a namespace, the namespace with URI x:ns:meta/. I don't
see any element or attribute in the namespace with URI
http://ns.alfaprint.be/, there is only a declaration binding the prefix
adam to that namespace URI.

I tried the following 2 things:
XmlNode oXmlNode =
oXmlRoot.SelectSingleNode("//x:root[@xmlns:adam='http://ns.alfaprint.be/']", XmlNode oXmlNode =
oXmlRoot.SelectSingleNode("//x:root[@xmlns:adam='http://ns.alfaprint.be/']",

mgr2);
- As the document can contain hundreds of elements, it's clear that I'm
looking to extract that one node that is of interest for me by using a
SelectSingleNode.


But which node exactly are you looking for? I have already told you if
you want the root element in your example then
xmlDocument.SelectSingleNode("/x:root", mgr1)
will give you the root element.

It is not clear what
//x:root[@xmlns:adam='http://ns.alfaprint.be/']
is supposed to find in your example document.
//x:root
makes some sense in general but in your example markup
/x:root
would suffice. But use
//x:root
if you want/need.
What the predicate
@xmlns:adam='http://ns.alfaprint.be/'
however is supposed to do I don't know, as said namespace declarations
do not show up as attribute nodes in the XPath data model so writing
@xmlns:adam where @something in XPath alway selects an attribute does
not make sense.

Do an XPath evaluation of e.g.
//@*
or
//*/@*
on that example XML, it will not find any nodes, those namespace
declarations are not attributes in terms of XPath.

If you want to find namespace nodes then you need to use the namespace
axis.

So doing e.g.
//element1[namespace::adam]
would find you the element with the markup
<element1 xmlns:adam="http://ns.alfaprint.be/">x</element1>
in your example markup. No namespace manager is needed in .NET to
evaluate that expression.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Feb 4 '06 #4
Hi Martin,

Thanks for your help. Indeed, I don't need 2 namespace managers. I used
the syntax you suggested (//element1[namespace::adam]) and it worked!

Thank you very much!

Kind regards,

Karine Bosch

Feb 6 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

25 posts views Thread by kj | last post: by
1 post views Thread by indo3 | last post: by
4 posts views Thread by Hollywood | last post: by
6 posts views Thread by JoostV | last post: by
reply views Thread by Peter Theill | last post: by
1 post views Thread by CARIGAR | last post: by
1 post views Thread by Mortomer39 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.