XPathExpression 未在正确的上下文中求值?

发布于 2024-11-06 18:23:58 字数 2168 浏览 0 评论 0原文

我正在尝试解析来自 USGS 的一些 XML。

这里是一个示例

“parameterCd”参数列出了我想要返回的 3 项数据。我可能会也可能不会拿回全部 3 个。

我正在使用 javax 库在 Android 上执行此操作。

在我的代码中,我最初检索 0-3 ns1:timeSeries 节点。这很好用。然后我想做的是,在单个 timeSeries 节点的上下文中,检索 ns1:variable 和 ns1:values 节点。

因此,在下面的代码中,我有:

expr = xpath.compile("//ns1:variable");
NodeList variableNodes = (NodeList) expr.evaluate(timeSeriesNode, XPathConstants.NODESET);

我希望只返回一个节点,因为评估应该发生在我传入的单个 timeSeriesNode 的上下文中(根据 文档) 。然而,它反而返回文档的所有 ns1:variable 节点。

我错过了什么吗?

这是相关部分...

XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
xpath.setNamespaceContext(new InstantaneousValuesNamespaceContext());
XPathExpression expr;
NodeList timeSeriesNodes = null;
InputStream is = new ByteArrayInputStream(sourceXml.getBytes());
try {
    expr = xpath.compile("//ns1:timeSeries");
    timeSeriesNodes = (NodeList) expr.evaluate(new InputSource(is), XPathConstants.NODESET);

    for(int timeSeriesIndex = 0;timeSeriesIndex < timeSeriesNodes.getLength(); timeSeriesIndex++){
        Node timeSeriesNode = timeSeriesNodes.item(timeSeriesIndex);
        expr = xpath.compile("//ns1:variable");
        NodeList variableNodes = (NodeList) expr.evaluate(timeSeriesNode, XPathConstants.NODESET);

        // Problem here. I've got all the variables, not the individual one I want.
        for(int variableIndex = 0; variableIndex < variableNodes.getLength(); variableIndex++){
            Node variableNode = variableNodes.item(variableIndex);
            expr = xpath.compile("//ns1:valueType");
            NodeList valueTypeNodes = (NodeList) expr.evaluate(variableNode, XPathConstants.NODESET);
        }
    }
} catch (XPathExpressionException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

I'm trying to parse some XML from the USGS.

Here's an example

The "parameterCd" parameter lists the 3 items of data I want back. I may or may not get all 3 back.

I'm doing this on an Android using the javax libraries.

In my code, I initially retrieve the 0-3 ns1:timeSeries nodes. This works fine. What I then want to do is, within the context of a single timeSeries node, retrieve the ns1:variable and ns1:values nodes.

So in my code below where I have:

expr = xpath.compile("//ns1:variable");
NodeList variableNodes = (NodeList) expr.evaluate(timeSeriesNode, XPathConstants.NODESET);

I would expect to only get back one node, since the evaluate SHOULD be happening in the context of the single timeSeriesNode that I'm passing in (according to the documentation). Instead, however, it returns all of the ns1:variable nodes for the document, however.

Am I missing something?

Here's the relevant portions...

XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
xpath.setNamespaceContext(new InstantaneousValuesNamespaceContext());
XPathExpression expr;
NodeList timeSeriesNodes = null;
InputStream is = new ByteArrayInputStream(sourceXml.getBytes());
try {
    expr = xpath.compile("//ns1:timeSeries");
    timeSeriesNodes = (NodeList) expr.evaluate(new InputSource(is), XPathConstants.NODESET);

    for(int timeSeriesIndex = 0;timeSeriesIndex < timeSeriesNodes.getLength(); timeSeriesIndex++){
        Node timeSeriesNode = timeSeriesNodes.item(timeSeriesIndex);
        expr = xpath.compile("//ns1:variable");
        NodeList variableNodes = (NodeList) expr.evaluate(timeSeriesNode, XPathConstants.NODESET);

        // Problem here. I've got all the variables, not the individual one I want.
        for(int variableIndex = 0; variableIndex < variableNodes.getLength(); variableIndex++){
            Node variableNode = variableNodes.item(variableIndex);
            expr = xpath.compile("//ns1:valueType");
            NodeList valueTypeNodes = (NodeList) expr.evaluate(variableNode, XPathConstants.NODESET);
        }
    }
} catch (XPathExpressionException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流殇 2024-11-13 18:23:58

尝试更改

//ns1:variable

.//ns1:variable

即使,正如文档所说,表达式是在当前节点的上下文中计算的, // 是特殊的并且(除非修改)always 表示“搜索”从根开始的整个文档”。通过放入.,您可以强制实现您想要的含义,“从这一点向下搜索整个树”。

Try changing

//ns1:variable

to

.//ns1:variable

Even though, as the docs say, the expression is evaluated within the context of the current node, // is special and (unless modified) always means 'search the whole document from the root'. By putting the . in, you force the meaning you want, 'search the whole tree from this point downwards'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文