【发布时间】:2018-01-08 11:04:03
【问题描述】:
我正在尝试执行 XPath 2.0 表达式,我可能遗漏了一些东西,我能够形成 XPathSelector,问题是如何在 org.w3c.dom.Document 对象上运行选择器?我的代码如下:
public class XPath2Test {
public static void main(String[] args) throws Exception{
String assemblyXPathString="/child::AssemblyBase/child::Assembly[attribute::roles='ALL' or contains(attribute::roles,$role)]";
DOMParser parser=new DOMParser();
FileInputStream fis=new FileInputStream("E:\\workspaces\\dev_werewolf\\Platform_Manual\\manual\\UIFramework\\RoleBasedUIAssembly2.xml");
InputSource inputSource=new InputSource(fis);
parser.parse(inputSource);
Document document=parser.getDocument();
Processor processor=new Processor(false);
//processor.newDocumentBuilder().build(new File("E:\\workspaces\\dev_werewolf\\Platform_Manual\\manual\\UIFramework\\RoleBasedUIAssembly2.xml"));
XPathCompiler compiler=processor.newXPathCompiler();
compiler.declareVariable(new QName("role"));
XPathExecutable executable=compiler.compile(assemblyXPathString);
XPathSelector selector=executable.load();
ArrayList<String> values=new ArrayList<>();
values.add("CIO");
selector.setVariable(new QName("role"), XdmItem.makeSequence(values));
//Now what???, how to run the quesry against the 'document'???
selector.evaluate();//Exception
}//main closing
}//class closing
【问题讨论】: