我想构造一个XPath查询,它将返回一个"div"或"table"元素,只要它有一个包含文本"abc"的后代.一个警告是它不能有任何div或table后代.
<div>
<table>
<form>
<div>
<span>
<p>abcdefg</p>
</span>
</div>
<table>
<span>
<p>123456</p>
</span>
</table>
</form>
</table>
</div>
Run Code Online (Sandbox Code Playgroud)
所以这个查询唯一正确的结果是:
/div/table/form/div
Run Code Online (Sandbox Code Playgroud)
我最好的尝试看起来像这样:
//div[contains(//text(), "abc") and not(descendant::div or descendant::table)] | //table[contains(//text(), "abc") and not(descendant::div or descendant::table)]
Run Code Online (Sandbox Code Playgroud)
但不会返回正确的结果.
谢谢你的帮助.
不太确定如何说出这个问题.我想知道是否有一种方法来检查自定义java类的某些部分,看它是否符合某个标准.比如这个
public Name(String forename, String middlename, String surname)
Run Code Online (Sandbox Code Playgroud)
然后当创建该类的实例数组时,
Name[] applicants = new Name[4];
applicants[0] = new Name("john","bob", "rush");
applicants[1] = new Name("joe","bob", "rushden");
applicants[2] = new Name("jack","bob", "rushden");
applicants[3] = new Name("jake","bob", "rushden");
Run Code Online (Sandbox Code Playgroud)
是否可以搜索类的实例
midddlename.equals("bob") && surname.equals("rush")
Run Code Online (Sandbox Code Playgroud)
我并不是在寻找解决方案if(surname.equals("bob")) then else等等
但更多内置的java类允许快速搜索数组.速度非常重要.
在调试模式下,如果我将鼠标悬停在谓词上,我看到的只是一些类型名称和一些不可理解的符号.这使得调试代码变得非常困难,例如知道某个变量所持有的谓词.我通常使用lambda表达式分配这个谓词值.有没有办法知道谓词包含什么?
例如,如果我有Predicate<object> myPred变量或List<Predicate<object>> predList变量,我如何调试myPred的值或者运行时包含的predList?
我本来可以发誓我最近看过一篇文章,但我找不到它.
我正在尝试创建一个类型来对数字mod进行二进制编码n,但为了这样做,我需要能够在类型级别自然地编写谓词:
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE UndecidableInstances #-}
module Modulo where
-- (pseudo-)binary representation of
-- numbers mod n
--
-- e.g. Mod Seven contains
-- Zero . Zero . Zero $ Stop
-- Zero . Zero . One $ Stop
-- Zero . One . Zero $ Stop
-- Zero . …Run Code Online (Sandbox Code Playgroud) 我想要一个具有以下API的方法:
//get all users with a role of admin
var users = myRepository.GetUsers(u => u.Role == Role.Admin);
Run Code Online (Sandbox Code Playgroud)
这样的事情会起作用吗?
IList<User> GetUsers(Func<User, bool> predicate)
{
var users = GetAllUsers();
return users.Where(predicate).ToList();
}
Run Code Online (Sandbox Code Playgroud)
如果是这样,我将能够指定更复杂的谓词,如(伪代码):
myRepository.GetUsers(u => u.CreatedDate is upto 14 days old);
Run Code Online (Sandbox Code Playgroud) 我正在使用番石榴库,并注意到一个非常有用的谓词没有定义 - "大于".还有其他地方我应该寻找像这样的基本谓词,还是我注定要创建我自己的功能支持jar包含这样的东西,并将其导入我的所有项目?有没有理由他们不会包含这个,但会花时间去做一堆其他的谓词(在Predicates类中)?
出于调试目的,我试图Predicate在Java 8中创建lambda表达式的字符串表示(特别是s,尽管对其他lambda表达式也很有趣).我的想法是这样的:
public class Whatever {
private static <T> String predicateToString(Predicate<T> predicate) {
String representation = ... // do magic
return representation;
}
public static void main(String[] args) {
System.out.println(Whatever.<Integer>predicateToString(i -> i % 2 == 0));
}
}
Run Code Online (Sandbox Code Playgroud)
输出将是i -> i % 2 == 0(或逻辑等价的东西).这个toString()方法似乎没有任何帮助,输出就是这样的com.something.Whatever$$Lambda$1/1919892312@5e91993f(我想这是预期的,因为toString()它没有被覆盖).
我不确定这样的事情是否可能,例如反射,到目前为止我当然无法找到任何东西.有任何想法吗?
我有以下搜索条件的地图:
private final Map<String, Predicate> searchMap = new HashMap<>();
private void initSearchMap() {
Predicate<Person> allDrivers = p -> p.getAge() >= 16;
Predicate<Person> allDraftees = p -> p.getAge() >= 18
&& p.getAge() <= 25
&& p.getGender() == Gender.MALE;
Predicate<Person> allPilots = p -> p.getAge() >= 23
&& p.getAge() <=65;
searchMap.put("allDrivers", allDrivers);
searchMap.put("allDraftees", allDraftees);
searchMap.put("allPilots", allPilots);
}
Run Code Online (Sandbox Code Playgroud)
我通过以下方式使用此地图:
pl.stream()
.filter(search.getCriteria("allPilots"))
.forEach(p -> {
p.printl(p.getPrintStyle("westernNameAgePhone"));
});
Run Code Online (Sandbox Code Playgroud)
我想知道,如何将一些参数传递到谓词的映射中?
即我想通过字符串缩写从地图中获取谓词,并将参数插入从地图谓词中取出的参数.
pl.stream()
.filter(search.getCriteria("allPilots",45, 56))
.forEach(p -> {
p.printl(p.getPrintStyle("westernNameAgePhone"));
});
Run Code Online (Sandbox Code Playgroud)
这里是链接从我用Google搜索这个地图谓语方法.
我正在尝试创建动态谓词,以便它可以用于过滤列表
public class Feature
{
public string Color{get;set;}
public string Weight{get;set;}
}
Run Code Online (Sandbox Code Playgroud)
我希望能够创建一个动态谓词,以便可以过滤List.我得到一些条件,因为字符串值">","<","> ="等.有什么方法可以做到这一点?
public Predicate<Feature> GetFilter(X property,T value, string condition) //no clue what X will be
{
switch(condition)
{
case ">=":
return new Predicate<Feature>(property >= value)//or something similar
}
}
Run Code Online (Sandbox Code Playgroud)
用法可能是:
var filterConditions=GetFilter(x=>x.Weight,100,">=");
Run Code Online (Sandbox Code Playgroud)
如何定义GetFilter?以及如何在其中创建谓词?
如何反转谓词的返回值,并删除返回false而不是true的元素?
这是我的代码:
headerList.remove_if(FindName(name));
Run Code Online (Sandbox Code Playgroud)
(请忽略缺少擦除)
使用FindName一个简单的仿函数:
struct FindName
{
CString m_NameToFind;
FindInspectionNames(const CString &nameToFind)
{
m_NameToFind = nameToFind;
}
bool operator()(const CHeader &header)
{
if(header.Name == m_NameToFind)
{
return true;
}
return false;
}
};
Run Code Online (Sandbox Code Playgroud)
我想要像:
list.remove_if(FindName(name) == false);
Run Code Online (Sandbox Code Playgroud)
还没有使用c ++ 0x,所以不允许使用lambdas.我希望有一个比编写NotFindName仿函数更好的解决方案.