471,610 Members | 1,336 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,610 software developers and data experts.

Select Nodes

I am using the XmlDocument Select Nodes method and it seems to work OK
except that it returns embedded nodes in the collection when I only want the
nodes that are not embedded. For example I have a statement as follows:

nodes = doc.SelectNodes(\\Node1\\Node\\DesiredNode);

It gives me back in the collection all the DesiredNode tags. If I select
retrieve the nth node as follows

node = nodes[n-1];

It gives me the correct node and all DesiredNodes that are embedded within
the nth node. So if I ask for the first node it gives me the first node
which includes about 5 embedded DesiredNodes. When I ask for the second
DesiredNode node it gives me the first embedded node within the first
DesiredNode. I would like to retrieve a DesireNode nodes collection which
only retrieves nodes which are not embedded. Embedded DesiredNodes cannot
be retrieved directly and can only be seen as embedded within the
non-embedded nodes. How do I do that? I can't seem to find that in the
XPath or XmlDoocument documentation. Please advise. Thanks for your help.

Best Regards,

Paul J. Lay
Nov 12 '05 #1
6 1635
Hi Paul,

In an XPath query, we need to seperate the nodes using slash '/' and with
namespace prefixes.

nodes = doc.SelectNodes("/ns1:Node1/ns2:Node/ns3:DesiredNode);

If that still doesn't work, would you please post your code and sample xml
document here, so that I can try to reproduce this on my machine?

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

Nov 12 '05 #2


Paul J Lay wrote:
I am using the XmlDocument Select Nodes method and it seems to work OK
except that it returns embedded nodes in the collection when I only want the
nodes that are not embedded. For example I have a statement as follows:

nodes = doc.SelectNodes(\\Node1\\Node\\DesiredNode);


What is that? It is not even syntactically correct XPath. It might be
that you are using // instead of the \\ you have above and in that case
// selects all descendants while you might only need the children which
can be done in XPath using /DesiredNode instead of //DesiredNode.
If that doesn't help could you post an example XML document and then
describe which elements or nodes you want to select with XPath?

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 12 '05 #3
Yes you are correct. I am selecting
//HsemServiceResponse//ServiceResponse//HSEMDomain//ManagedEntity. I get
back a collection of more than 500 nodes. When I retrieve the first node
(node = nodes[0]) I get back the first ManagedEntity node and it includes
8 embedded ManagedEntity nodes. When I retrieve the second node I get back
the 1st embedded ManagedEntity node within the first ManagedEntity node. I
want to receive a collection of only the ManagedEntity nodes which are not
embedded. Embedded nodes can be seen only by retrieving the parent node in
which they are embedded. I believe this is possible if you know the correct
XPATH syntax. Thanks for your help.

Best Regards,

Paul J Lay

"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:O8**************@TK2MSFTNGP12.phx.gbl...


Paul J Lay wrote:
I am using the XmlDocument Select Nodes method and it seems to work OK
except that it returns embedded nodes in the collection when I only want
the nodes that are not embedded. For example I have a statement as
follows:

nodes = doc.SelectNodes(\\Node1\\Node\\DesiredNode);


What is that? It is not even syntactically correct XPath. It might be that
you are using // instead of the \\ you have above and in that case //
selects all descendants while you might only need the children which can
be done in XPath using /DesiredNode instead of //DesiredNode.
If that doesn't help could you post an example XML document and then
describe which elements or nodes you want to select with XPath?

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Nov 12 '05 #4


Paul J Lay wrote:
I am selecting
//HsemServiceResponse//ServiceResponse//HSEMDomain//ManagedEntity. I get
back a collection of more than 500 nodes. When I retrieve the first node
(node = nodes[0]) I get back the first ManagedEntity node and it includes
8 embedded ManagedEntity nodes. When I retrieve the second node I get back
the 1st embedded ManagedEntity node within the first ManagedEntity node. I
want to receive a collection of only the ManagedEntity nodes which are not
embedded. Embedded nodes can be seen only by retrieving the parent node in
which they are embedded. I believe this is possible if you know the correct
XPATH syntax.


As I said, in XPath // selects descendants so I guess you want / instead
e.g.
/HsemServiceResponse/ServiceResponse/HSEMDomain/ManagedEntity
or at least
//HsemServiceResponse//ServiceResponse//HSEMDomain/ManagedEntity
that way you get only <ManagedEntity> elements which are child nodes of
<HSEMDomain> elements.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 12 '05 #5
That worked! Thanks so much for your prompt reply. Do you know where there
is a good reference on XPath syntax? I have been trying to find one. All I
have found so far are incomplete snippets. Thanks again.

Best Regards,

Paul J Lay

"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:Oi*************@TK2MSFTNGP14.phx.gbl...


Paul J Lay wrote:
I am selecting
//HsemServiceResponse//ServiceResponse//HSEMDomain//ManagedEntity. I get
back a collection of more than 500 nodes. When I retrieve the first node
(node = nodes[0]) I get back the first ManagedEntity node and it
includes 8 embedded ManagedEntity nodes. When I retrieve the second node
I get back the 1st embedded ManagedEntity node within the first
ManagedEntity node. I want to receive a collection of only the
ManagedEntity nodes which are not embedded. Embedded nodes can be seen
only by retrieving the parent node in which they are embedded. I believe
this is possible if you know the correct XPATH syntax.


As I said, in XPath // selects descendants so I guess you want / instead
e.g.
/HsemServiceResponse/ServiceResponse/HSEMDomain/ManagedEntity
or at least
//HsemServiceResponse//ServiceResponse//HSEMDomain/ManagedEntity
that way you get only <ManagedEntity> elements which are child nodes of
<HSEMDomain> elements.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Nov 12 '05 #6
Hi Paul,

You can check the w3c xpath specifications.

http://www.w3.org/TR/xpath

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

Nov 12 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Michael | last post: by
3 posts views Thread by someone | last post: by
4 posts views Thread by G Uljee | last post: by
13 posts views Thread by André Nogueira | last post: by
1 post views Thread by Christopher DeMarco | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by MichaelMortimer | last post: by
reply views Thread by CCCYYYY | last post: by

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.