473,238 Members | 1,684 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,238 software developers and data experts.

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 2220


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: kj | last post by:
Consider the following XML document: <?xml version='1.0' encoding='UTF-8'?> <bar:foo xmlns:bar='someuri'> <baz/> </bar:foo> What namespace does baz belong to? What is this namespace bound...
1
by: indo3 | last post by:
HELLO For the top root element of my schema file, i want to declare following attribute: <xs:attribute name="xmlns:m" type="xs:string" default="http://www.w3.org/1998/Math/MathML"/> But...
8
by: Hugh Sparks | last post by:
When processing an xml document that contains elements such as: <element xmlns="goop"/> ... <element xmlns="gleep"/> I want to create two xsl stylesheet templates: one that matches the...
4
by: Hollywood | last post by:
I'm using XML serialization to produce the following XML document: <TestDoc xmlns:srd="some-url"> <Additional> <Security> <srd:Login>login_id</srd:Login> <srd:Password>password</srd:Password>...
6
by: JoostV | last post by:
How can I set the default namespace of an XmlDocument/XmlElement? I've tried doing something like rootElement.SetAttribute( "xmlns", "http://www.w3.org/2000/xmlns/",...
0
by: Peter Theill | last post by:
Hi, I'm having an issue with deserializing some xml using a proxy class generated by "xsd.exe". My proxy class contains this class definition: public class IqProfileType {
3
by: George | last post by:
I am currently developing an xbrl validation software that takes an xml instance file and a lot of schemas(xsd files) and validates it against the xsd files. I am using Visual basic in visual...
9
by: Mark Olbert | last post by:
I'm trying to serialize (using XmlSerializer.Serialize) a class that I generated from an XSD schema using XSD.EXE /c. The problem I'm running into is that the root element needs to be unqualified,...
7
by: WTH | last post by:
I am now aware (I am primarily a C++ developer) that in C# if you reference the same interface from the same file in two different projects the types are actually incompatible. I found this out...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.