如何根据子值使用 XPath 选择父级

问题描述 投票:0回答:1

我有以下 XML:

<A>
    <Lot>
        <Of>
            <Various>
                <Parents>
                    <Object NodeType="UsageType">
                        <Property Name="Descriptor" Value="A" Type="My.DescriptorType"/>
                        [...]
                    </Object>
                    <Object NodeType="UsageType">
                        <Property Name="Descriptor" Value="B" Type="My.DescriptorType"/>
                        [...]
                    </Object>
                    <Object NodeType="UsageType">
                        <Property Name="Descriptor" Value="C" Type="My.DescriptorType"/>
                        [...]
                    </Object>
                </Parents>
            </Various>
        </Of>
    </Lot>
</A>

我正在尝试选择具有子元素的对象元素,其中包含

<Property Name="Descriptor" Value="B" Type="My.DescriptorType"/>

我真的不知道如何进行此选择,因为这依赖于很多属性:

  • 该元素应该是一个“对象”,其属性为“NodeType”,值为“UsageType”
  • Property 类型的一个子元素,具有值为“Descriptor”的 Name 属性和值为“B”的 Value 属性。

我已经尝试过以下方法:

//Object[@NodeType='UsageType' and Property@Name='Descriptor' and Property@Value='B']

但是它不起作用(而且我认为它不能确保它是具有这两个属性的相同“属性”。

知道如何实现吗?

xml xpath
1个回答
0
投票
//Object[@NodeType='UsageType' and Property[@Name='Descriptor' and @Value='B']]
© www.soinside.com 2019 - 2024. All rights reserved.