XML Xpath选择上面节点的值

时间:2017-09-11 13:51:55

标签: xml xpath

我正在制作聊天机器人,我得到了以下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>

首先,我会查看所有短语并检查用户是否与我在上面的XML中添加的那个相同或类似,然后当我找到一个短语时,我想获得该主题的值一个XPath。

我将用户提交的文本保存在变量中,让我们将其称为&#34; UserText&#34;

我该怎么做?

我试过这个,但它没有用,没有比赛更不用说获取主题的@value

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

例如,如果用户键入&#34;你的名字是什么,丑陋?&#34; xpath将返回值&#39; name&#39;

谢谢&lt; 3!

1 个答案:

答案 0 :(得分:2)

xpath 表达式:

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