473,320 Members | 1,982 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,320 software developers and data experts.

XPath and namespace problem

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
Nov 11 '05 #1
4 5817
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");

=)
--
ANDREAS HÅKANSSON
STUDENT OF SOFTWARE ENGINEERING
andreas (at) selfinflicted.org
"Andreas Håkansson" <an*****@selfinflicted.org> wrote in message news:OK**************@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
Nov 11 '05 #2
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

"Andreas Håkansson" <an*****@selfinflicted.org> wrote in message
news:OK**************@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

Nov 11 '05 #3
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



"Andreas Håkansson" <an*****@selfinflicted.org> wrote in message news:uk**************@TK2MSFTNGP11.phx.gbl...
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");

=)
--
ANDREAS HÅKANSSON
STUDENT OF SOFTWARE ENGINEERING
andreas (at) selfinflicted.org
"Andreas Håkansson" <an*****@selfinflicted.org> wrote in message news:OK**************@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
Nov 11 '05 #4
Wray Smallwood wrote:
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."
--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Nov 11 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Chris | last post by:
Hi all, I have recently started working with the new XML functionality in PHP5, but I am running into a few problems. Specifically, I am using an Xpath query to try and pull out the data in...
1
by: Robert | last post by:
I am having a problem selecting nodes using the XMLnodelist Selectnodes using XPATH when I use XML SPY is successfully queries but when is use VB.net it comes up with nothing. Here is my code ...
2
by: ree32 | last post by:
When I import an xml document in Visual studio and Genereate as schema from it, and create a dataset from it, it adds this line into to the root element of my xml file -...
18
by: jacksu | last post by:
I have a simple program to run xpath with xerces 1_2_7 XPathFactory factory = XPathFactory.newInstance(); XPath xPath = factory.newXPath(); XPathExpression xp = xPath.compile(strXpr);...
3
by: Jason Mobarak | last post by:
Hello -- I'm attempting to get a handle on how to do xpath queries with System.Xml -- so far the biggest hurdle has been how to deal with a default namespace. If I use the test xml: <?xml...
1
by: sobczyk.wojciech | last post by:
I have XML: <xml xmlns="http://abc"> <A t="ttt"></A> <B xmlns="http://qwerty"><X>aaaa</X></B> </xml> How to point node "B" with XPath without any changes in this xml?
6
by: J.Marsch | last post by:
I must be completely losing my mind. I have some code that writes to config files. It works great with app.config files, but fails miserably with web.config files. For the life of me, I cannot...
14
by: Mikhail Teterin | last post by:
Hello! What's would be the syntax for a query, which would allow me to get only the elements with non-empty text-nodes? For example, from: <a><b></b></a> <c/> <d><e>meow</e></d>
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
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.