473,406 Members | 2,710 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,406 software developers and data experts.

Xpath Search with Namespaces

I've run into a problem trying to search an XML document with a
namespace. Sample extract is below:

<Vendors xmlns="http://tempuri.org/Vendor.xsd">
<Vendor>
<VendorID>002010</VendorID>
<PropricerId>002010S</PropricerId>
<VendorName>AEROTEK, INC.</VendorName>
</Vendor>
<Vendor>
<VendorID>002012</VendorID>
<PropricerId>002012S</PropricerId>
<VendorName>ARONSON &amp; COMPANY</VendorName>
</Vendor>
<Vendor>
<VendorID>002018</VendorID>
<PropricerId>002018S</PropricerId>
<VendorName>ASSOCIATED SPRING RAYMOND</VendorName>
</Vendor>
I'm trying to get a VendorName based on a vendor ID. I'm using
Microsoft .Net framework XML parser.

I've tried numerous variations on the following:

..SelectSingleNode("//default:Vendor/default:VendorName[VendorID='008300'],
<<Variable indicating namespace>>)

TIA,

Michael Schnell

Nov 12 '05 #1
2 1387
try the following:

..SelectSingleNode("a:Vendors/a:Vendor/a:VendorName[../a:VendorID='008300']")
where "a" maps to your namespace.

When you are using the predicate on VendorName, the context at that point is
inside the element VendorName, so you need to use the parent axis to access
VendorID like "../a:VendorID"

hope this helps,
swapna

"NO************@rdacorp.NOSPAM.com" <sc*****@rdacorp.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
I've run into a problem trying to search an XML document with a
namespace. Sample extract is below:

<Vendors xmlns="http://tempuri.org/Vendor.xsd">
<Vendor>
<VendorID>002010</VendorID>
<PropricerId>002010S</PropricerId>
<VendorName>AEROTEK, INC.</VendorName>
</Vendor>
<Vendor>
<VendorID>002012</VendorID>
<PropricerId>002012S</PropricerId>
<VendorName>ARONSON &amp; COMPANY</VendorName>
</Vendor>
<Vendor>
<VendorID>002018</VendorID>
<PropricerId>002018S</PropricerId>
<VendorName>ASSOCIATED SPRING RAYMOND</VendorName>
</Vendor>
I'm trying to get a VendorName based on a vendor ID. I'm using
Microsoft .Net framework XML parser.

I've tried numerous variations on the following:

.SelectSingleNode("//default:Vendor/default:VendorName[VendorID='008300'],
<<Variable indicating namespace>>)

TIA,

Michael Schnell

Nov 12 '05 #2
Hi Michael,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to get the VendorName by
VendorID in an XmlDocument. If there is any misunderstanding, please feel
free to let me know.

According to the document you have provided, we cannot get the VendorName
directly using SelectSingleNode. Because the VendorID and VendorName is in
the same level of the document. However, we can get the parent Vendor node
with SelectSingleNode. Here is the code.

XmlDocument doc = new XmlDocument();
doc.LoadXml(XmlText);
XmlNamespaceManager nsm = new XmlNamespaceManager(doc.NameTable);
nsm.AddNamespace("default", "http://tempuri.org/Vendor.xsd");

XmlNode n =
doc.SelectSingleNode("//default:Vendor[default:VendorID='008300']", nsm);

Node n is the <Vendor> node. You can get the child <VendorName> element to
get the name.

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 12 '05 #3

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: Alexander Gräf | last post by:
Hello, I'm stuck with a simple problem, for which I don't have a solution. I basically have an XML file containing fragments of plain text and html, in several languages: <?xml version="1.0"...
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>
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: 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>...
6
by: AMDRIT | last post by:
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...
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: 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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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.