Solved

What is the correct XPath to select an element

Posted on 2004-10-18
7
221 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now