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

Xpath with namespaces

Hello Everyone,

I am having an issue with xml and namespaces, at least I think it is
namespaces. When I use namespaces, I cannot use SelectSingleNode /
SelectNodes as they always return 0/Nothing respectively. Anyone have any
thoughts?

Thanks in advance

VB.Net 2005
WinXP Sp2

Test code:

Dim objXML As System.Xml.XmlDocument

'Test XML 1 ==Always returns 0
objXML = New System.Xml.XmlDocument
objXML.Load(Application.StartupPath & "\xmlfile1.xml")
MsgBox(objXML.SelectNodes("top/middle").Count)

'Test XML 2 ==Always returns expected results
objXML = New System.Xml.XmlDocument
objXML.Load(Application.StartupPath & "\xmlfile2.xml")
MsgBox(objXML.SelectNodes("top/middle").Count)

XML File 1
<?xml version="1.0" encoding="utf-8" ?>
<top xmlns="urn:bob" xmlns:firstns="urn:firstns"
xmlns:secondns="urn:secondns">
<middle firstns:name="Entry1" secondns:value="Value1">Some Data
Here</middle>
</top>
XML File 2
<?xml version="1.0" encoding="utf-8" ?>
<top>
<middle name="Entry1" value="Value1">Some Data Here</middle>
</top>
Oct 11 '06 #1
6 2978
* AMDRIT wrote in microsoft.public.dotnet.xml:
>I am having an issue with xml and namespaces, at least I think it is
namespaces. When I use namespaces, I cannot use SelectSingleNode /
SelectNodes as they always return 0/Nothing respectively. Anyone have any
thoughts?
You have to declare your namespaces first using e.g. a custom
XmlNamespaceManager or you have to rewrite your expressions to
avoid them like

//foo:bar -//*[local-name() = 'bar' namespace-uri() = '...']
--
Björn Höhrmann · mailto:bj****@hoehrmann.de · http://bjoern.hoehrmann.de
Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/
Oct 11 '06 #2
I appreciate your response, however it is leading me nowhere. I have
discovered XMLNamespaceManager prior to posting, the docs are not clear how
this resolved my issue.

First, I am not attempting to get to the attributes yet, merely attempting
to count the number of 'middle' nodes in 'top'. So I do not see how I would
change my xpath statement to reflect namespaces.

Second, when I create a document from scratch using these namespaces and
attempt to xpath through the document I am not having any issues. Any ideas
why?

Sample Code to Count Nodes (XML Loaded from File)

Dim objXML As System.Xml.XmlDocument
Dim objNSMGR As Xml.XmlNamespaceManager

objXML = New System.Xml.XmlDocument
objXML.Load(Application.StartupPath & "\xmlfile1.xml")

objNSMGR = New System.Xml.XmlNamespaceManager(objXML.NameTable)

objNSMGR.AddNamespace("firstns", "urn:firstns")
objNSMGR.AddNamespace("secondns", "urn:secondns")

MsgBox(objXML.SelectNodes("top/middle", objNSMGR).Count)

Sample Code to create an query XML

Dim objXML As System.Xml.XmlDocument
Dim objNode As System.Xml.XmlNode
objXML = New System.Xml.XmlDocument

'Create root node
objNode = objXML.CreateNode(Xml.XmlNodeType.Element, "top", "")

'create namespaces
objNode.Attributes.Append(objXML.CreateAttribute(" xmlns"))
objNode.Attributes.GetNamedItem("xmlns").Value = "urn:bob"

objNode.Attributes.Append(objXML.CreateAttribute(" xmlns:firstns"))
objNode.Attributes.GetNamedItem("xmlns:firstns").V alue = "urn:firstns"

objNode.Attributes.Append(objXML.CreateAttribute(" xmlns:secondns"))
objNode.Attributes.GetNamedItem("xmlns:secondns"). Value = "urn:secondns"

'Append root node to #document
objXML.AppendChild(objNode)

'Create first child node
objNode = objXML.CreateNode(Xml.XmlNodeType.Element, "middle", "")

'Create first attribute with namespace-uri
objNode.Attributes.Append(objXML.CreateAttribute(" firstns:name",
"urn:firstns"))
objNode.Attributes.GetNamedItem("firstns:name").Va lue = "test"

'Append node to root node
objXML.SelectSingleNode("top").AppendChild(objNode )

'Count all occurances of middle in top
MsgBox(objXML.SelectNodes("top/middle").Count)
"Bjoern Hoehrmann" <bj****@hoehrmann.dewrote in message
news:la********************************@hive.bjoer n.hoehrmann.de...
>* AMDRIT wrote in microsoft.public.dotnet.xml:
>>I am having an issue with xml and namespaces, at least I think it is
namespaces. When I use namespaces, I cannot use SelectSingleNode /
SelectNodes as they always return 0/Nothing respectively. Anyone have any
thoughts?

You have to declare your namespaces first using e.g. a custom
XmlNamespaceManager or you have to rewrite your expressions to
avoid them like

//foo:bar -//*[local-name() = 'bar' namespace-uri() = '...']
--
Björn Höhrmann · mailto:bj****@hoehrmann.de · http://bjoern.hoehrmann.de
Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/

Oct 11 '06 #3
* AMDRIT wrote in microsoft.public.dotnet.xml:
>I appreciate your response, however it is leading me nowhere. I have
discovered XMLNamespaceManager prior to posting, the docs are not clear how
this resolved my issue.
Any and all elements and attributes in an XML document have a name-
space name and a local name associated with it, in your example

