dom4j:如何解决此 XPath 错误?
我正在使用 dom4j 读取 XML,并使用 XPath 技术来选择所需的节点。考虑一下我的 XML 看起来像这样:
<Emp_Dir>
<Emp_Classification type ="Permanent" >
<Emp id= "1">
<name>jame</name>
<Emp_Bio>
<age>12</age>
<height>5.4</height>
<weight>78</weight>
</Emp_Bio>
<Emp_Details>
<salary>2000</salary>
<designation>developer</designation>
</Emp_Details>
</Emp>
<Emp id= "2">
<name>jame</name>
<Emp_Bio>
<age>12</age>
<height>5.4</height>
<weight>78</weight>
</Emp_Bio>
<Emp_Details>
<salary>2000</salary>
<designation>developer</designation>
</Emp_Details>
</Emp>
</Emp_Classification>
<Emp_Classification type ="Contract" >
.
.
.
</Emp_Classification>
<Emp_Classification type ="PartTime" >
.
.
.
</Emp_Classification>
</Emp_Dir>
注意:上面的 XML 对你来说可能看起来很难看,但我创建这个虚拟文件只是为了理解和保持项目的保密性
当我指定一些简单的 XPath 表达式时,例如:
//Emp_Classification (or)
/Emp_Dir/Emp_Classification
那么它的工作原理很好,但是当我指定一些复杂的表达式时,例如:
/Emp_Dir/Emp_Classification/[@type='Permanent'] (or)
//Emp_Dir/Emp_Classification/[@type='Permanent']
然后它给我以下错误:
"Invalid XPath expression: /Emp_Dir/Emp_Classification/[@type='Permanent'] Expected one of '.', '..', '@', '*', <QName>"
任何人都可以指导我 XPath 中出了什么问题吗?
我的第二个问题是,如何仅选择永久员工的 Emp_Bio 节点,这有效吗?
//Emp_Dir/Emp_Classification/[@type='Permanent']/Emp/Emp_Bio
I am reading an XML using dom4j by using XPath techniques for selecting desired nodes. Consider that my XML looks like this:
<Emp_Dir>
<Emp_Classification type ="Permanent" >
<Emp id= "1">
<name>jame</name>
<Emp_Bio>
<age>12</age>
<height>5.4</height>
<weight>78</weight>
</Emp_Bio>
<Emp_Details>
<salary>2000</salary>
<designation>developer</designation>
</Emp_Details>
</Emp>
<Emp id= "2">
<name>jame</name>
<Emp_Bio>
<age>12</age>
<height>5.4</height>
<weight>78</weight>
</Emp_Bio>
<Emp_Details>
<salary>2000</salary>
<designation>developer</designation>
</Emp_Details>
</Emp>
</Emp_Classification>
<Emp_Classification type ="Contract" >
.
.
.
</Emp_Classification>
<Emp_Classification type ="PartTime" >
.
.
.
</Emp_Classification>
</Emp_Dir>
Note: The above XML might looks ugly to you but i only create this dummy file for the sake of understanding and keeping the secracy of my project
When i specify some simple XPath expression, like:
//Emp_Classification (or)
/Emp_Dir/Emp_Classification
then its works fine but when i specify some complex expression like:
/Emp_Dir/Emp_Classification/[@type='Permanent'] (or)
//Emp_Dir/Emp_Classification/[@type='Permanent']
then it gives me the following error:
"Invalid XPath expression: /Emp_Dir/Emp_Classification/[@type='Permanent'] Expected one of '.', '..', '@', '*', <QName>"
Coulde anybody guides me what goes wrong in my XPath?
My second question is that how do i select the Emp_Bio node of Permanent Employees only, does this works?
//Emp_Dir/Emp_Classification/[@type='Permanent']/Emp/Emp_Bio
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用:
//Emp_Dir/Emp_Classification[@type='Permanent']
(注意删除
/
)然后使用:
//Emp_Dir/Emp_Classification[ @type='Permanent']/Emp/Emp_Bio
对于问题的后半部分。Use :
//Emp_Dir/Emp_Classification[@type='Permanent']
(note the removal of
/
)And then use this :
//Emp_Dir/Emp_Classification[@type='Permanent']/Emp/Emp_Bio
for the latter part of the question.