为什么这个 XPath 表达式获取太多数据?

发布于 2024-12-13 12:16:20 字数 5438 浏览 0 评论 0原文

我有一个 XPath 表达式,它应该只从 XML 文档中返回/获取一个节点。但它正在变得不止这一点。我不明白为什么。

隐藏代码:

Dim xmlNameTbl As XmlNameTable = rootDoc.NameTable
Dim xmlNS As XmlNamespaceManager = New XmlNamespaceManager(xmlNameTbl)
xmlNS.AddNamespace("asp", "http://test.com/asp")

Dim sectionPosition As String = rowNode.GetAttribute("ID")
'In this example sectionPosition is "A03"

Dim sectionLetter As String = rowNode.GetAttribute("ID").Substring(0, 1)
Dim sectionRowNumberText As String = rowNode.GetAttribute("ID").Remove(0, 1)
Dim sectionRowNumber As Integer
Integer.TryParse(sectionRowNumberText, sectionRowNumber)

Dim addingNav As XPathNavigator = rootDoc.CreateNavigator

Dim hello = rootDoc.ChildNodes

Dim addingItr As XPathNodeIterator = addingNav.Select("//asp:TableRow[@ID='" & sectionPosition & "']", xmlNS)
'Nodes with A03 and A02 are being returned, even though it should be only A03 returned

XML 文档:

<?xml version="1.0" encoding="utf-8"?>
<Root xmlns:asp="http://test.com/asp" xmlns:meta="http://test.com/meta" xmlns:cc1="http://test.com/cc1">
  <asp:TableRow ID="A03">
    <asp:TableCell>
      <asp:Localize ID="tagthreeCtrlNumberRes" meta:resourcekey="tagthreeCtrlNumberRes" runat="server" />
    </asp:TableCell>
    <asp:TableCell>
      <asp:Localize ID="tagthreeCtrlDescRes" meta:resourcekey="tagthreeCtrlDescRes" runat="server" />
      <asp:Localize ID="tagthreeCtrlNoteRes" meta:resourcekey="tagthreeCtrlNoteRes" runat="server" />
    </asp:TableCell>
    <asp:TableCell>
      <asp:RadioButtonList ID="rblthreeCtrlRes0" RepeatDirection="Horizontal" runat="server">
        <asp:ListItem Text="Yes" meta:resourcekey="rblthreeCtrl0Res0" Value="1" />
        <asp:ListItem Text="No" meta:resourcekey="rblthreeCtrl1Res0" Value="0" />
        <asp:ListItem Text="N/A" meta:resourcekey="rblthreeCtrl2Res0" Value="2" />
      </asp:RadioButtonList>
      <asp:RadioButtonList ID="rblthreeCtrlRes1" RepeatDirection="Horizontal" runat="server">
        <asp:ListItem Text="Yes" meta:resourcekey="rblthreeCtrl0Res1" Value="1" />
        <asp:ListItem Text="No" meta:resourcekey="rblthreeCtrl1Res1" Value="0" />
        <asp:ListItem Text="N/A" meta:resourcekey="rblthreeCtrl2Res1" Value="2" />
      </asp:RadioButtonList>
    </asp:TableCell>
    <asp:TableCell>
      <asp:Button ID="cmdthreeCtrlRes" meta:resourcekey="cmdthreeCtrlRes" runat="server" OnClick="FormDataSave_Click" />
    </asp:TableCell>
    <asp:TableCell>
      <asp:Localize ID="lblAssmthreeCtrlRes" meta:resourcekey="lblAssmthreeCtrlRes" runat="server" />
    </asp:TableCell>
    <asp:TableCell>
      <asp:Localize ID="lblQualthreeCtrlRes" meta:resourcekey="lblQualthreeCtrlRes" runat="server" />
      <asp:Button ID="cmdQualAcceptthreeCtrlRes" meta:resourcekey="cmdQualAcceptthreeCtrlRes" OnClick="cmdQualAccept_Click" runat="server" Text="Accept" Visible="True" />
    </asp:TableCell>
    <asp:TableCell />
    <asp:TableCell />
  </asp:TableRow>
  <asp:TableRow ID="A04">
    <asp:TableCell>
      <asp:Localize ID="tagoneCtrlNumberRes" meta:resourcekey="tagoneCtrlNumberRes" runat="server" />
    </asp:TableCell>
    <asp:TableCell>
      <asp:Localize ID="tagoneCtrlDescRes" meta:resourcekey="tagoneCtrlDescRes" runat="server" />
      <asp:Localize ID="tagoneCtrlNoteRes" meta:resourcekey="tagoneCtrlNoteRes" runat="server" />
    </asp:TableCell>
    <asp:TableCell>
      <asp:Localize ID="tagtxtoneCtrlRes0" meta:resourcekey="tagtxtoneCtrlRes0" runat="server" />
      <asp:Textbox ID="txtoneCtrlRes0" runat="server" />
    </asp:TableCell>
    <asp:TableCell>
      <asp:Button ID="cmdoneCtrlRes" meta:resourcekey="cmdoneCtrlRes" runat="server" OnClick="FormDataSave_Click" />
    </asp:TableCell>
    <asp:TableCell>
      <asp:Localize ID="lblAssmoneCtrlRes" meta:resourcekey="lblAssmoneCtrlRes" runat="server" />
    </asp:TableCell>
    <asp:TableCell />
    <asp:TableCell />
    <asp:TableCell />
  </asp:TableRow>
  <asp:TableRow ID="A02">
    <asp:TableCell>
      <asp:Localize ID="tagtwoCtrlNumberRes" meta:resourcekey="tagtwoCtrlNumberRes" runat="server" />
    </asp:TableCell>
    <asp:TableCell>
      <asp:Localize ID="tagtwoCtrlDescRes" meta:resourcekey="tagtwoCtrlDescRes" runat="server" />
      <asp:Localize ID="tagtwoCtrlNoteRes" meta:resourcekey="tagtwoCtrlNoteRes" runat="server" />
    </asp:TableCell>
    <asp:TableCell>
      <asp:Localize ID="tagtxttwoCtrlRes0" meta:resourcekey="tagtxttwoCtrlRes0" runat="server" />
      <asp:Textbox ID="txttwoCtrlRes0" runat="server" />
    </asp:TableCell>
    <asp:TableCell>
      <asp:Button ID="cmdtwoCtrlRes" meta:resourcekey="cmdtwoCtrlRes" runat="server" OnClick="FormDataSave_Click" />
    </asp:TableCell>
    <asp:TableCell>
      <asp:Localize ID="lblAssmtwoCtrlRes" meta:resourcekey="lblAssmtwoCtrlRes" runat="server" />
    </asp:TableCell>
    <asp:TableCell />
    <asp:TableCell />
    <asp:TableCell />
  </asp:TableRow>
