473,386 Members | 2,050 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,386 software developers and data experts.

xpath expression to find which element

Hello.. I have been trying to figure this out for the last hour, but I am
baffled... I'm hoping someone can help!

I'm trying to write an xpath expression to find the position of the node
with value "c" in the following xml. The result I'm looking for in this case
should be 3. Does anyone have any ideas?

<?xml version="1.0" encoding="UTF-8"?>
<TopNode>
<FieldDefinition>a</FieldDefinition>
<FieldDefinition>b</FieldDefinition>
<FieldDefinition>c</FieldDefinition>
<FieldDefinition>d</FieldDefinition>
<FieldDefinition>e</FieldDefinition>
</TopNode>

My idea was something like:

/*/position(". = 'c'")

But that doesn't work.

Could anyone please help! Thank you very much in advance.
--
Steve
Feb 18 '06 #1
3 1167
/*/*[.='c']
Cheers,
Dimitre Novatchev


"Steve" <sm*******@newsgroup.nospam> wrote in message
news:63**********************************@microsof t.com...
Hello.. I have been trying to figure this out for the last hour, but I am
baffled... I'm hoping someone can help!

I'm trying to write an xpath expression to find the position of the node
with value "c" in the following xml. The result I'm looking for in this
case
should be 3. Does anyone have any ideas?

<?xml version="1.0" encoding="UTF-8"?>
<TopNode>
<FieldDefinition>a</FieldDefinition>
<FieldDefinition>b</FieldDefinition>
<FieldDefinition>c</FieldDefinition>
<FieldDefinition>d</FieldDefinition>
<FieldDefinition>e</FieldDefinition>
</TopNode>

My idea was something like:

/*/position(". = 'c'")

But that doesn't work.

Could anyone please help! Thank you very much in advance.
--
Steve

Feb 18 '06 #2
Steve wrote:
Hello.. I have been trying to figure this out for the last hour, but I am
baffled... I'm hoping someone can help!

I'm trying to write an xpath expression to find the position of the node
with value "c" in the following xml. The result I'm looking for in this case
should be 3. Does anyone have any ideas?

<?xml version="1.0" encoding="UTF-8"?>
<TopNode>
<FieldDefinition>a</FieldDefinition>
<FieldDefinition>b</FieldDefinition>
<FieldDefinition>c</FieldDefinition>
<FieldDefinition>d</FieldDefinition>
<FieldDefinition>e</FieldDefinition>
</TopNode>

My idea was something like:

/*/position(". = 'c'")

But that doesn't work.

Could anyone please help! Thank you very much in advance.


position() won't help you here because it's a null function (no
arguments).

<xsl:value-of select="count(/*/*[.='c']/preceding-sibling::*)+1"/>

This will work provided you know that no other elements types apart from
FieldDefinition are permitted in the content model of TopNode. If other
elements types are allowed, then you will have to qualify the element
nodes located by the preceding-sibling axis.

///Peter
Feb 18 '06 #3
Sorry, was misled by the subject.

Peter Flyn has provided an answer.

Another possibility is to use <xsl:number> , depending on what you mean
by "the position of the element"

Cheers,
Dimitre Novatchev
"Dimitre Novatchev" <di******@tpg.com.au> wrote in message
news:43***********************@authen.yellow.readf reenews.net...
/*/*[.='c']
Cheers,
Dimitre Novatchev


"Steve" <sm*******@newsgroup.nospam> wrote in message
news:63**********************************@microsof t.com...
Hello.. I have been trying to figure this out for the last hour, but I am
baffled... I'm hoping someone can help!

I'm trying to write an xpath expression to find the position of the node
with value "c" in the following xml. The result I'm looking for in this
case
should be 3. Does anyone have any ideas?

<?xml version="1.0" encoding="UTF-8"?>
<TopNode>
<FieldDefinition>a</FieldDefinition>
<FieldDefinition>b</FieldDefinition>
<FieldDefinition>c</FieldDefinition>
<FieldDefinition>d</FieldDefinition>
<FieldDefinition>e</FieldDefinition>
</TopNode>

My idea was something like:

/*/position(". = 'c'")

But that doesn't work.

Could anyone please help! Thank you very much in advance.
--
Steve


Feb 19 '06 #4

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

Similar topics

1
by: bdinmstig | last post by:
I refined my attempt a little further, and the following code does seem to work, however it has 2 major problems: 1. Very limited support for XPath features Basic paths are supported for...
3
by: ShakyMobo | last post by:
<line><c n="H7">Drinkin' all </c><c n="E">night</c><c n="E7">We got in to a </c><c n="A">fight</c></line> XPath: substring(/line/c, 3, 7) Is it possible to refer to the textelement that...
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 -...
2
by: Echo 8 | last post by:
Is there a simple way of compare dates? I am trying to create a list of perishable stock in an inventory that is near or past its expiration date. I've tried telling it to compare a node's value...
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...
5
by: Gnic | last post by:
Hi , I have an XmlDocument instance, I want to find a node in the xml, but I don't know it's path until runtime, for example <aaa> <bbb name="x"/> <aaa attr="y"> <ccc>sometext</ccc> </aaa>
2
by: linuxadmin | last post by:
Hello everyone! I evaluate XPath expressions using XPathNavigator and need to know for each result, which line number (or et least file position) it has inside the XML file. Any ideas? ...
3
by: Arndt Jonasson | last post by:
Let's say we have a schema (maybe expressed in XML Schema, but not necessarily so), that allows this instance document: <top> <txt>This is text</txt> <books> <book>Tarzan</book> <book>Harry...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.