Solved

What is the correct XPath to select an element

Posted on 2004-10-18
7
224 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

912 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

19 Experts available now in Live!

Get 1:1 Help Now