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
Solved

What is the correct XPath to select an element

Posted on 2004-10-18
7
228 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
  • 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

 
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
PrintingFoundIt(Almost!(Damn!)) 1 30
Cant save 3D 4 22
Moving code from one TFS branch to another 3 33
Get distinct values from excel or comm seperated file 4 30
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
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…

790 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