【发布时间】:2019-08-29 00:43:00
【问题描述】:
输入xml
<req>
<family>
<person>
<id>id1</id>
<name>name1</name>
<mother>mother1</mother>
<age>age1</age>
</person>
<person>
<id>id2</id>
<name>name2</name>
<mother>mother2</mother>
<age>age2</age>
</person>
<person>
<id>id4</id>
<name>mother3</name>
<mother>mother4</mother>
</person>
<person>
<id>id3</id>
<name>mother2</name>
<mother>mother3</mother>
<age>age3</age>
</person>
</family>
</req>
您能否帮助我如何为每个“请求/家庭/人”获取具有现有元素“年龄”的顶级父母“人”?
我的关注 xquery
declare function local:recons($family as element(*), $person as element(*))
as element(*) {
let $parrent := for $p in $family/person
where $p/name=$person/mother
return local:recons($family,$p)
return
<person>
{$person/*}
<parrent>{$parrent}</parrent>
</person>
};
declare function xf:MyTest($inputXML as element(*))
as element(*) {
<res>
<family>
{
for $person in $inputXML/family/person
return local:recons($inputXML/family,$person)
}
</family>
</res>
};
declare variable $inputXML as element(*) external;
xf:MyTest($inputXML)
预期结果
<res>
<family>
...
<person>
<id>id2</id>
<name>name2</name>
<mother>mother2</mother>
<age>age2</age>
<parrent>
<person>
<id>id3</id>
<name>mother2</name>
<mother>mother3</mother>
<age>age3</age>
<parrent/>
</person>
</parrent>
</person>
...
</family>
</res>
实际结果
<res>
<family>
...
<person>
<id>id2</id>
<name>name2</name>
<mother>mother2</mother>
<age>age2</age>
<parrent>
<person>
<id>id3</id>
<name>mother2</name>
<mother>mother3</mother>
<age>age3</age>
<parrent>
<person>
<id>id4</id>
<name>mother3</name>
<mother>mother4</mother>
<parrent/>
</person>
</parrent>
</person>
</parrent>
</person>
...
</family>
</res>
我尝试使用诸如'$parrent//person[fn:exists(age)]'之类的祖先和xpath,但不成功。 我尝试使用祖先和 xpath 之类的 '$parrent//person[fn:exists(age)]',但不成功。
【问题讨论】:
-
我不确定我是否掌握了
age与何处相关的问题,并且您还没有拼出完整的结果。 xqueryfiddle.liberty-development.net/pPgCcox/3 的结果是否代表您想要的结果?我在那里使用的实现方法有点不同,因为递归函数只是从输入中返回现有元素而不是创建新元素,但是由于您只想要递归调用中的最后一个结果,我认为在稍后出结果。 -
疯子,感谢您的反馈。我试图做这个谓词,这个解决方案有助于找到所有具有现有“年龄”的人的父母。但我想为人找到现有“年龄”的最高/最后一位父母。预期结果 res/family/person | top(last) parent only 实际结果 res/family/person |第一父母|第二个父母... |顶部(最后)父母
标签: xml recursion xquery parent-child hierarchy