Home > Software engineering >  extract subnodes that have the same attributes but with different values excluding a value
extract subnodes that have the same attributes but with different values excluding a value

Time:11-08

I have an XML file which contains a main node main with a CA subnode which can contain multiple CB nodes. In turn, the CB nodes can contain multiple CC nodes with attribute R which can have different values. In the example shown R can have the value 'A' or 'B'.

<?xml version="1.0" encoding="UTF-8"?>
<main>
    <CA>
        <CB N="NODE1">
            <CC R="A" />
        </CB>
        <CB N="NODE2">
            <CC R="A"/>
            <CC R="B"/>
        </CB>
        <CB N="NODE3">
            <CC R="B"/>
        </CB>
    </CA>
</main>

I want to xpath extract CB nodes that contain only the @R = 'A' attribute excluding @R = 'B'. According to the xml example I would like only the CB NODE1 node to be extracted. I tried the following xpath expression, but NODE1 and NODE2 are returned:

main/CA/CB[CC/@R='A' and CC/@R!='B']

How can I do? Thanks in advance.

CodePudding user response:

Try changing your xpath to

//CB[.//CC/@R="A"][not(.//CC/@R="B")]

and see if it works.

  • Related