首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用撒克逊XPath S9API评估S9API 2.0表达式

用撒克逊XPath S9API评估S9API 2.0表达式
EN

Stack Overflow用户
提问于 2018-01-08 11:04:03
回答 1查看 279关注 0票数 0

我正在尝试执行一个XPath 2.0表达式,我可能遗漏了什么,我能够形成XPathSelector,问题是如何在org.w3c.dom.Document对象上运行选择器?我的代码如下:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-08 12:59:23

你需要做的

代码语言:javascript
复制
selector.setContextItem(item)

若要设置XPath计算的上下文节点,以及获取引用DOM文档根节点的项的方式,请执行以下操作

代码语言:javascript
复制
XdmNode item = processor.newDocumentBuilder().wrap(document);

请注意,只有在有充分理由的情况下,才应该对Saxon使用DOM节点;使用Saxon的内部树实现通常要快5到10倍。如果你不在乎它是什么样的树,用

代码语言:javascript
复制
XdmNode item = processor.newDocumentBuilder().build(
  new File("E:\\workspaces\\dev_werewolf\\Platform_Manual\\manual\\UIFramework\\RoleBasedUIAssembly2.xml"));
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48148986

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档