Solved

What is the correct XPath to select an element

Posted on 2004-10-18
7
230 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
Industry Leaders: 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

732 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