My XML structure is the following:
ROOT
   |_ SetOfBandC (*)
   |               |_ SetOfB (1)
   |               |           |_ ElementB (*)
   |               |           |_ ElementB__Key 
   |               |_ SetOfC (1)
   |                           |_ ElementC (*)
   |                           |_ ElementC__Key 
   |_ ElementD (*)
   |
   |_ SetOfBandC__Key
   |_ ElementD__Key
   |
   |_ ElementD->SetOfBandC__Keyref
   |_ ElementD->ElementB__Keyref
In this structure I can have multiple SetOfBandC as well as multiple ElementB and ElementC, but only 1 SetofB and SetOfC for each SetOfBandC. 
The problem is that ElementD has a keyref referencing a particular SetOfBandC and another one referencing a ElementB of that set, but the XML Validator when checking for the ElementD->ElementB__Keyref's validity it only searches in the last SetOfBandC, and not in all of them or, better, in the one referenced by ElementD->SetOfBandC__Keyref. 
So when ElementD->ElementB__Keyref references an ElementB which is not in the last SetOfBandC, validation won't work.
Here is my ElementD->ElementB__Keyref:
<xs:keyref name="ElementD->ElementB__Keyref" refer="tns:ElementB__Key ">
    <xs:selector xpath="tns:ElementD" />
    <xs:field xpath="@elementD_ref" />
</xs:keyref>
And my ElementB__Key:
<xs:key name="ElementB__Key">
    <xs:selector xpath="tns:ElementB" />
    <xs:field xpath="@elemB_name" />
</xs:key>
Where tns is my target namespace. What am I missing?
PS: names in my code are different and there are not -> in them.