XPath 不返回预期值
我有一个使用 jDOM 解析的 XML 文档,然后尝试获取满足 XPath 表达式的所有节点,但 XPath 始终不返回任何元素。 这是我的 XML 文件:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Using aggregate instance. Should pass -->
<pnml xmlns="http://www.pnml.org/version-2009/grammar/pnml">
<net id="rootAgregate" type="http://www.cs.stu.cn.ua/jess/enets">
<page id="main">
<transition id="agregate1">
<name>
<text>agregate1</text>
<graphics>
<offset x="22" y="-14"/>
</graphics>
</name>
<definition type="aggregate" subType="myAggregate"/>
<graphics>
<position x="950" y="484"/>
</graphics>
</transition>
<transition id="Input1">
<name>
<text>agregate1</text>
<graphics>
<offset x="22" y="-14"/>
</graphics>
</name>
<definition type="input"/>
<graphics>
<position x="950" y="484"/>
</graphics>
</transition>
<transition id="Output1">
<name>
<text>agregate1</text>
<graphics>
<offset x="22" y="-14"/>
</graphics>
</name>
<definition type="output"/>
<graphics>
<position x="950" y="484"/>
</graphics>
</transition>
</page>
</net>
</pnml>
这是我的 XPath 代码:
private List<String> getChildNames(Document parsedDom) throws JDOMException {
ArrayList<String> childNames = new ArrayList<String>();
XPath childCounter = XPath.newInstance("//definition[@type=\"aggregate\"]");
ListIterator<Element> listIterator = childCounter.selectNodes(parsedDom).listIterator();
while (listIterator.hasNext()) {
Element definitionElement = listIterator.next();
childNames.add(definitionElement.getAttributeValue("subType"));
}
return childNames;
}
我在这里检查了我的 XPath 表达式: http://www.whitebeam.org/library/guide/TechNotes/xpathtestbed.rhtm 它有效。
为什么它在我的代码中不起作用?
先感谢您。
I have XML document that I parse with jDOM and then try to take all nodes that satisfies XPath expression, but XPath always returns no elements.
Here is my XML file:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Using aggregate instance. Should pass -->
<pnml xmlns="http://www.pnml.org/version-2009/grammar/pnml">
<net id="rootAgregate" type="http://www.cs.stu.cn.ua/jess/enets">
<page id="main">
<transition id="agregate1">
<name>
<text>agregate1</text>
<graphics>
<offset x="22" y="-14"/>
</graphics>
</name>
<definition type="aggregate" subType="myAggregate"/>
<graphics>
<position x="950" y="484"/>
</graphics>
</transition>
<transition id="Input1">
<name>
<text>agregate1</text>
<graphics>
<offset x="22" y="-14"/>
</graphics>
</name>
<definition type="input"/>
<graphics>
<position x="950" y="484"/>
</graphics>
</transition>
<transition id="Output1">
<name>
<text>agregate1</text>
<graphics>
<offset x="22" y="-14"/>
</graphics>
</name>
<definition type="output"/>
<graphics>
<position x="950" y="484"/>
</graphics>
</transition>
</page>
</net>
</pnml>
Here is my XPath code:
private List<String> getChildNames(Document parsedDom) throws JDOMException {
ArrayList<String> childNames = new ArrayList<String>();
XPath childCounter = XPath.newInstance("//definition[@type=\"aggregate\"]");
ListIterator<Element> listIterator = childCounter.selectNodes(parsedDom).listIterator();
while (listIterator.hasNext()) {
Element definitionElement = listIterator.next();
childNames.add(definitionElement.getAttributeValue("subType"));
}
return childNames;
}
I checked my XPath expression here:
http://www.whitebeam.org/library/guide/TechNotes/xpathtestbed.rhtm
and it works.
Why it doesn't work in my code?
Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要 xpath 表达式中的名称空间。
如何在具有默认命名空间的 xml 文档上使用 XPath
使用 XPath 解析 XML Java 中的命名空间
You need the namespace in your xpath expression.
How to use XPath on xml docs having default namespace
Parse XML with XPath & namespaces in Java