XPath 根据节点中的其他元素选择元素 [英] XPath select element based on other elements in node

查看:20
本文介绍了XPath 根据节点中的其他元素选择元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 XML 文档如下所示:

My XML doc looks like this:

<?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>

And I want to do a couple of things with it and I cant figure out how using XPath and I can find the specific case I need in the examples I find on line. My problem is I want to find 'Dxf' which is in Recognized_elements:reference:code but only when the is 'reg' and the is full_match>

我有一个不起作用的表达式如下:

The expression I have which does not work is the following:

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

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

the error I always get is as follows: 'Object reference not set to an instance of an object'

推荐答案

我认为您需要的是:

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 元素.

You were missing the namespace in a few places and you forgot to put the full path to a couple of the child elements. I'm not sure if you intended to read the reference/code element or the nominal/code element.

这篇关于XPath 根据节点中的其他元素选择元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