在 Java 中使用 SAXON Xpath 引擎
这是我的代码:
public static void main(String[] args) {
// System.setProperty(
// "javax.xml.xpath.XPathFactory",
// "net.sf.saxon.xpath.XPathFactoryImpl");
String xml="<root><a>#BBB#</a><a>#CCC#</a><b><a>#DDD#</a></b></root>";
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)
这意味着当我通过 net.sf.saxon.xpath 明确创建我的工厂时,Java 正在使用 org.apache.xpath.compiler.XPathParser 类。 XPathFactoryImpl。
(实际上,我只需要在我的 xpath 中放入一些匹配
...因此,如果已知任何不涉及 Saxon 的解决方案,请考虑达到我的需求)。
我究竟做错了什么 ?
Here is my code :
public static void main(String[] args) {
// System.setProperty(
// "javax.xml.xpath.XPathFactory",
// "net.sf.saxon.xpath.XPathFactoryImpl");
String xml="<root><a>#BBB#</a><a>#CCC#</a><b><a>#DDD#</a></b></root>";
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();
}
}
And I get this :
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)
Which means Java is using org.apache.xpath.compiler.XPathParser
class when I clearly created my factory through net.sf.saxon.xpath.XPathFactoryImpl
.
(I actually only need to put some matches
in my xpaths... so if any solution not involving Saxon is known, consider my need reached).
What am I doing wrong ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从撒克逊人的例子来看:
工作正常。
From Saxon examples :
Works fine.