473,772 Members | 2,478 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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>
<FieldDefinitio n>a</FieldDefinition >
<FieldDefinitio n>b</FieldDefinition >
<FieldDefinitio n>c</FieldDefinition >
<FieldDefinitio n>d</FieldDefinition >
<FieldDefinitio n>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 1180
/*/*[.='c']
Cheers,
Dimitre Novatchev


"Steve" <sm*******@news group.nospam> wrote in message
news:63******** *************** ***********@mic rosoft.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>
<FieldDefinitio n>a</FieldDefinition >
<FieldDefinitio n>b</FieldDefinition >
<FieldDefinitio n>c</FieldDefinition >
<FieldDefinitio n>d</FieldDefinition >
<FieldDefinitio n>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>
<FieldDefinitio n>a</FieldDefinition >
<FieldDefinitio n>b</FieldDefinition >
<FieldDefinitio n>c</FieldDefinition >
<FieldDefinitio n>d</FieldDefinition >
<FieldDefinitio n>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.c om.au> wrote in message
news:43******** *************** @authen.yellow. readfreenews.ne t...
/*/*[.='c']
Cheers,
Dimitre Novatchev


"Steve" <sm*******@news group.nospam> wrote in message
news:63******** *************** ***********@mic rosoft.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>
<FieldDefinitio n>a</FieldDefinition >
<FieldDefinitio n>b</FieldDefinition >
<FieldDefinitio n>c</FieldDefinition >
<FieldDefinitio n>d</FieldDefinition >
<FieldDefinitio n>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
6825
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 elements, attributes, ".", and "..", plus also the "" predicate format is supported - however, only one predicate per path step is supported, and expr must be a relative path. 2. Poor performance
3
501
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 contains the value of the substring in XSLT? In this case the firstchild of the first "c" element. I'm doing this in Java, and my clumsy solution was to take out the string "/line/c" and find the firstchild of the returned node. I need to split up the...
7
1811
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"; XmlElement crsElement = (XmlElement)m_colorRangeSwatchDoc.SelectSingleNode(expr); bool fr = bool.Parse(crsElement.GetAttribute("fixed").ToString()); The element returned is always the 1st, All Blue Colors, why doesn't the expression...
2
2882
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 - "xmlns="http://tempuri.org/nameOfRoot.xsd" I have no idea what its pointing to & what is tempuri.org? So when this tag is in my xml tag my xpath query never works. But when I delete it work fine.
2
7753
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 with a DateTime string, but that doesn't work. I've assumed that it is easier to manipulate today's date instead of the date in my ExpireDate element. Thankyou in advanced for taking the time to read this and possibly helping me
5
2408
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 XmlNamespaceManager(xml.NameTable); context.AddNamespace("", "http://www.test.org"); context.AddNamespace("sns", "http://www.test.org/sub"); XmlNode node = xml.SelectSingleNode("/root", context);
5
7932
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
5956
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? Thanks in advice!
3
3090
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 Potter</book> </books> </top>
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10264
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10039
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8937
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7461
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5355
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3610
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2851
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.