2012-04-23 13 views
5

Voglio usare Group By in xquery. Qualcuno può dirmi come usare Group By in Marklogic?Come usare Group By in Marklogic?

+1

non hanno utilizzato Mark Logic. Ma per favore date un'occhiata a http://blakeley.com/blogofile/archives/560/ –

+0

La versione di MarkLogic che state usando supporta la bozza XQuery 3.0? (È solo a partire dal 3.0 che il supporto 'group by' è stato aggiunto alle espressioni FLWOR: prima di quel punto, devi farlo tu stesso). Un riassunto di 'group by' in XQuery 3.0 può essere trovato su http://docs.basex.org/wiki/XQuery_3.0#Group_By –

risposta

4

In alternativa, è possibile chiamare XSLT utilizzando xdmp:xslt-invoke o xdmp:xslt-eval. Il processore XSLT di MarkLogic supporta XSLT 2.0, che include il supporto completo per <xsl:for-each-group>.

+0

Vedere la risposta di @mg_kedzie per un esempio .. – grtjn

0

MarkLogic copre parti di XQuery 3.0 (con il suo dialetto da 1,0 ml), ma sfortunatamente manca il gruppo FLWOR per supporto.

Tuttavia, è comunque possibile creare una sintassi di gruppo simile a quella che otterrà gli stessi risultati. Ecco un esempio 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> 

HTH

+1

MarkLogic 5 non supporta raggruppa per. – wst

+0

È possibile velocizzare questo approccio utilizzando 'cts: values' (anziché valori distinti) e' cts: search' (anziché i predicati XPath), in particolare se è possibile memorizzare gli articoli come file separati. – grtjn

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)