XPath statement to assign an integer variable to the position() function in a powershell script -


i working on powershell script takes input xml file, searches inner text associated specific element/tag in xml file, , returns position of element/tag, if exists, position of element can used replace inner text other data. example, there may xml file looks following . . .

<root>     <category>         <fruit>apple</fruit>         <vegetable>broccoli</vegetable>         <fruit>pear</fruit>         <vegetable>brussel sprouts</vegetable>         <fruit>orange</fruit>     </category> </root> 

so, let's in 1 part of powershell script have code find inner text, "orange". store in variable "position" integer of "fruit" element contains "orange" it's inner text. so, in above xml file, position integer 3 (or 2 if starting @ base zero). how write proper xpath statement access 3rd "fruit" location through variable? maybe want access location can change "orange" "banana" or something. have tried following no success . . .

$orangeposition = ??? (i assigned value of 3 testing purposes)

root.selectsinglenode("descendant::category/fruit[position()=$orangeposition]")

i have seen "position() assigned integer value. want use variable in place of integer because don't know index in particular file inner text "orange" may be. possible? i've seen other posts question similar issues, none of them seem work when apply solution (if there 1 stated) powershell script). ideas on how can resolved?

you use root.selectnodes("//category/fruit[. = 'orange']/preceding-sibling::fruit).count + 1. if want xpath alone use evaluate method e.g. root.createnavigator().evaluate("count(//category/fruit[. = 'orange']/preceding-sibling::fruit) + 1"). should return c# double value.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -