JeFawk
JeFawk

Reputation: 81

XML Xpath pick value of node above

I'm making a chat bot and I got the following xml

<questions> 
<topic value='age'> 
    <phrase mood_modifier='10'>what's your age?</phrase>
    <phrase mood_modifier='-10'>I dislike you, but what's your age?</phrase>
</topic>
<topic value='name'> 
    <phrase mood_modifier='10'>what's your name?</phrase>
    <phrase mood_modifier='-10'>what's your name, ugly?</phrase>
</topic>

At first I go through all the phrases and check if the user submited one is equal or similar to the one I added in the XML above, then when I do find a phrase, I want to get the value of the topic with an XPath.

I save the user submitted text in a variable, let's call it "UserText"

How would I do this?

I tried this but it didn't work, no match let alone acquiring the @value of the topic

/questions/topic/@value[../phrase/text()=UserText]

So for example if the user types "what's your name, ugly?" the xpath would return the value 'name'

Thanks <3 !

Upvotes: 3

Views: 38

Answers (1)

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

xpath expression:

//questions/topic[./phrase/text()="what's your name?"]/@value

Upvotes: 2

Related Questions