473,320 Members | 1,580 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

XPath expression

Hello,

I'm in trouble with a simple XPath expression. I've got the following
XML file

<?xml version="1.0" encoding="UTF-8"?>
<a>
<b>
<c>A</c>
</b>
<b>
<c>B</c>
</b>
<b>
<c>C</c>
</b>
<b>
<c>D</c>
<c>E</c>
<c>F</c>
<c>G</c>
</b>
</a>

and I'd like to reference the c-element with content B, i.e. the
second c-element in the file. But when I execute a/b/c[2], I get the c-
element with content E (the fifth one). Can somebody give me a hint in
what is wrong with this expression. In my opinion, at first a/b/c is
evaluated, which give a set of 7 nodes (7 c-element). Out of this set
the second node is taken, which would be the c-element with content B.

Best regards,
Sascha

Oct 29 '07 #1
4 2320
In article <11*********************@o80g2000hse.googlegroups. com>,
piramido <pi******@googlemail.comwrote:
>and I'd like to reference the c-element with content B, i.e. the
second c-element in the file. But when I execute a/b/c[2], I get the c-
element with content E (the fifth one). Can somebody give me a hint in
what is wrong with this expression. In my opinion, at first a/b/c is
evaluated, which give a set of 7 nodes (7 c-element). Out of this set
the second node is taken, which would be the c-element with content B.
That's not how it works. You don't get the second node of a/b/c;
you get the c[2] of a/b. Only the fourth a/b has a c[2], and that's
the one you get.

(a/b/c)[2] will select the right element.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Oct 29 '07 #2
Richard Tobin a écrit :
In article <11*********************@o80g2000hse.googlegroups. com>,
piramido <pi******@googlemail.comwrote:
>and I'd like to reference the c-element with content B, i.e. the
second c-element in the file. But when I execute a/b/c[2], I get the c-
element with content E (the fifth one). Can somebody give me a hint in
what is wrong with this expression. In my opinion, at first a/b/c is
evaluated, which give a set of 7 nodes (7 c-element). Out of this set
the second node is taken, which would be the c-element with content B.

That's not how it works. You don't get the second node of a/b/c;
you get the c[2] of a/b. Only the fourth a/b has a c[2], and that's
the one you get.
and a/b/c[1] will give you the 4 c elements that contain A B C D : you
evaluate a step upon the result of the previous one ; but as Richard
shows you, () are used for grouping
>
(a/b/c)[2] will select the right element.

-- Richard

--
Cordialement,

///
(. .)
--------ooO--(_)--Ooo--------
| Philippe Poulard |
-----------------------------
http://reflex.gforge.inria.fr/
Have the RefleX !
Oct 29 '07 #3
Have a look at Xml Studio, it has a free XPath visualiser.

http://www.liquid-technologies.com/P...nShots.aspx?11
On 29 Oct, 02:01, piramido <piram...@googlemail.comwrote:
Hello,

I'm in trouble with a simple XPath expression. I've got the following
XML file

<?xml version="1.0" encoding="UTF-8"?>
<a>
<b>
<c>A</c>
</b>
<b>
<c>B</c>
</b>
<b>
<c>C</c>
</b>
<b>
<c>D</c>
<c>E</c>
<c>F</c>
<c>G</c>
</b>
</a>

and I'd like to reference the c-element with content B, i.e. the
second c-element in the file. But when I execute a/b/c[2], I get the c-
element with content E (the fifth one). Can somebody give me a hint in
what is wrong with this expression. In my opinion, at first a/b/c is
evaluated, which give a set of 7 nodes (7 c-element). Out of this set
the second node is taken, which would be the c-element with content B.

Best regards,
Sascha

Oct 29 '07 #4
Hello Richard,

it works. Thank you so much!

Kind regards,
Sascha

Oct 29 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: inquirydog | last post by:
Hi- One frusterating thing for me with xsl is that I don't know how to make xslt throw some sort of exception when a value-of path does not exist. For instance, suppose I have the following...
3
by: gimme_this_gimme_that | last post by:
I once downloaded a shareware program that allowed you to open an xml file, click on a text or an attribute, an then see the xpath expression that would fetch that data. The program didn't...
7
by: steve bull | last post by:
I have the following code snippet to read the colorRange attributes for the colorRangeSwatch in the xml file listed below. string expr = "/swatches/colorRangeSwatch/colorRange";...
2
by: ree32 | last post by:
When I import an xml document in Visual studio and Genereate as schema from it, and create a dataset from it, it adds this line into to the root element of my xml file -...
5
by: laks | last post by:
Hi I have the following xsl stmt. <xsl:for-each select="JOB_POSTINGS/JOB_POSTING \"> <xsl:sort select="JOB_TITLE" order="ascending"/> This works fine when I use it. But when using multiple...
5
by: David Thielen | last post by:
Hi; I set up my xml as follows: XmlDocument xml = new XmlDocument(); xml.Load(File.Open("data.xml", FileMode.Open, FileAccess.Read)); XmlNamespaceManager context = new...
6
by: Chua Wen Ching | last post by:
Hi there, I had this xml file with me (not yet consider implementing xml namespaces yet). <?xml version='1.0'?> <Object> <Windows> <EID>1</EID> <EDesc>Error 1</EDesc> </Windows>
4
by: syed.akhlaq | last post by:
Hi, Does anyone know how can I validate XPath expressions using xsd schema? Thanks
2
by: arunairs | last post by:
Hi, Is there a way of validating and XPath? In this case , I am accepting an XPath from the user and I need to validate the XPath syntax. Is there a regular expression available that anyone can...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.