How to use Group By in Marklogic?

风流意气都作罢 提交于 2019-12-06 03:41:50

问题


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


回答1:


Alternatively, you could call out to XSLT using xdmp:xslt-invoke or xdmp:xslt-eval. MarkLogic's XSLT processor supports XSLT 2.0, which includes full support for <xsl:for-each-group>.




回答2:


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.




回答3:


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)



回答4:


MarkLogic covers parts of XQuery 3.0 (with its 1.0-ml dialect), but unfortunately FLWOR group by support is lacking.

However, you can still programmatically create group by like syntax which will achieve the same results. Here is an XQuery example:

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>

HTH



来源:https://stackoverflow.com/questions/10276436/how-to-use-group-by-in-marklogic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!