【问题标题】:XPath select element based on other elements in nodeXPath 根据节点中的其他元素选择元素
【发布时间】:2013-08-23 15:01:44
【问题描述】:

我的 XML 文档如下所示:

<?xml version="1.0" encoding="ISO-8859-1"?>
<XCER xmlns="http://www.x.y.z/base" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.x.y.z/base APSchema.xsd" mp_id="56977" ma_id="398" sub_id="54977">
  <final_result OD="DGS=1.7:ADB=2" creator="Px" version="1.1" xsi:type="XCT">
    <code>3040280100331</code>
    <code_cnf>1.0000</code_cnf>
    <code_attr>seq</code_attr>
    <status_attr>fdos</status_attr>
    <text>xxx</text>
    <standardized_text>xxxx</standardized_text>
    <region>
      <type>address</type>
      <symbology>machine</symbology>
    </region>
    <recognized_elements>
      <type>reg</type>
      <nominal>
        <type>reg</type>
        <code>Dx</code>
        <code_cnf>0.0635</code_cnf>
        <location page="3">
          <face>rear</face>
          <polygon>
            <dot x="720" y="709" />
            <dot x="744" y="708" />
            <dot x="744" y="591" />
            <dot x="720" y="592" />
          </polygon>
        </location>
      </nominal>
      <reference>
        <type>reg</type>
        <code>Dx</code>
        <match_cnf>1.0000</match_cnf>
        <attribute>full_match</attribute>
      </reference>
    </recognized_elements>
    <recognized_elements>
      <type>cty</type>
      <reference>
        <type>cty</type>
        <code>Dx 9</code>
        <attribute>derived</attribute>
      </reference>
    </recognized_elements>
    <standardized_elements>
      <type>reg</type>
      <code>Dx</code>
    </standardized_elements>
    <standardized_elements>
      <type>cty</type>
      <code>Dx 9</code>
      <attribute>err</attribute>
    </standardized_elements>
  </final_result>
</XCER>

我想用它做几件事,但我不知道如何使用 XPath,我可以在网上找到的示例中找到我需要的具体案例。我的问题是我想找到在 Recognized_elements:reference:code 中的 'Dxf' 但只有当它是 'reg' 并且是 full_match>

我所拥有的不起作用的表达式如下:

doc.SelectSingleNode("/x:XCER[0]/x:final_result[@creator = 'Px']/x:recognized_elements/x:reference[type='reg' and attribute='full_match]/x:code", nsmgr).InnerText

我总是得到的错误如下: '对象引用未设置为对象的实例'

【问题讨论】:

  • 为什么是x:result 而不是x:final_result
  • 抱歉,原文中的错字,我现在已更改
  • 您还在寻找XCER 的节点1,而根节点是节点0。x:recognized_elements 也没有紧接在它下面的attribute 元素(它在@987654328 下@)。
  • 感谢您的帮助,我不得不混淆代码和 xml,因为它们非常敏感,所以我在写问题时犯了一堆错误

标签: xml vb.net xpath xml-parsing


【解决方案1】:

我想你需要的是这样的:

doc.SelectSingleNode("/x:XCER/x:final_result[@x:creator = 'Px']/x:recognized_elements[x:type='reg' and x:reference/x:attribute='full_match']/x:reference/x:code", nsmgr).InnerText

您在一些地方丢失了命名空间,并且忘记了将完整路径放置到几个子元素中。我不确定您是打算阅读 reference/code 元素还是 nominal/code 元素。

【讨论】:

  • 是我选择中缺少的命名空间,谢谢
猜你喜欢
  • 2015-04-03
  • 1970-01-01
  • 2018-05-21
  • 2012-03-18
  • 1970-01-01
  • 1970-01-01
  • 2017-02-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多