我需要使用XPath函数normalized-space()来规范化我要从XHTML文档中提取的文本:http://test.anahnarciso.com/clean_bigbook_0.html
我正在使用以下表达式:
//*[@slot="address"]/normalize-space(.)
Run Code Online (Sandbox Code Playgroud)
这在我用来测试XPath表达式的Qizx Studio中非常有效.
let $doc := doc('http://test.anahnarciso.com/clean_bigbook_0.html')
return $doc//*[@slot="address"]/normalize-space(.)
Run Code Online (Sandbox Code Playgroud)
这个简单的查询返回一个序列xs:string.
144 Hempstead Tpke
403 West St
880 Old Country Rd
8412 164th St
8412 164th St
1 Irving Pl
1622 McDonald Ave
255 Conklin Ave
22011 Hempstead Ave
7909 Queens Blvd
11820 Queens Blvd
1027 Atlantic Ave
1068 Utica Ave
1002 Clintonville St
1002 Clintonville St
1156 Hempstead Tpke
Route 49
10007 Rockaway Blvd
12694 Willets Point Blvd
343 James St
Run Code Online (Sandbox Code Playgroud)
现在,我想在我的Java代码中使用前一个表达式.
String exp = "//*[@slot=\"address"\"]/normalize-space(.)";
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression expr = xpath.compile(exp);
Object result = expr.evaluate(doc, XPathConstants.NODESET);
Run Code Online (Sandbox Code Playgroud)
但是最后一行抛出异常:
Cannot convert XPath value to Java object: required class is org.w3c.dom.NodeList; supplied value has type xs:string
很明显,我应该换XPathConstants.NODESET一些东西; 我试过XPathConstants.STRING但它只返回序列的第一个元素.
我怎样才能获得类似Strings数组的东西?
提前致谢.
您的表达式在 XPath 2.0 中有效,但在 XPath 1.0(在 Java 中使用)中是非法的 - 它应该是normalize-space(//*[@slot='address']).
无论如何,在 XPath 1.0 中,当normalize-space()在节点集上调用时,只采用第一个节点(按文档顺序)。
为了做你想做的事情,你需要使用一个 XPath 2.0 兼容的解析器,或者遍历生成的节点集并normalize-space()在每个节点上调用:
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression expr;
String select = "//*[@slot='address']";
expr = xpath.compile(select);
NodeList result = (NodeList)expr.evaluate(input, XPathConstants.NODESET);
String normalize = "normalize-space(.)";
expr = xpath.compile(normalize);
int length = result.getLength();
for (int i = 0; i < length; i++) {
System.out.println(expr.evaluate(result.item(i), XPathConstants.STRING));
}
Run Code Online (Sandbox Code Playgroud)
...输出正是您给定的输出。