</Root>

I have a XPath expression that is supposed to return/get only one node out of the XML document. But it is getting more than the one. I don't understand why.

Code-behind:

Dim xmlNameTbl As XmlNameTable = rootDoc.NameTable
Dim xmlNS As XmlNamespaceManager = New XmlNamespaceManager(xmlNameTbl)
xmlNS.AddNamespace("asp", "http://test.com/asp")

Dim sectionPosition As String = rowNode.GetAttribute("ID")
'In this example sectionPosition is "A03"

Dim sectionLetter As String = rowNode.GetAttribute("ID").Substring(0, 1)
Dim sectionRowNumberText As String = rowNode.GetAttribute("ID").Remove(0, 1)
Dim sectionRowNumber As Integer
Integer.TryParse(sectionRowNumberText, sectionRowNumber)

Dim addingNav As XPathNavigator = rootDoc.CreateNavigator

Dim hello = rootDoc.ChildNodes

Dim addingItr As XPathNodeIterator = addingNav.Select("//asp:TableRow[@ID='" & sectionPosition & "']", xmlNS)
'Nodes with A03 and A02 are being returned, even though it should be only A03 returned

XML Document:

<?xml version="1.0" encoding="utf-8"?>
<Root xmlns:asp="http://test.com/asp" xmlns:meta="http://test.com/meta" xmlns:cc1="http://test.com/cc1">
  <asp:TableRow ID="A03">
    <asp:TableCell>
      <asp:Localize ID="tagthreeCtrlNumberRes" meta:resourcekey="tagthreeCtrlNumberRes" runat="server" />
    </asp:TableCell>
    <asp:TableCell>
      <asp:Localize ID="tagthreeCtrlDescRes" meta:resourcekey="tagthreeCtrlDescRes" runat="server" />
      <asp:Localize ID="tagthreeCtrlNoteRes" meta:resourcekey="tagthreeCtrlNoteRes" runat="server" />
    </asp:TableCell>
    <asp:TableCell>
      <asp:RadioButtonList ID="rblthreeCtrlRes0" RepeatDirection="Horizontal" runat="server">
        <asp:ListItem Text="Yes" meta:resourcekey="rblthreeCtrl0Res0" Value="1" />
        <asp:ListItem Text="No" meta:resourcekey="rblthreeCtrl1Res0" Value="0" />
        <asp:ListItem Text="N/A" meta:resourcekey="rblthreeCtrl2Res0" Value="2" />
      </asp:RadioButtonList>
      <asp:RadioButtonList ID="rblthreeCtrlRes1" RepeatDirection="Horizontal" runat="server">
        <asp:ListItem Text="Yes" meta:resourcekey="rblthreeCtrl0Res1" Value="1" />
        <asp:ListItem Text="No" meta:resourcekey="rblthreeCtrl1Res1" Value="0" />
        <asp:ListItem Text="N/A" meta:resourcekey="rblthreeCtrl2Res1" Value="2" />
      </asp:RadioButtonList>
    </asp:TableCell>
    <asp:TableCell>
      <asp:Button ID="cmdthreeCtrlRes" meta:resourcekey="cmdthreeCtrlRes" runat="server" OnClick="FormDataSave_Click" />
    </asp:TableCell>
    <asp:TableCell>
      <asp:Localize ID="lblAssmthreeCtrlRes" meta:resourcekey="lblAssmthreeCtrlRes" runat="server" />
    </asp:TableCell>
    <asp:TableCell>
      <asp:Localize ID="lblQualthreeCtrlRes" meta:resourcekey="lblQualthreeCtrlRes" runat="server" />
      <asp:Button ID="cmdQualAcceptthreeCtrlRes" meta:resourcekey="cmdQualAcceptthreeCtrlRes" OnClick="cmdQualAccept_Click" runat="server" Text="Accept" Visible="True" />
    </asp:TableCell>
    <asp:TableCell />
    <asp:TableCell />
  </asp:TableRow>
  <asp:TableRow ID="A04">
    <asp:TableCell>
      <asp:Localize ID="tagoneCtrlNumberRes" meta:resourcekey="tagoneCtrlNumberRes" runat="server" />
    </asp:TableCell>
    <asp:TableCell>
      <asp:Localize ID="tagoneCtrlDescRes" meta:resourcekey="tagoneCtrlDescRes" runat="server" />
      <asp:Localize ID="tagoneCtrlNoteRes" meta:resourcekey="tagoneCtrlNoteRes" runat="server" />
    </asp:TableCell>
    <asp:TableCell>
      <asp:Localize ID="tagtxtoneCtrlRes0" meta:resourcekey="tagtxtoneCtrlRes0" runat="server" />
      <asp:Textbox ID="txtoneCtrlRes0" runat="server" />
    </asp:TableCell>
    <asp:TableCell>
      <asp:Button ID="cmdoneCtrlRes" meta:resourcekey="cmdoneCtrlRes" runat="server" OnClick="FormDataSave_Click" />
    </asp:TableCell>
    <asp:TableCell>
      <asp:Localize ID="lblAssmoneCtrlRes" meta:resourcekey="lblAssmoneCtrlRes" runat="server" />
    </asp:TableCell>
    <asp:TableCell />
    <asp:TableCell />
    <asp:TableCell />
  </asp:TableRow>
  <asp:TableRow ID="A02">
    <asp:TableCell>
      <asp:Localize ID="tagtwoCtrlNumberRes" meta:resourcekey="tagtwoCtrlNumberRes" runat="server" />
    </asp:TableCell>
    <asp:TableCell>
      <asp:Localize ID="tagtwoCtrlDescRes" meta:resourcekey="tagtwoCtrlDescRes" runat="server" />
      <asp:Localize ID="tagtwoCtrlNoteRes" meta:resourcekey="tagtwoCtrlNoteRes" runat="server" />
    </asp:TableCell>
    <asp:TableCell>
      <asp:Localize ID="tagtxttwoCtrlRes0" meta:resourcekey="tagtxttwoCtrlRes0" runat="server" />
      <asp:Textbox ID="txttwoCtrlRes0" runat="server" />
    </asp:TableCell>
    <asp:TableCell>
      <asp:Button ID="cmdtwoCtrlRes" meta:resourcekey="cmdtwoCtrlRes" runat="server" OnClick="FormDataSave_Click" />
    </asp:TableCell>
    <asp:TableCell>
      <asp:Localize ID="lblAssmtwoCtrlRes" meta:resourcekey="lblAssmtwoCtrlRes" runat="server" />
    </asp:TableCell>
    <asp:TableCell />
    <asp:TableCell />
    <asp:TableCell />
  </asp:TableRow>
