XPath表达式不返回//元素,但//*返回计数

Ste*_*all 4 java xpath tag-soup xml-namespaces xom

我正在使用带有以下示例数据的XOM:

Element root = cleanDoc.getRootElement();
//find all the bold elements, as those mark institution and clinic.
Nodes nodes = root.query("//*");

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:html="http://www.w3.org/1999/xhtml">
    <head>
        <title>Patient Information</title>
    </head>
</html>
Run Code Online (Sandbox Code Playgroud)

以下元素返回许多元素(来自实际数据):

//*
Run Code Online (Sandbox Code Playgroud)

但有点像

//head
Run Code Online (Sandbox Code Playgroud)

什么都不返回 如果我遍历根的子节点,数字似乎匹配,如果我打印元素名称,一切看起来都是正确的.

我正在使用HTML,使用tagsoup解析它,然后从结果字符串构建XOM文档.这部分可能会出现如此可怕的错误?我觉得这里有一些奇怪的编码问题,但我只是没有看到它.Java字符串是字符串,对吧?

Lac*_*che 6

您的文档具有默认命名空间,这意味着在XPath模型中,所有元素都在该命名空间中.

查询应该是//html:head.您必须提供命名空间映射到XPath查询.

请注意,虽然XPath表达式使用名称空间前缀,但它必须匹配的名称空间uri.

XPathContext ctx = new XPathContext("html", "http://www.w3.org/1999/xhtml");
Nodes nodes = root.query("//html:head", ctx );
Run Code Online (Sandbox Code Playgroud)