I am stuck with a problem using XQuery to return common elements based on a tag. I am trying to write a XQ function which returns the common elements. For example I have an XML as: $schoolA
<schoolA>
<name>ABC</name>
<students>1000</students>
<classes>25</classes>
</schoolA>
<schoolA>
<name>DEF</name>
<students>1200</students>
<classes>20</classes>
</schoolA>
<schoolA>
<name>GHY</name>
<students>900</students>
<classes>30</classes>
</schoolA>
The other one as: $schoolB
<schoolB>
<name>ABC</name>
<students>1000</students>
<classes>25</classes>
</schoolB>
<schoolB>
<name>DEF</name>
<students>1200</students>
<classes>20</classes>
</schoolB>
<schoolB>
<name>XYZ</name>
<students>1100</students>
<classes>30</classes>
</schoolB>
The function should return: $commonSchool
<schoolA>
<name>ABC</name>
<students>1000</students>
<classes>25</classes>
</schoolA>
<schoolA>
<name>DEF</name>
<students>1200</students>
<classes>20</classes>
</schoolA>
The matching factor is the name. I am trying to write a nested for loop, but stuck appending the element to a variable. Any clues on how to do it would be great!