XPath表达式不为//元素返回任何内容,但是//*返回一个计数 [英] XPath Expression returns nothing for //element, but //* returns a count

查看:160
本文介绍了XPath表达式不为//元素返回任何内容,但是//*返回一个计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将XOM与以下示例数据一起使用:

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>Patient Information</title>
    </head>
</html>

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

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

//*

但类似

//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.

我正在获取HTML,并使用tagsoup对其进行解析,然后从结果字符串构建XOM文档.哪一部分会如此严重地出错?我觉得这里发生了一些奇怪的编码问题,但我只是没有看到它. Java字符串是字符串,对吧?

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?

推荐答案

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

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

查询应为//html:head.您将必须提供名称空间映射到XPath查询.

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

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

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 );

这篇关于XPath表达式不为//元素返回任何内容,但是//*返回一个计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