当前位置: 首页 > 工具软件 > Saxon > 使用案例 >

java saxon_在Java中使用SAXON Xpath引擎

谷梁云瀚
2023-12-01

这是我的代码:

public static void main(String[] args) {

// System.setProperty(

// "javax.xml.xpath.XPathFactory",

// "net.sf.saxon.xpath.XPathFactoryImpl");

String xml="#BBB##CCC##DDD#";

try{

JDocument dom = new JDocument(xml);

XPathFactory factory = net.sf.saxon.xpath.XPathFactoryImpl.newInstance();

XPath xpath = factory.newXPath();

XPathExpression expr = xpath.compile("//a[matches(.,'#...#')]");

Object result = expr.evaluate(dom, XPathConstants.NODESET);

NodeList nodes = (NodeList) result;

Nodes sharped = new Nodes(nodes);

for (Node n:sharped){

System.out.println(n.toString());

}

}

catch(Exception e){

e.printStackTrace();

}

}

我得到这个:

javax.xml.transform.TransformerException: Impossible to find the function : matches

at org.apache.xpath.compiler.XPathParser.error(XPathParser.java:608)

at org.apache.xpath.compiler.XPathParser.FunctionCall(XPathParser.java:1505)

at org.apache.xpath.compiler.XPathParser.PrimaryExpr(XPathParser.java:1444)

at org.apache.xpath.compiler.XPathParser.FilterExpr(XPathParser.java:1343)

at org.apache.xpath.compiler.XPathParser.PathExpr(XPathParser.java:1276)

这意味着org.apache.xpath.compiler.XPathParser当我通过明确创建工厂时,Java正在使用类net.sf.saxon.xpath.XPathFactoryImpl。

(实际上,我只需要matches在xpaths中放置一些…因此,如果知道不涉及Saxon的任何解决方案,请考虑达到了我的需要)。

我究竟做错了什么 ?

 类似资料: