在 XPath 中是否可以将 OR 运算符与节点名称一起使用? [英] In XPath is it possible to use the OR operator with node names?
问题描述
使用 XPath,我知道您可以通过以下方式使用联合运算符:
//*[@id="Catalog"]/thead |//*[@id="目录"]/tbody
这对我来说似乎有点尴尬.有没有办法做类似于其中之一的事情?
//*[@id="Catalog"]/(thead|tbody)//*[@id="目录"]/(thead 或 tbody)//*[@id="Catalog"]/*[nodename="thead" 或 nodename="tbody"]
这对我来说似乎更具可读性和直观性...
虽然表达式:
<块引用>//*[@id="Catalog"]/*[name()="thead" or name()="tbody"]
正确
这个表达式更有效:
//*[@id="Catalog"]/*[self::thead 或 self::tbody]
还有第三种方法可以检查元素的名称是否是指定的字符串序列之一:
//*[@id="Catalog"]/*[contains('|thead|tbody|',concat('|',name(),'|'))]
在可能名称的数量非常长的情况下,使用最后一种技术特别实用(无限且未知长度).管道分隔的可能名称字符串甚至可以作为外部参数传递给转换,这大大增加了其通用性、可重用性和DRY"性.
With XPath, I know that you can use the union operator in the following way:
//*[@id="Catalog"]/thead | //*[@id="Catalog"]/tbody
This seems to be a little awkward to me though. Is there a way to do something similar to one of these instead?
//*[@id="Catalog"]/(thead|tbody)
//*[@id="Catalog"]/(thead or tbody)
//*[@id="Catalog"]/*[nodename="thead" or nodename="tbody"]
That seems a lot more readable and intuitive to me...
While the expression:
//*[@id="Catalog"]/*[name()="thead" or name()="tbody"]
is correct
This expression is more efficient:
//*[@id="Catalog"]/*[self::thead or self::tbody]
There is yet a third way to check if the name of an element is one of a specified sequence of strings:
//*[@id="Catalog"]/*[contains('|thead|tbody|',concat('|',name(),'|'))]
Using this last technique can be especially practical in case the number of possible names is very long (of unlimited and unknown length). The pipe-delimited string of possible names can even be passed as an external parameter to the transformation, which greatly increases its generality, re-usability and "DRY"-ness.
这篇关于在 XPath 中是否可以将 OR 运算符与节点名称一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!