使用xpath从XML文件获取区域

get region from xml file using xpath

本文关键字:获取 区域 文件 XML xpath 使用      更新时间:2023-09-26

我有一个内容为region, state, city name的xml文件

<childrens>
    <child_1 entity_id="1" value="Region">
        <child_2 entity_id="2" value="Asia">
            <child_3 entity_id="3" value="Gujarat">
                <child_4 entity_id="4" value="Ahemdabad" />
            </child_3>
        </child_2>
    </child_1>
</childrens>

我试图获得地区名称,如果输入的城市名称在XML文件中匹配。

Ahemdabad(城市名称)中有一个变量

这是我的xpath:-
value = 'Ahemdabad';
            var xPath = '//*[@value= "' + value+'"]' + 
                            '/../../@entity_id';

当我尝试将城市名称作为字符串直接转换为xpath时,其工作

            var xPath = '//*[@value= "Ahemdabad"]' + 
                            '/../../@entity_id';

它不回我任何东西。
请帮我解决我的xpath问题。
谢谢。

我猜问题出在声明语法上。在Xquery中使用xpath时,我们将变量声明为let $var1 = "Ahemdabad"

,如果使用XSLT,声明类似于

价值

声明的变量在必要的位置寻址。你能解释一下你是如何使用它的吗?