我有xml文档,如:(这可以嵌套多次)
<?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>
我正在提供原始XML的简短形式。 xml可以是任意深度。我必须找到关系。 (1)兄弟关系(2)侄子侄子关系。 (巴黎acutally)
我的兄弟巴黎:
<sibling>Sabrina,ALfonso</sibling> <sibling>Dolores,Ahmed</sibling>
<sibling>Dolores,Nusrat</sibling> <sibling>Ahmed,Nusrat</sibling>
<sibling>Perdita,Ioan</sibling>
这不应该是艰难的。但是对于Aunt-Nephew对,我们也必须检查性别。 阿姨 - 侄子对输出
<AuntNeph>Sabrina,Ioan</AuntNeph>
感谢。
答案 0 :(得分:1)
对于每个person
,请检查following-sibling
轴以查找其他人(兄弟姐妹)。只检查一种方式(不是后面和前面的)以省略重复的兄弟对(例如“Sabina Alfonso”和“Alfonso Sabina”)。
for $person in //person
for $sibling in $person/following-sibling::person
return element sibling { string-join(($person, $sibling)/@name, ',') }
对于侄子,查询非常相似,我认为不需要进一步解释:
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, ',') }