如何使用 Jaxen 选择具有空白命名空间的内容?

发布于 2024-08-19 17:13:09 字数 649 浏览 2 评论 0原文

我有以下 xml:

<config xmlns="http://www.someurl.com">
  <product>
    <brand>
      <content />
    </brand>
  </product>
</config>

我正在将其很好地读入 JDOM。

然而,当我尝试使用 Jaxen 抓取内容时,我似乎什么也得不到。

这是一个似乎不起作用的例子:

XPath xpath = new JDOMXPath("config");

SimpleNamespaceContext namespaceContext = new SimpleNamespaceContext();
namespaceContext.addNamespace("", "http://www.someurl.com");

xpath.setNamespaceContext(namespaceContext);

assert xpath.selectNodes(document).size() > 0 : "should find more than 0";

这个断言总是失败。

我做错了什么?

I have the following xml:

<config xmlns="http://www.someurl.com">
  <product>
    <brand>
      <content />
    </brand>
  </product>
</config>

I'm reading it nicely into JDOM.

However, when I try to use Jaxen to grab the contents, I can't seem to get anything.

Here's an example of what doesn't seem to work:

XPath xpath = new JDOMXPath("config");

SimpleNamespaceContext namespaceContext = new SimpleNamespaceContext();
namespaceContext.addNamespace("", "http://www.someurl.com");

xpath.setNamespaceContext(namespaceContext);

assert xpath.selectNodes(document).size() > 0 : "should find more than 0";

This assertion always fails.

What am I doing wrong?

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

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

发布评论

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

评论(1

拥抱我好吗 2024-08-26 17:13:09

您必须分配一个前缀。进行调用 addNamespace("hopfrog", "http://...");
然后制作XPath(“hopfrog:config”);

请记住,XML 中的前缀不是真实数据模型的一部分。真实数据模型为每个元素和属性分配一个 URL(可能为空)。您可以在 XPath 中使用任何您想要的前缀,只要它绑定到正确的 URL。由于您希望 URL 为空,因此您将前缀绑定到“空白”。

You have to assign a prefix. Make that call addNamespace("hopfrog", "http://...");
Then make the XPath ("hopfrog:config");

Keep in mind that the prefixes in XML aren't part of the real data model. The real data model assigns a URL, possibly blank, to each element and attribute. You can use any prefix you want in XPath so long as it's bound to the right URL. Since the URL you want it blank, you bind a prefix to 'blank'.

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