5

I want to use Group By in xquery. Can someone tell me please how to use Group By in Marklogic ?

4

4 回答 4

4

xdmp:xslt-invoke或者,您可以使用或调用 XSLT xdmp:xslt-eval。MarkLogic 的 XSLT 处理器支持 XSLT 2.0,其中包括对<xsl:for-each-group>.

于 2012-04-23T16:05:13.707 回答
4

简短的回答是使用map:map. 有关文档,请参阅http://docs.marklogic.com/map:map ,有关更长时间的讨论,请参阅http://blakeley.com/blogofile/archives/560/ 。

于 2012-04-23T14:43:48.660 回答
1
xquery version "1.0-ml";
let $xml:= <Students>
    <Student Country="England" Name="Dan" Age="20" Class="C"/>
    <Student Country="England" Name="Maria" Age="20" Class="B" />
    <Student Country="Australia" Name="Mark" Age="22" Class="A" />
  </Students>

let $xsl:= <xsl:stylesheet version="2.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:template match="Students">
  <result>
    <xsl:for-each-group select="Student" group-by="@Country">
    <country>
      <xsl:attribute name="name"><xsl:value-of select="fn:current-grouping-key()"/></xsl:attribute>
      <xsl:for-each select="fn:current-group()/@Name">
        <name><xsl:value-of select="."/></name>     
      </xsl:for-each>
    </country>
    </xsl:for-each-group>
  </result>
</xsl:template>
</xsl:stylesheet>

return xdmp:xslt-eval($xsl,$xml)
于 2016-03-09T21:58:21.520 回答
0

MarkLogic 涵盖了 XQuery 3.0 的部分内容(及其 1.0-ml 方言),但遗憾的是缺乏 FLWOR group by 支持。

但是,您仍然可以通过类似的语法以编程方式创建组,这将获得相同的结果。这是一个 XQuery 示例:

for $d in distinct-values(doc("order.xml")//item/@dept)
let $items := doc("order.xml")//item[@dept = $d]
order by $d
return <department code="{$d}">{
         for $i in $items
         order by $i/@num
         return $i
       }</department>

高温高压

于 2012-08-31T17:21:21.127 回答