开发者

XPath Expression returns nothing for //element, but //* returns a count

开发者 https://www.devze.com 2022-12-21 05:35 出处:网络
I\'m using XOM with the following sample data: Element root = cleanDoc.getRootElement(); //find all the bold elements, as those mark institution and clinic.

I'm using XOM with the following sample data:

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&g开发者_运维问答t;Patient Information</title>
    </head>
</html>

The following element returns many elements (from real data):

//*

but something like

//head

Returns nothing. If I run through the children of the root, the numbers seem to match up, and if I print the element name, everything seems to look correct.

I'm taking HTML, parsing it with tagsoup, and then building a XOM Document from the resulting string. What part of this could go so horribly wrong? I feel there's some weird encoding issue going on here, but I'm just not seeing it. Java Strings are Strings, right?


Your document has a default namespace, which means in the XPath model all the elements are in that namespace.

The query should be //html:head. You will have to supply the namespace mapping to the XPath query.

Note that while the XPath expression uses a namespace prefix, it is the namespace uri that must match.

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

精彩评论

暂无评论...
验证码 换一张
取 消