473,404 Members | 2,137 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,404 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 2234


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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.