<?xml version="1.0" encoding="utf-8" ?>
<top xmlns="urn:bob" xmlns:firstns="urn:firstns"
xmlns:secondns="urn:secondns">
<middle firstns:name="Entry1" secondns:value="Value1">Some Data
Here</middle>
</top>

you have

<{urn:bob}top>
<{urn:bob}middle {urn:firstns}name="Entry1"
{urn:secondns}value="Value1">Some Data here
...

Your XPath expression "top/middle" ignores elements in the 'urn:bob'
namespace, it matches middle elements in no namespace that have a parent
top element in no namespace. In your example, the elements are in a
namespace however. You have to specify this namespace in the XPath, like

"foo:top/foo:middle"

and the prefix "foo" needs to be bound to "urn:bob"; XmlNamespace-
Manager does just that.
--
Björn Höhrmann · mailto:bj****@hoehrmann.de · http://bjoern.hoehrmann.de
Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/
Oct 12 '06 #4


AMDRIT wrote:

Dim objXML As System.Xml.XmlDocument
Dim objNSMGR As Xml.XmlNamespaceManager

objXML = New System.Xml.XmlDocument
objXML.Load(Application.StartupPath & "\xmlfile1.xml")

objNSMGR = New System.Xml.XmlNamespaceManager(objXML.NameTable)

objNSMGR.AddNamespace("firstns", "urn:firstns")
objNSMGR.AddNamespace("secondns", "urn:secondns")

MsgBox(objXML.SelectNodes("top/middle", objNSMGR).Count)
You have to use the prefixes in your XPath expressions to qualify
element or attributes and you need a prefix for the default namespace
the XML has e.g.
objNSMGR.AddNamespace("bb", "urn:bob")

objXML.SelectNodes("bb:top/bb:middle", objMSNGR).Count

See <http://www.faqts.com/knowledge_base/view.phtml/aid/34022/fid/616>
for some more explanation.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Oct 12 '06 #5


AMDRIT wrote:

Second, when I create a document from scratch using these namespaces and
attempt to xpath through the document I am not having any issues.
Dim objXML As System.Xml.XmlDocument
Dim objNode As System.Xml.XmlNode
objXML = New System.Xml.XmlDocument

'Create root node
objNode = objXML.CreateNode(Xml.XmlNodeType.Element, "top", "")
Yes, you have issues, your are _not_ creating the element in any
namespace. With the DOM the namespace an element or attribute belongs to
is determined when it is created so your code creates an element named
"top" in no namespace.

If you serialized your XML then you would see that the top element is in
no namespace. If you want to use the DOM to create an element in a
certain namespace then you can do e.g.

XmlElement topElement = objXML.CreateElement("top", "urn:bob")

that is you use a method that takes a namespace URI and you pass in the
namespace URI of the namespace you want the element to belong to.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Oct 12 '06 #6
Thank you for the clear response. I am off and running again.

"Martin Honnen" wrote:
>

AMDRIT wrote:

Dim objXML As System.Xml.XmlDocument
Dim objNSMGR As Xml.XmlNamespaceManager

objXML = New System.Xml.XmlDocument
objXML.Load(Application.StartupPath & "\xmlfile1.xml")

objNSMGR = New System.Xml.XmlNamespaceManager(objXML.NameTable)

objNSMGR.AddNamespace("firstns", "urn:firstns")
objNSMGR.AddNamespace("secondns", "urn:secondns")

MsgBox(objXML.SelectNodes("top/middle", objNSMGR).Count)

You have to use the prefixes in your XPath expressions to qualify
element or attributes and you need a prefix for the default namespace
the XML has e.g.
objNSMGR.AddNamespace("bb", "urn:bob")

objXML.SelectNodes("bb:top/bb:middle", objMSNGR).Count

See <http://www.faqts.com/knowledge_base/view.phtml/aid/34022/fid/616>
for some more explanation.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Oct 12 '06 #7

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

Similar topics

2
by: Piet | last post by:
Hello, Via Xpath, I want to access nodes which have a namespace prefix. THe document at hand is an Xsl-FO document. I tried the following: from xml.dom import minidom from xml.xpath import...
3
by: Colin Green | last post by:
I have come across what seems like a failing in the .Net XML classes. Many people have posted requesting how to write an XPath query with namespace prefixes that works. The solution shown in all...
1
by: Hollywood | last post by:
I have the following XSD created in VS.NET 2003: <?xml version="1.0" encoding="utf-8" ?> <xs:schema id="ReferralSchama" targetNamespace="http://test.org/Referral"...
6
by: Chua Wen Ching | last post by:
Hi there, I had this xml file with me (not yet consider implementing xml namespaces yet). <?xml version='1.0'?> <Object> <Windows> <EID>1</EID> <EDesc>Error 1</EDesc> </Windows>
3
by: Goran Djuranovic | last post by:
Hi All, Does anyone know how to retreive deepest XPath value from XML document by using VB.NET? For example, if I had an XML file like this: <Root> <Customer> <Name>MyName</Name> </Customer>...
14
by: Mat| | last post by:
Hello :-) I am learning XPath, and I am trying to get child nodes of a node whose names do *not* match a given string, e.g : <dummy> <example> <title>Example 1</title> <body>this is an...
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
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...
1
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...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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)...
1
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: 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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.