XPath 1.0查找元素的值是否在值列表中 [英] XPath 1.0 to find if an element's value is in a list of values

查看:137
本文介绍了XPath 1.0查找元素的值是否在值列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法构建一个XPath来评估元素的值是否在预定义的值列表中?类似于此:

  / Location / Addr [State ='TX或AL或MA'] 

哪些会与德州,阿拉巴马州或马萨诸塞州的州元素匹配?我知道我可以解压缩表达式:

  / Location / Addr [State ='TX] or / Location / Addr [State ='AL']等... 

但是这是有点麻烦,因为xpaths是相当的长的,值的列表。我的google-fu在这个问题上没有太大的变化...

解决方案

你可以检查同一个方块内的多个条件括号:

  / Location / Addr [State ='TX'或State ='AL'或State ='MA'] 

或者如果您有一个很长的列表,您可以创建一个状态列表,并使用 contains() function。

  / Location / Addr [contains('TX AL MA ',州)] 

这对两个字母的状态缩写可以正常工作。如果你想使更长的字符串更健壮,你可以在末尾添加一些空格,并检查 _TX _ _AL _ 等(下划线为空格)。

  / Location / Addr [contains('TX AL MA',concat ('',州,'))] 


Is there a way to construct an XPath that evaluates whether an element's value is in a predefined list of values? Something akin to this:

/Location/Addr[State='TX or AL or MA']

Which would match nodes whith State elements for Texas, Alabama, or Massachusetts? I know that I can unpack the expression:

/Location/Addr[State='TX] or  /Location/Addr[State='AL'], etc...

But this is a bit cumbersome since the xpaths are quite long, as is the list of values. My google-fu isn't turning up much on the issue...

解决方案

You can check multiple conditions inside the same square brackets:

/Location/Addr[State='TX' or State='AL' or State='MA']

Or if you have a really long list, you can create a list of states and use the contains() function.

/Location/Addr[contains('TX AL MA', State)]

This will work fine for two-letter state abbreviations. If you want to make it more robust for longer strings you could add some spaces on the ends and check for _TX_, _AL_, etc. (where the underscores are spaces).

/Location/Addr[contains(' TX AL MA ', concat(' ', State, ' '))]

这篇关于XPath 1.0查找元素的值是否在值列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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