Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / October 2003

Tip: Looking for answers? Try searching our database.

XPath and namespace problem

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Andreas H?kansson - 03 Oct 2003 19:24 GMT
I have a price of XML that looks like this

<Root>
   <SomeNode>
       .....
   </SomeNode>
   <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
       ...
   </Signature>
</Root>

If I load this into an XmlDocument object and try calling
SelectSingleNode("//Signature") to
determine if the signature has been added or not, then it alwasy return
null. I have managed
to get it that I probably need a XmlNamespaceManager object and pass it to
the method
as well, however I do not know how to set this up since the new namespace
declared in
the <Signature> node doesn't use a prefix and I can not alter the XML to
setup a prefix
either since the <Signature> section is generated by some other code and
need to comply
with a W3C schema =)

Any suggestions on how to be able to tell if the <Signature> node it present
or not, using
XPath and a XmlNamespaceManager object ? =)

Signature

ANDREAS H?KANSSON
STUDENT OF SOFTWARE ENGINEERING
andreas (at) selfinflicted.org

Andreas Håkansson - 03 Oct 2003 20:07 GMT
Nevermind.. figured it out..

Create a fake namespace alias in the XmlNamespaceManager and prefix you node in the
XPath with it..

XmlNamespaceManager manager = new XmlNamespaceManager();
manager.AddNamespace("test", "http://www.w3.org/2000/09/xmldsig#");

XmlNode n = doc.SelectSingleNode("//test:Signature");

=)

Signature

ANDREAS HÅKANSSON
STUDENT OF SOFTWARE ENGINEERING
andreas (at) selfinflicted.org
 "Andreas Håkansson" <andreas@selfinflicted.org> wrote in message news:OKUHludiDHA.1368@TK2MSFTNGP12.phx.gbl...
 I have a price of XML that looks like this

 <Root>
     <SomeNode>
         .....
     </SomeNode>
     <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
         ...
     </Signature>
 </Root>

 If I load this into an XmlDocument object and try calling
 SelectSingleNode("//Signature") to
 determine if the signature has been added or not, then it alwasy return
 null. I have managed
 to get it that I probably need a XmlNamespaceManager object and pass it to
 the method
 as well, however I do not know how to set this up since the new namespace
 declared in
 the <Signature> node doesn't use a prefix and I can not alter the XML to
 setup a prefix
 either since the <Signature> section is generated by some other code and
 need to comply
 with a W3C schema =)

 Any suggestions on how to be able to tell if the <Signature> node it present
 or not, using
 XPath and a XmlNamespaceManager object ? =)

 --
 ANDREAS HÅKANSSON
 STUDENT OF SOFTWARE ENGINEERING
 andreas (at) selfinflicted.org

Wray Smallwood - 09 Oct 2003 09:12 GMT
Thanks, I was looking for a workaround as well as I could not get mine to work. However despite the fact that what you have come up with below works, in my opinion this is a blatant bug.

To be specific if you have a DefaultNamespace you have to fudge an XPath search to make it work:

If your XML "xmlfile1.xml" has:

<?xml version="1.0" encoding="utf-8" ?><myObject xmlns="http://tempuri.org/MyObject.xsd"><Object1> ...etc

which has a default namespace, instead of one with a named namespace = "anamespace":

<?xml version="1.0" encoding="utf-8" ?><myObject xmlns:anamespace="http://tempuri.org/MyObject.xsd"><Object1> ...etc

then the following code will not work!!!!

Dim xdd As XmlDocument = New XmlDocument
xdd.Load("..\xmlfile1.xml")
Dim nod As XmlNode
Dim nsmgr As XmlNamespaceManager = New XmlNamespaceManager(xdd.NameTable)
nsmgr.AddNamespace(String.Empty, "http://tempuri.org/MyObject.xsd")  ' Add a default namespace
nod = xdd.SelectSingleNode("//Object1", nsmgr)

The node is not found despite the fact that the documentation for .NET says that it will.

