在 Dom4j 中使用 xsd 模式时,从 xpath 表达式中获取 null
我正在尝试在我的应用程序中使用 dom4j,并且在解析以下 XML 文件时遇到了问题:
<menu xmlns="http://example.com/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://example.com/ my-schema.xsd">
<content>
<caption>aaa</caption>
<description>vvvv</description>
</content>
</menu>
我初始化了 dom4j 解析器,现在我正在尝试检索描述节点:(
Node node = document.selectSingleNode( "/menu/content/description" );
我知道我可以使用迭代器,但 xpath 适合我更好)
我有 null
而不是描述节点。
当我试图找出问题所在时,我从 xml 中删除了模式声明(因此开头只有简单的
我宁愿有 XSD 声明,那么我做错了什么?
提前致谢!
I'm trying to use dom4j with my application, and I approached a problem with parsing following XML file:
<menu xmlns="http://example.com/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://example.com/ my-schema.xsd">
<content>
<caption>aaa</caption>
<description>vvvv</description>
</content>
</menu>
I initialized dom4j parser, and now I'm trying to retrieve description node:
Node node = document.selectSingleNode( "/menu/content/description" );
(I know I could use iterators, but xpath suits me better)
I've got null
instead of the description node.
When I tried to figure out what's wrong, I removed schema declaration from xml (so there was only plain simple <menu>
at the beginning) and then it worked perfectly fine.
I rather have that XSD declaration, so what am I doing wrong?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
xpath 失败的原因是它不支持命名空间。
试试这个:
更好的是,下载 DanSharp Xml Viewer,您可以自己生成这些路径。
http://www.bizbert.com/bizbert/2007/11 /25/DanSharp+XmlViewer.aspx
The reason your xpath fails is that it is not namespace aware.
Try this one:
Even better, download DanSharp Xml Viewer and you can generate these paths for yourself.
http://www.bizbert.com/bizbert/2007/11/25/DanSharp+XmlViewer.aspx