如何使用XPATH获取特定节点的父节点?

发布于 2024-12-23 14:50:11 字数 1805 浏览 1 评论 0原文

我有下面的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

氛圍 2024-12-30 14:50:11

使用此 XPath 表达式

/*/*/*[linenumber = '801']/amount

但在此之前,请更正所呈现的格式不正确的 XML 文档(您可能没有注意到在加载 <代码>InputXml)。更具体地说,所有 amount 元素都没有结束标记。

Use this XPath expression:

/*/*/*[linenumber = '801']/amount

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, all amount elements have no closing tag.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文