Xquery - 计算具有特定属性的特定元素的数量
Xquery - count the number of specific elements with specific attributes
标题中的问题有点难以解释,所以这里有一个具体的xml文件示例以及代码应该是什么return:
<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 object 种类型:Brich、Circle 和 Cross,以及 3 to'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>
基本上,对于每种类型,我想获取右、下、左和上的子元素以及它们在该 object 类型中出现的次数。我知道由于存在限制,我只能 hard-code 每个计数并有一堆 let 语句,但我希望有人可以建议更好的方法来做到这一点。
如果对象类型和方向相当静态并且您有很多对象,那么您最好列出已知值,而不是使用 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)
...
这是一个完全动态的版本(没有硬编码),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>
标题中的问题有点难以解释,所以这里有一个具体的xml文件示例以及代码应该是什么return:
<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 object 种类型:Brich、Circle 和 Cross,以及 3 to'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>
基本上,对于每种类型,我想获取右、下、左和上的子元素以及它们在该 object 类型中出现的次数。我知道由于存在限制,我只能 hard-code 每个计数并有一堆 let 语句,但我希望有人可以建议更好的方法来做到这一点。
如果对象类型和方向相当静态并且您有很多对象,那么您最好列出已知值,而不是使用 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)
...
这是一个完全动态的版本(没有硬编码),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>