XPATH:使用通配符命名空间选择多个记录 [英] XPATH: Selecting multiple records with Wildcard Namespaces

查看:89
本文介绍了XPATH:使用通配符命名空间选择多个记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 XPATH 还很陌生,当某个节点等于两个值之一时,我需要从多个 XML 对象返回一个值.该解决方案还需要使用 local-name() 与命名空间无关.鉴于以下 XML,我希望返回B"的所有值,其中A"等于4"或5".

I am fairly new to XPATH and need to return a value from multiple XML objects when a certain node is equal to one of two values. The solution also needs to be namespace agnostic using local-name(). Given the following XML, I am looking to return all of the values of "B" where "A" is equal to either "4" or "5".

<?xml version="1.0"?>
    <element>
        <A>3</A>
        <B>Value 1</B>
    </element>
    <element>
        <A>4</A>
        <B>Value 2</B>
    </element>
    <element>
        <A>5</A>
        <B>Value 3</B>
    </element>

我尝试了以下表达式的多次迭代,但是当我尝试为A"添加第二个值时似乎无法获得正确的语法.我似乎无法找出或"表达式的正确语法.我也考虑过使用联合,但语法也不正确.

I have tried many iterations of the following expression but cannot seem to get the syntax correct when I try to add the second value for "A". I just can't seem to figure out the correct syntax for an "or" expression. I have also considered the use of a union and couldn't get that syntax correct either.

这适用于A"等于4"的单个值但如何添加5"值的测试,以便结果返回B"的两个值(值 2"和值 3")?

This works for when "A" equals a single value of "4" but how to I add the test for a value of "5" so that the result returns two values for "B" ("Value 2" and "Value 3")?

//*[local-name()='element'][*[local-name()='A']='4']/*[local-name()='B']/text()

谢谢.任何帮助将不胜感激!

Thank you. Any help would be very much appreciated!

推荐答案

试试这个:

//*[local-name() = 'element']/*[local-name() = 'A' and (. = 4 or . = 5)]
    /../*[local-name() = 'B']

此表达式选择Value 2Value 3.

这篇关于XPATH:使用通配符命名空间选择多个记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