XPath / XQuery:计数节点元素并返回具有父ID的最小计数

问题描述 投票:1回答:1

我将举一个例子以更好地阐明自己。

示例:

<asdf>
    <parentnode id ="1">
           <childnode>
            ...
            </childnode>
            <childnode>
            ...
            </childnode>
    </parentnode>
    <parentnode id ="2">
            <childnode>
            ...
            </childnode>
            <childnode>
            ...
            </childnode>
            <childnode>
            ...
            </childnode>
    </parentnode>
    <parentnode id ="3">
            <childnode>
            ...
            </childnode>
    </parentnode>
</asdf>

我需要计算所有父节点的所有子节点,并用ID返回给定父节点的最小子节点数。

对于此示例,解决方案将是:

<parentnode id="3" amount="1"/>

我真的不知道从哪里开始。我是否必须在for循环中计算不同的元素,或者Xpath表达式也可以做到这一点?我不确定这是否是正确的方向:

let $a := fn:doc('asdf.xml')/asdf/*
 return 
for $z in distinct-values($a/name() = childnode )
 order by count($z) ascending
 return $z)[1]
xml xpath xsd xquery
1个回答
2
投票
  • parentnodecount()将那些childnode元素排序
© www.soinside.com 2019 - 2024. All rights reserved.