Xquery - 计算具有特定属性的特定元素的数量

时间:2016-04-05 20:21:52

标签: xpath xquery

有点难以解释标题中的问题,所以这里是一个特定的xml文件示例以及代码应返回的内容:

<objects>
    <object type='Brick' to='Up' />
    <object type='Cross' to='Down' />
    <object type='Brick' to='Left' />
    <object type='Brick' to='Up' />
    <object type='Circle' to='Right' />
    <object type='Circle' to='Right' />
</objects>

所以我有3种对象类型:Brich,Circle和Cross,以及3到&#39; s,Up,Down,Left和Right。我想使用xquery来获得类似的东西:

<objects>
    <object type="Brick">
        <where to="Up" count="2" />
        <where to="Down" count="0" />
        <where to="Left" count="1" />
        <where to="Right" count="2" />
    </object>
    <object type="Cross">
     .
     .
     .
</objects> 

基本上,对于每种类型,我想获得右,下,左和上的子元素,以及它们为该对象类型出现的次数。我知道,因为有限制,我可以硬编码每一个计数并有一堆let语句,但我希望有人能提出一个更好的方法来做到这一点。

2 个答案:

答案 0 :(得分:3)

这是一个完全动态的版本(没有硬编码),distinct-values(...)是你在XQuery 1.0中的朋友:

<objects>{
  let $directions := distinct-values(//@to)
  for $type in distinct-values(//object/@type)
  return <object type="{$type}">{
    for $to in $directions
    return <where to="{$to}" count="{count(//object[@type = $type and @to = $to])}"/>
  }</object>
}</objects>

答案 1 :(得分:0)

如果对象类型和方向是相当静态的并且你有很多对象,那么最好列出已知值,而不是使用distinct-values,这对于大型序列来说可能很慢。

let $object-types := ('Brick', 'Circle', 'Cross')
let $directions := ('Up', 'Down', 'Left', 'Right')
for $t in $object-types
return element object {
  attribute type { $t },
  for $d in $directions
  let $count := count($objects/object[@type = $t][@to = $d])
  return element where {
    attribute to { $d },
    attribute count { $count }
  }
}

但如果你不能提前知道输入值,你可以动态地构建这些值,如:

let $object-types := distinct-values($objects/object/@type)
...
相关问题