سؤال

I have xml document like:(where this can be nested as many times)

<?xml version = "1.0" encoding = "UTF -8"?>
<person xmlns:xsi = " http://www.w3.org/2001/XMLSchema-instance"
    name = "Adrian"
    gender = "male">
    <children>
        <person name = "Sabina" gender = "female">
            <children>
            <person name = "Dolores" gender = "female"/>
            <person name = "Ahmed" gender = "male"/>
            <person name = "Nusrat" gender = "female"/>
            </children>
        </person>
        <person name = "Alfonso" gender = "male">
            <children>
            <person gender = "female" name = "Perdita"/>
            <person gender = "male" name = "Ioan "> </person>
            </children>
        </person>
    </children>
</person>

I am giving a short form of original XML. The xml can be any arbitrary depth. I have to find to relations. (1) Sibling relation (2) Aunt Nephew relation. (paris acutally)

My sibling paris:

<sibling>Sabrina,ALfonso</sibling> <sibling>Dolores,Ahmed</sibling>
<sibling>Dolores,Nusrat</sibling> <sibling>Ahmed,Nusrat</sibling> 
<sibling>Perdita,Ioan</sibling>

It should not be tough. But in case of Aunt-Nephew pair, we have to check gender also. The Aunt-Nephew pairs output

<AuntNeph>Sabrina,Ioan</AuntNeph>

Thanks.

هل كانت مفيدة؟

المحلول

For each person, check the following-sibling axis for other persons (siblings). Only check one way (not both following and preceding) to omit duplicate sibling pairs (eg. "Sabina Alfonso" and "Alfonso Sabina").

for $person in //person
for $sibling in $person/following-sibling::person
return element sibling { string-join(($person, $sibling)/@name, ',') }

For the nephews the query is quite similar, I don't think it requires further explanation:

for $aunt in //person[@gender="female"]
for $nephew in $aunt/(preceding-sibling::person, following-sibling::person)/children/person[@gender="male"]
return element AuntNeph { string-join(($aunt, $nephew)/@name, ',') }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top