Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
XQuery XML中从子级到上父级的简单递归查询_Xml_Recursion_Xquery_Parent Child_Hierarchy - Fatal编程技术网

XQuery XML中从子级到上父级的简单递归查询

XQuery XML中从子级到上父级的简单递归查询,xml,recursion,xquery,parent-child,hierarchy,Xml,Recursion,Xquery,Parent Child,Hierarchy,输入xml id1 名称1 母亲1 年龄1 id2 姓名2 母亲2 年龄2 id4 母亲3 母亲4 id3 母亲2 母亲3 3岁 您能否帮助我如何为每个“请求/家庭/人员”获取具有现有元素“年龄”的顶级人员 我的跟踪xquery declare function local:recons($family作为元素(*),$person作为元素(*) 作为元素(*){ let$parrent:=在$family/person中为$p 其中$p/name=$person/mother 返回本地:侦

输入xml


id1
名称1
母亲1
年龄1
id2
姓名2
母亲2
年龄2
id4
母亲3
母亲4
id3
母亲2
母亲3
3岁
您能否帮助我如何为每个“请求/家庭/人员”获取具有现有元素“年龄”的顶级人员

我的跟踪xquery

declare function local:recons($family作为元素(*),$person作为元素(*)
作为元素(*){
let$parrent:=在$family/person中为$p
其中$p/name=$person/mother
返回本地:侦察(家庭,$p)
返回
{$person/*}
{$parrent}
};
声明函数xf:MyTest($inputXML作为元素(*))
作为元素(*){
{
对于$inputXML/family/person中的$person
返回本地:侦察($inputXML/家庭,$person)
}
};
将变量$inputXML声明为外部元素(*);
xf:MyTest($inputXML)
预期结果


...
id2
姓名2
母亲2
年龄2
id3
母亲2
母亲3
3岁
...
实际结果


...
id2
姓名2
母亲2
年龄2
id3
母亲2
母亲3
3岁
id4
母亲3
母亲4
...
我尝试使用祖先和xpath,比如“$parrent//person[fn:exists(age)]”,但没有成功。 我尝试使用祖先和xpath,比如“$parrent//person[fn:exists(age)]”,但没有成功


调整
where
子句your
local:recons()
,以确保您仅选择
$parrent
(该元素和变量是否应拼写为parent?),前提是它具有
年龄
,并且
名称
$person/mother
匹配

您可以使用谓词过滤器轻松地做到这一点:

where $p[age]/name = $person/mother    
应用于函数:

declare function local:recons($family as element(*), $person as element(*))
    as element(*) {
    let $parrent := for $p in $family/person
      where $p[age]/name = $person/mother
      return local:recons($family,$p)
    return
      <person>
        {$person/*}
        <parrent>{$parrent}</parrent>
      </person>
};
declare function local:recons($family作为元素(*),$person作为元素(*)
作为元素(*){
let$parrent:=在$family/person中为$p
其中$p[age]/name=$person/mother
返回本地:侦察(家庭,$p)
返回
{$person/*}
{$parrent}
};

我不确定我是否掌握了年龄相关的问题,您也没有详细说明完整的结果。上的结果是否代表您想要的结果?我在那里使用的实现方法有点不同,因为递归函数只是从输入返回现有元素,而不是创建新的元素,但是由于您只需要递归调用中的最后一个结果,我认为稍后比较结果更容易。Mads,感谢您的反馈。我试着做这个谓词,这个解决方案很好地帮助找到所有具有person现有“age”的parrent。但我想找到最上面/最后一个有“年龄”的人。预期结果res/家庭/人员|上(上)位仅限实际结果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>
      </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>
where $p[age]/name = $person/mother    
declare function local:recons($family as element(*), $person as element(*))
    as element(*) {
    let $parrent := for $p in $family/person
      where $p[age]/name = $person/mother
      return local:recons($family,$p)
    return
      <person>
        {$person/*}
        <parrent>{$parrent}</parrent>
      </person>
};