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!
The reason your xpath fails is that it is not namespace aware.
Try this one:
/*[local-name()='menu' and namespace-uri()='http://example.com/']/*[local-name()='content' and namespace-uri()='http://example.com/']/*[local-name()='description' and namespace-uri()='http://example.com/']
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
精彩评论