Search code examples
xmlxpath

How to select a parent using XPath based on a child value


I've the following 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>

I'm trying to select the Object element, that has a child, with the <Property Name="Descriptor" Value="B" Type="My.DescriptorType"/> inside.

I'm really not sure how to do this selection, because this rely on a lot of properties:

  • The element should be an "Object" with a property "NodeType" with value of "UsageType"
  • One children element of type Property, that has a Name property with value of "Descriptor" and a Value property of "B".

I've tried the following :

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

But it doesn't work(And I think it wouldn't ensure that it's the same "Property" that has both attributes.

Any idea how to implement this?


Solution

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