Solved

What is the correct XPath to select an element

Posted on 2004-10-18
7
233 Views
Last Modified: 2010-04-15
I know the path for selecting on an attribute is

/Node1/Node2[@attrib='new attrib']

what is the equivelent if I want to select on attrib when attrib is an element as in the following case:

<Node1>
<Node2>
<attrib>1234</attrib>
<attrib>5677</attrib>
</Node2>
</Node1>
0
Comment
Question by:jjacksn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 14

Expert Comment

by:AvonWyss
ID: 12345274
The XPath you posted does NOT select an attribute, but a Node2 element which is inside a Node1 element and which has an attribute named attrib with the given value.

/Node1/Node2/attrib[.='new attrib']

may be what you are looking for.
0
 
LVL 3

Expert Comment

by:Uritsukidoji
ID: 12345787
almost the same I think:

Node1/Node2[./attrib='new attrib']
0
 
LVL 14

Accepted Solution

by:
AvonWyss earned 300 total points
ID: 12346864
No, that's not the same. The part in the [] gives a condition, whereas the part before it selects the node. This means that

/Node1/Node2/attrib[.='new attrib']
selects an element named "attrib" which has the value (inner text) of "new attrib" and which is contained in a Node2, which in turn is contained in a Node1.

Node1/Node2[./attrib='new attrib'] (same as Node1/Node2[attrib='new attrib'])
selects a Node2 which has a subnode called "attrib" with the value "new attrib" and which is contained in a Node1.

As you can see, that is NOT the same. Not even almost.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Assisted Solution

by:Uritsukidoji
Uritsukidoji earned 200 total points
ID: 12347115
Sorry I didn't mean to offend you mr. AvonWyss...

I'll make myself more clear, I wasn't replying to you, but replying to jjacksn.

In his example he DID want to select 'Node2' where 'Node2' has an attribute value of 'new attrib'.
selecting Node2 containing an element with the value 'new attrib', is almost the same syntax, except for leaving the @ out.
 
I think jjacksn didn't want to select the attrib element, but the Node2 containing the attrib with value 'new attrib'

Summary:

"/Node1/Node2[@attrib='new attrib']"
selects Node2 containing an attribute value 'new attrib'

"/Node1/Node2[attrib='new attrib']"
selects Node2 containing an element value 'new attrib'.

Or to do it in a more qualified form:
"/Node1/Node2[./attrib/text()='new attrib']"
selects Node2 containing an element value 'new attrib'.

Good luck, Wiebe
0
 
LVL 5

Author Comment

by:jjacksn
ID: 12364338
thanks to both of you, that cleared things up.

what other things can be used besides text()?
0
 
LVL 5

Author Comment

by:jjacksn
ID: 12364344
Incidentally, AvonWyss, i misstated the question and you answer was correct for my questio, but Wiebe's intuition about what I meant to ask was also correct.  
0
 
LVL 3

Expert Comment

by:Uritsukidoji
ID: 12367237
Hi jjacksn,

see the XPath reference on MSDN:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/xmlsdk/html/_xpath_reference.asp

you can also use functions like comment() and processing-instruction()

thx, Wiebe
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question