【发布时间】:2011-04-10 16:04:34
【问题描述】:
我在 Eclipse 下的 Java 中使用 XPath。我正在编写一个肥皂网络服务,XML 使用前缀和命名空间,我想删除前缀并只保留命名空间,因为我更喜欢看起来更易读的 XML 文档。
当我从 XML 定义文件中删除前缀并尝试使用 XPath 查询 XML 时,我开始为 XML 中的每个节点获取 null!
我的问题是,如果我想使用 XPath,我必须使用前缀吗?仅仅是命名空间就够了吗?
<myrequest
xmlns="http://www.mywebsite.com/xml/webservice"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.mywebsite.com/xml/webservice Request.xsd">
<state>
<value>demox</value>
这就是我的 XML 的开始方式,当我使用像 XPath.selectSingleNode() 这样的 XPath 查询它时,我总是从 XPath 中收到其中每个节点的 null。
String myExpression = "myrequest/state/value";
Document doc = new Document(requestXML);
Element e = doc.getRootElement();
request.setMybase(getBase((org.jdom.Element)
XPath.selectSingleNode(doc,myExpression)));
【问题讨论】:
-
好问题,+1。请参阅我的答案以获得完整的解释和解决方案。