【问题标题】:MarkLogic: Getting xpath of an element using xqueryMarkLogic:使用 xquery 获取元素的 xpath
【发布时间】:2015-02-10 11:52:28
【问题描述】:

我正在尝试查找 xml 中每个元素的 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。任何意见都是有价值的。

【问题讨论】:

  • 显示代码,即使它是错误的,通常也是值得赞赏的..

标签: xml xpath xquery marklogic


【解决方案1】:

我建议使用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!

【讨论】:

  • 非常感谢。它有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-01
  • 1970-01-01
  • 2016-07-26
  • 2022-01-20
相关资源
最近更新 更多