This is Interesting: Free Magazines for Graphics designers and webmasters
Home > Archive > Microsoft XML > December 2005 > Finding the position of a value in a nodelist
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Finding the position of a value in a nodelist
|
|
| Dave F. 2005-12-29, 10:30 pm |
| Hi
I'm new to the joys of XML.
I have an XML file:
<?xml version="1.0"?>
<Offset>
<x>0.2</x>
<x>1</x>
<x>2</x>
<x>3</x>
<x>4</x>
<x>4.5</x>
<x>5</x>
<x>7</x>
<x>7.1</x>
</Offset>
Given the value of 4.5, what's the best way to find it's position in the
childnodes?
I know I can get the NodeList:
Set NodeLst = MsDom.selectNodes("/Offset/x/text()")
& iterate it using the item method, but i was wondering if I there was a
quicker/easier way.
I can get the value at position 3 etc. using position(), but is there an
option to do the opposite?
Thanks in advance
Dave F.
| |
| Martin Honnen 2005-12-30, 6:37 pm |
|
Dave F. wrote:
> <Offset>
> <x>0.2</x>
> <x>1</x>
> <x>2</x>
> <x>3</x>
> <x>4</x>
> <x>4.5</x>
> <x>5</x>
> <x>7</x>
> <x>7.1</x>
> </Offset>
>
> Given the value of 4.5, what's the best way to find it's position in the
> childnodes?
It depends on what you regard as child nodes, if you are looking for all
element child nodes then you can use the XPath expression
/Offset/x[. = "4.5"][1]/preceding-sibling::*
and for instance do a selectNodes on that, take the length of the
returned node list and add 1 to find the position e.g. with JScript
var position = xmlDocument.selectNodes(
'/Offset/x[. = "4.5"][1]/preceding-sibling::*').length + 1;
If you wanted to consider all kind of child nodes (element, text,
comment etc.) then you need the XPath expression
/Offset/x[. = "4.5"][1]/preceding-sibling::node()
and do the same as above.
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
| |
| Janwillem Borleffs 2005-12-30, 6:37 pm |
| Dave F. wrote:
> Given the value of 4.5, what's the best way to find it's position in
> the childnodes?
>
> I know I can get the NodeList:
> Set NodeLst = MsDom.selectNodes("/Offset/x/text()")
> & iterate it using the item method, but i was wondering if I there
> was a quicker/easier way.
>
Try something like:
NodePosition = MsDom.selectNodes("count(x[.='4.5']/preceding-sibling::*) +
1")
JW
|
|
|
| | Copyright 2003 - 2008 forum4designers.com Software forum Computer Hardware reviews |
|