XPath选择基于节点中其他元素的元素

时间:2013-08-21 09:44:02

标签: xml vb.net xpath xml-parsing

我的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中找到'Dxf':reference:code,但只有当'reg'和is full_match&gt;

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

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

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

1 个答案:

答案 0 :(得分: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元素。