Fudging the last two lines works:
nsmgr.AddNamespace("fakenamespace", "http://tempuri.org/MyObject.xsd")  ' Add a default namespace
nod = xdd.SelectSingleNode("//fakenamespace:Object1", nsmgr)

This bug is probably very closely related to 324996

BUG: XmlNamespaceManager Does Not Correctly Atomize Strings During Namespace Lookups

However it isn't quite the same bug and if they fix 324996 they may not fix this one. If anyone that has MSDN Universal or some other means of logging a bug with Microsoft wants to be the first to log a new one, go ahead. I neither know how, nor have any premium support from Microsoft.

Note that one of the reasons this may not be a trivial bug is that Visual Studio itself uses default namespaces. An XSD file that is used for a dataset leads to an XML data file with a default namespace.  So XPath's searches don't work on XMLDataDocuments.

Wray Smallwood

Nevermind.. figured it out..

Create a fake namespace alias in the XmlNamespaceManager and prefix you node in the
XPath with it..

XmlNamespaceManager manager = new XmlNamespaceManager();
manager.AddNamespace("test", "http://www.w3.org/2000/09/xmldsig#");

XmlNode n = doc.SelectSingleNode("//test:Signature");

=)

Signature

ANDREAS HÅKANSSON
STUDENT OF SOFTWARE ENGINEERING
andreas (at) selfinflicted.org
"Andreas Håkansson" <andreas@selfinflicted.org> wrote in message news:OKUHludiDHA.1368@TK2MSFTNGP12.phx.gbl...
I have a price of XML that looks like this

<Root>
   <SomeNode>
       .....
   </SomeNode>
   <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
       ...
   </Signature>
</Root>

If I load this into an XmlDocument object and try calling
SelectSingleNode("//Signature") to
determine if the signature has been added or not, then it alwasy return
null. I have managed
to get it that I probably need a XmlNamespaceManager object and pass it to
the method
as well, however I do not know how to set this up since the new namespace
declared in
the <Signature> node doesn't use a prefix and I can not alter the XML to
setup a prefix
either since the <Signature> section is generated by some other code and
need to comply
with a W3C schema =)

Any suggestions on how to be able to tell if the <Signature> node it present
or not, using
XPath and a XmlNamespaceManager object ? =)

--
ANDREAS HÅKANSSON
STUDENT OF SOFTWARE ENGINEERING
andreas (at) selfinflicted.org

Oleg Tkachenko - 09 Oct 2003 09:49 GMT
> Thanks, I was looking for a workaround as well as I could not get mine
> to work. However despite the fact that what you have come up with below
> works, in my opinion this is a blatant bug.
Not really, that's the fact of the reality :) That's how XPath 1.0 was
designed - it doesn't support default namespace. Any non-prefixed name in
XPath data model is treated as name in no namespace. Full stop.

> To be specific if you have a DefaultNamespace you have to fudge an XPath
> search to make it work:
"To fudge"? :) No, but one has to consider abovementioned fact and read more
about XPath. "Object1" in XPath means Object1 element in *null namespace*. See
http://www.w3.org/TR/xpath#node-tests
"This is the same way expansion is done for element type names in start and
end-tags except that the default namespace declared with xmlns is not used: if
the QName does not have a prefix, then the namespace URI is null (this is the
same way attribute names are expanded)."

> nod = xdd.SelectSingleNode("//Object1", nsmgr)
>
> The node is not found despite the fact that the documentation for .NET
> says that it will.
I don't believe it does. Moreover, XmlNode.SelectSingleNode Method (String)
documentation in MSDN says:
"Note   If the XPath expression does not include a prefix, it is assumed that
the namespace URI is the empty namespace. If your XML includes a default
namespace, you must still use the XmlNamespaceManager and add a prefix and
namespace URI to it; otherwise, you will not get a selected node."
Signature

Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Dimitre Novatchev - 03 Oct 2003 20:34 GMT
You do not need to alter the source.xml in order to be able to use a
prefix -- what counts is the namespace-uri and not the prefix.

=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL

> I have a price of XML that looks like this
>
[quoted text clipped - 24 lines]
> or not, using
> XPath and a XmlNamespaceManager object ? =)

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.