Javascript 在 XPATH 中使用 OR 的两个条件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12562597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Two conditions using OR in XPATH
提问by edsamiracle
I have a textbox, 'txtSearch'. I am using it to search people by Last Name. this is my code.
我有一个文本框“txtSearch”。我用它来按姓氏搜索人。这是我的代码。
var xmlTempResultSearch = xmlResidentListDisplay.selectNodes(
"//PeopleList/Row[contains(translate(@LastName, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), '" +
txtSearch.value + "')]");
This code selects all last names in the XML like the text input in the txtSearch textbox.
此代码选择 XML 中的所有姓氏,如 txtSearch 文本框中的文本输入。
This translates all uppercase letters to lowercase letters.
这会将所有大写字母转换为小写字母。
So if I am searching for 'Dorosan', if I type 'doro', it retrieves the correct person because it translated the 'D' to 'd'. But when I type 'Doro', it doesn't retrieve the correct person.
因此,如果我正在搜索“Dorosan”,如果我输入“doro”,它会检索正确的人,因为它将“D”翻译成“d”。但是当我输入“Doro”时,它没有检索到正确的人。
I'm wondering if I can have two conditions in an XPATH, and how? I want to be able to translate all uppercase to lowercase, OR translate all lowercase to uppercase.
我想知道我是否可以在 XPATH 中有两个条件,以及如何设置?我希望能够将所有大写字母转换为小写字母,或者将所有小写字母转换为大写字母。
回答by Jiri Kremser
and
and or
are allowed inside the condition: [here]
. Or you may also use multiple paths in one XPath expression using the pipe sign.
and
并且or
允许在条件内:[here]
。或者,您也可以使用管道符号在一个 XPath 表达式中使用多个路径。
//PeopleList/Row[c1] | //PeopleList/Row[c2]
//PeopleList/Row[c1] | //PeopleList/Row[c2]
回答by CosminO
you can use or
/ and
inside [....]
你可以使用or
/and
里面[....]
Example:
例子:
//*[contains('abc') or contains('def') or text()='abcdef']
More info about operators: http://www.w3schools.com/xpath/xpath_operators.asp
有关运营商的更多信息:http: //www.w3schools.com/xpath/xpath_operators.asp
回答by Dimitre Novatchev
As noted by Michael Kay, no or
is necessary.
正如迈克尔·凯所说,没有or
必要。
Simply use:
只需使用:
PeopleList/Row
[contains(translate(@LastName,
'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
'abcdefghijklmnopqrstuvwxyz'), '"
+
translate(txtSearch.value,
'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
'abcdefghijklmnopqrstuvwxyz')'"
+ "')]");
回答by Michael Kay
I don't think you need an "or" here. You just need to translate both operands to lower-case, rather than only translating one of them.
我认为这里不需要“或”。您只需要将两个操作数都翻译成小写,而不是只翻译其中一个。