如何使用XPATH获取特定节点的父节点?
我有下面的 xml 文件,我需要使用 xpath 来根据“行号”获取“金额”值。
<Doc>
<Documents>
<Document1>
<linenumber>800</linenumber>
<amount>100.00</amount>
<name>fee1</name>
</Document1>
<Document2>
<linenumber>801</linenumber>
<amount>200.00</amount>
<name>fee2</name>
</Document2>
<Document3>
<linenumber>802</linenumber>
<amount>300.00</amount>
<name>fee3</name>
</Document3>
<Document4>
<linenumber>803</linenumber>
<amount>400.00</amount>
<name>fee4</name>
</Document4>
</Documents>
</Doc>
我尝试了下面函数“GetDocumentField”中指定的 xpath,但函数调用 GetDocumentField(801, "amount") 没有返回任何值。我无法使用 LINQ,因为它基于 .Net Framework 2.0。谁能建议如何编写这个 xpath 查询?谢谢!
Private Function GetDocumentField(ByVal line As Integer, ByVal field As String) As String
Return GetValueFromXPath(String.Format("//linenumber[.='{0}']/parent::node()/{1}", line, field)))
End Function
Private Function GetValueFromXPath(ByVal xpath As String) As String
Dim node As XmlNode
node = InputXml.SelectSingleNode(xpath)
Return GetValueFromNode(node)
End Function
Private Function GetValueFromNode(ByVal node As XmlNode) As String
If node Is Nothing Then
Return String.Empty
End If
If node.InnerText Is Nothing Then
Return String.Empty
End If
Return node.InnerText
End Function
I have the below xml file and I need to use an xpath to get "amount" value based on "linenumber".
<Doc>
<Documents>
<Document1>
<linenumber>800</linenumber>
<amount>100.00</amount>
<name>fee1</name>
</Document1>
<Document2>
<linenumber>801</linenumber>
<amount>200.00</amount>
<name>fee2</name>
</Document2>
<Document3>
<linenumber>802</linenumber>
<amount>300.00</amount>
<name>fee3</name>
</Document3>
<Document4>
<linenumber>803</linenumber>
<amount>400.00</amount>
<name>fee4</name>
</Document4>
</Documents>
</Doc>
I tried the xpath specified in the below function 'GetDocumentField' but the function call GetDocumentField(801, "amount") did not return any value. I can't use LINQ since it is based on .Net framework 2.0. Can anyone suggest how to write this xpath query? Thanks!
Private Function GetDocumentField(ByVal line As Integer, ByVal field As String) As String
Return GetValueFromXPath(String.Format("//linenumber[.='{0}']/parent::node()/{1}", line, field)))
End Function
Private Function GetValueFromXPath(ByVal xpath As String) As String
Dim node As XmlNode
node = InputXml.SelectSingleNode(xpath)
Return GetValueFromNode(node)
End Function
Private Function GetValueFromNode(ByVal node As XmlNode) As String
If node Is Nothing Then
Return String.Empty
End If
If node.InnerText Is Nothing Then
Return String.Empty
End If
Return node.InnerText
End Function
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用此 XPath 表达式:
但在此之前,请更正所呈现的格式不正确的 XML 文档(您可能没有注意到在加载 <代码>InputXml)。更具体地说,所有
amount
元素都没有结束标记。Use this XPath expression:
But before this, correct the presented non-well-formed XML document (you probably didn't notice that there was a parsing exception when you loaded
InputXml
). More specifically, allamount
elements have no closing tag.