0

I have code like this:

<List>
    <Header>
        <HeaderItem>ID</HeaderItem>
        <HeaderItem>Number</HeaderItem>
        <HeaderItem>State</HeaderItem>
        <HeaderItem>Name</HeaderItem>
    </Header>
    <ListItem>
        <Text>12345</Text>
        <Text>1</Text>
        <Text>1</Text>
        <Text>Ex1</Text>
    </ListItem>
    <ListItem>
        <Text>67890</Text>
        <Text>2</Text>
        <Text>1</Text>
        <Text>Ex2</Text>
    </ListItem>
</List>

And I want to get the "1" under header State. I have such an XPATH:

//List[1][Header/HeaderItem[@Name="State"]]/ListItem[1]/Text[@Name = "1"]

But it selects both "1" (under Number and State). How to get that one under State?

Noxe
  • 3
  • 1
  • It's not clear what you want so please edit your question. First, try changing all the text numbers in your html so that they all unique. Second, explain which of these unique numbers exactly you want and based on what criterion (as in - it's in the first ListItem, or whatever). – Jack Fleeting Mar 03 '20 at 13:40
  • @JackFleeting This is an example from a list in my app. Im looking for a valid XPATH to select **one** element - that is the number "1" that is under the header "State" in the first ListItem node. The current XPATH in my question returns two elements - "1" under Number and "1" under "State". – Noxe Mar 03 '20 at 13:53
  • @Noxe This `//List//ListItem/Text[3]/text()` xpath will return all columns values under `State` column. – CodeIt Mar 03 '20 at 15:29

1 Answers1

0

This will return you the values under "State" column:

//ListItem/Text[position()=count(/List/Header/HeaderItem[text()='State']/preceding-sibling::*)+1]

If you need just the topmost line then:

//ListItem[1]/Text[position()=count(/List/Header/HeaderItem[text()='State']/preceding-sibling::*)+1]

P.S. - count(/List/Header/HeaderItem[text()='State']/preceding-sibling::*)+1 is the approach of obtaining element's position taken from this answer. So worth upvoting if you like it.

Alexey R.
  • 8,057
  • 2
  • 11
  • 27