</Root>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

热血少△年 2024-12-20 12:16:20

我找不到上面的代码/XML 有什么问题。

运行您的示例按预期工作,addingItr.Count 返回 1。

我通过迭代返回的节点来仔细检查这一点,如下所示:

For Each n As XPathNavigator In addingItr
    Dim nodeName As String = n.Name
Next

您正在运行什么代码来确定返回节点的计数?

编辑

来源:XPathNodeIterator 类< /a>

XPathNavigator 类返回的 XPathNodeIterator 对象是
未定位在选定节点集中的第一个节点上。致电
必须使 XPathNodeIterator 类的 MoveNext 方法
将 XPathNodeIterator 对象定位在第一个节点上
选定的一组节点。

所以调试时看到的是根节点的 InnerXml 属性。

I cannot find anything wrong with the above code/XML.

Running your example works as expected, with addingItr.Count returning 1.

I double checked this by iterating the returned nodes like so:

For Each n As XPathNavigator In addingItr
    Dim nodeName As String = n.Name
Next

What code are you running to determine the count of the returned nodes?

Edit

From: XPathNodeIterator Class

An XPathNodeIterator object returned by the XPathNavigator class is
not positioned on the first node in a selected set of nodes. A call to
the MoveNext method of the XPathNodeIterator class must be made to
position the XPathNodeIterator object on the first node in the
selected set of nodes.

So what you are seeing when debugging is the InnerXml property of the root node.

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