How to use Group By in Marklogic?

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

+5
source share
4 answers

The short answer is to use map:map. See http://docs.marklogic.com/map:map for documentation and http://blakeley.com/blogofile/archives/560/ for a longer discussion.

+4
source

Alternatively, you can access XSLT with xdmp:xslt-invokeor xdmp:xslt-eval. The MarkLogic XSLT processor supports XSLT 2.0, which includes full support <xsl:for-each-group>.

+4
source
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)
+1

MarkLogic XQuery 3.0 ( 1.0 ), , , FLWOR .

, , . 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>

0
source

All Articles