使用xpath和jdom选择节点 [英] Select a node using xpath and jdom

查看:141
本文介绍了使用xpath和jdom选择节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张xform文件

I have an xform document

<?xml version="1.0" encoding="UTF-8"?><h:html xmlns:h="http://www.w3.org/1999/xhtml" xmlns="http://www.w3.org/1999/xhtml"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:jr="http://openrosa.org/javarosa">
<h:head>
    <h:title>Summary</h:title>
    <model>
        <instance>
            <data vaultType="nsp_inspection.4.1">
                <metadata vaultType="metadata.1.1">
                    <form_start_time type="dateTime" />
                    <form_end_time type="dateTime" />
                    <device_id type="string" />
                    <username type="string" />
                </metadata>
                <date type="date" />
                <monitor type="string" />
            </data>
        </instance>
    </model>
</h:head>


我想使用xpath和jdom从xform中选择数据元素

I would like to select the data element from the xform using xpath and jdom

XPath xpath = XPath.newInstance("h:html/h:head/h:title/");

似乎工作正常并选择title元素但是

seems to work fine and selects the title element but

XPath xpath = XPath.newInstance("h:html/h:head/model");

不选择模型元素。
我猜它与命名空间有关。

does not select the model element. I guess it has something to do with the namespace.

推荐答案

一些事情。你真的应该使用JDOM 2.0.x ...(2.0.5是最新版本)。 2.0.x版本中的XPath API远远优于JDOM 1.x中的XPath:请参阅 https://github.com/hunterhacker/jdom/wiki/JDOM2-Feature-XPath-Upgrade

A few things. You really should be using JDOM 2.0.x ... (2.0.5 is latest release). The XPath API in the 2.0.x versions is far better than the one in JDOM 1.x: see https://github.com/hunterhacker/jdom/wiki/JDOM2-Feature-XPath-Upgrade

@wds是对的没有正确的xforms元素名称空间....这就是XPath工作的原因,因为它具有与相同的名称空间。您的代码可能会被破坏。

@wds is right about not having the correct namespace for the xforms elements too.... and that is why you XPath is working, because it has the same namespace as the xhtml elements with the 'h' prefix. Your code is likely to be broken still.

XPath中的命名空间经常让人感到困惑,因为 XPath 中的每个命名空间都有有一个前缀。即使某些东西是XML中的默认命名空间(没有像'model'元素那样的前缀),它还有在XPath中有一个。 XPath中没有前缀的查询总是引用'no namespace'命名空间....(XPath规范: http://www.w3.org/TR/xpath/#node-tests

Namespaces in XPaths often confuse people, because every namespace in an XPath has to have a prefix. Even if something is the default namespace in the XML (no prefix like your 'model' element), it has to have one in the XPath. queries with no prefix in the XPath always reference the 'no namespace' namespace.... (XPath specification: http://www.w3.org/TR/xpath/#node-tests )


A QName在节点测试中使用表达式上下文中的命名空间
声明扩展为扩展名。这与在开始和结束标记中对元素类型名称进行扩展
的方式相同,除了使用xmlns声明的默认名称空间
:如果QName没有前缀,则为
名称空间URI为空(这与扩展属性名称的方式相同)。如果QName的前缀在
中没有名称空间声明,那么
就是错误表达式上下文

A QName in the node test is expanded into an expanded-name using the namespace declarations from the expression context. This is the same way expansion is done for element type names in start and end-tags except that the default namespace declared with xmlns is not used: if the QName does not have a prefix, then the namespace URI is null (this is the same way attribute names are expanded). It is an error if the QName has a prefix for which there is no namespace declaration in the expression context



<假设@wds是正确的,并且模型元素的命名空间应该是 http:// www .w3.org / 2002 / xforms 然后你的文档中的命名空间扩展应该是xmlns =http://www.w3.org/2002/xforms。但是,此命名空间是默认命名空间,XPath查询中无前缀命名空间的URI是。

Assuming @wds is correct, and the namespace for the model element is supposed to be "http://www.w3.org/2002/xforms" then your namespace delcaration in your document should be xmlns="http://www.w3.org/2002/xforms". But, this namespace is the 'default' namespace, and the URI for the no-prefix namespace in your XPath query is "".

访问 http://www.w3.org/2002/xforms 命名空间你必须给它一个前缀fo XPath的上下文,假设是xpns(对于xpath名称空间)。在JDOM 1.x中,您可以使用以下命令添加该命名空间:

To access the http://www.w3.org/2002/xforms namespace in your XPath you have to give it a prefix fo the context of the XPath, let's say xpns (for xpath namespace). In JDOM 1.x you add that namespace with:

XPath xpath = XPath.newInstance("/h:html/h:head/xpns:model");
xpath.addNamespace(Namespace.getNamespace("xpns", "http://www.w3.org/2002/xforms");
Element model = (Element)xpath.selectSingleNode(mydoc)

注意如何将xpns添加到查询中。另外,请注意我已经'锚定'了h: / html引用文档的'/'根,这将提高查询评估的性能。

Note how that adds the xpns to the query. Also, note that I have 'anchored' the h:/html reference to the '/' root of the document, which will improve the performance of the query evaluation.

在JDOM 2.x中,XPath API显着更好(即使在某些情况下看起来有点矫枉过正)。

IN JDOM 2.x, the XPath API is significanty better (even though in some cases it may seem overkill).

XPathFactory xpf = XPathFactory.instance();
XPathExpression<Element> xpath = xpf.compile("/h:html/h:head/xpns:model",
              Filters.element(), null,
              Namespace.getNamesace("xpns", "http://www.w3.org/2002/xforms"));
Element model = xpath.evaluateFirst(mydoc);

在JDOM 2.x javadoc中查看有关新XPath API的更多信息: XPathFactory.compile(... )javadoc

See more about the new XPath API in the JDOM 2.x javadoc: XPathFactory.compile(...) javadoc

这篇关于使用xpath和jdom选择节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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