2015-02-10 98 views
1

我正在尝试在xml中找到每个元素的xpath并将其作为元素值进行提取。 我的源文件看起来像:MarkLogic:使用xquery获取元素的xpath

<root> 
<parent1> 
    <child1></child1> 
    <child2></child2> 
</parent1> 

<parent2> 
    <child1></child1> 
</parent2> 
</root> 

我想要的输出,如:

<root> 
<parent1> 
    <child1> /root/parent1/child1 </child1> 
    <child2> /root/parent1/child2 </child2> 
</parent1> 

<parent2> 
    <child1> /root/parent2/child1 </child1> 
</parent2> 
</root> 

我目前得到的输出为:

<root> 
<parent1> 
<child1> /root/parent1/child1 </child1> 
<child2> /root/parent1/child2 </child2> 
</parent1>" 

<parent2> 
<child1> /root/parent1/parent2/child1 </child1> 
</parent2> 
</root> 

我无法正确地遍历找到xpath。任何投入都是有价值的。

+0

显示代码,即使它是错误的,通常赞赏.. – grtjn 2015-02-10 12:24:41

回答

7

我会建议使用xdmp:path,也许是这样的:

declare function local:add-paths($nodes) { 
    for $node in $nodes 
    return 
    typeswitch ($node) 
    case element() 
     return element { node-name($node) } { 
     $node/@*, 
     if ($node/node()) then 
      local:add-paths($node/node()) 
     else 
     xdmp:path($node) 
     } 
    default 
     return $node 
}; 

let $xml := 
    <root> 
     <parent1> 
      <child1></child1> 
      <child2></child2> 
     </parent1> 

     <parent2> 
      <child1></child1> 
     </parent2> 
    </root> 
return local:add-paths($xml) 

HTH!

+0

非常感谢。它有帮助。 – kol 2015-02-11 03:34:16