[xquery-talk] Group starting with in XQuery?

Geert Josten geert.josten at dayon.nl
Wed Nov 20 05:22:10 PST 2013


Thnx Michael,

Found a tumbling example, and fiddled until I had this:

xquery version "3.0";

let $xml :=
    <Docs>
        <Doc>
            <P>blah blah blah<pg>1</pg>blah blah</P>
            <P>blah blah blah<pg>2</pg>blah blah</P>
        </Doc>
        <Doc>
            <P>blah blah blah<pg>3</pg>blah blah</P>
            <P>blah blah blah<pg>4</pg>blah blah</P>
        </Doc>
    </Docs>
return
    <Docs>{
        for $doc in $xml/Doc
        return
            <Doc>{
                for $P in $doc/P
                return
                    for tumbling window $w in $P/node()
                    start when true()
                    end next $e
                        when $e instance of element(pg)
                    return (
                        $w[self::pg],
                        <P>{
                            $w[not(self::pg)]
                        }</P>
                    )
            }</Doc>
    }</Docs>

Tested with http://try.zorba.io .. :)

Cheers,
Geert

> -----Oorspronkelijk bericht-----
> Van: Michael Kay [mailto:mike at saxonica.com]
> Verzonden: woensdag 20 november 2013 1:09
> Aan: Geert Josten
> CC: talk at x-query.com Talk
> Onderwerp: Re: [xquery-talk] Group starting with in XQuery?
>
> If you're asking about XQuery 1.0, the answer is no, unless you regard
the
> solution using recursion as elegant.
>
> In XQuery 3.0 there are "tumbling windows" which give you the capability
of
> XSLT's group-starting-with, and a lot more besides.
>
> Michael Kay
> Saxonica
>
> On 19 Nov 2013, at 18:47, Geert Josten <geert.josten at dayon.nl> wrote:
>
> > Just wondering, is there an elegant way of doing something like this
in
> > XQuery?
> >
> >    <xsl:stylesheet version="2.0"
> > xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
> > 		<xsl:output indent="yes"/>
> > 		<xsl:strip-space elements="*"/>
> >
> > 		<xsl:template match="@*|node()">
> > 			<xsl:copy>
> > 				<xsl:apply-templates select="@*|node()"/>
> > 			</xsl:copy>
> > 		</xsl:template>
> >
> > 		<xsl:template match="P">
> > 			<xsl:variable name="P" select="."/>
> > 			<xsl:for-each-group select="node()"
> > group-starting-with="pg">
> > 				<xsl:apply-templates select="self::pg"/>
> > 				<xsl:element name="{node-name($P)}">
> > 					<xsl:apply-templates
> > select="$P/@*|current-group()[not(self::pg)]"/>
> > 				</xsl:element>
> > 			</xsl:for-each-group>
> > 		</xsl:template>
> >
> >    </xsl:stylesheet>
> >
> > Example input:
> >
> > <Docs>
> >    <Doc>
> >        <P>blah blah blah<pg>1</pg>blah blah</P>
> >        <P>blah blah blah<pg>2</pg>blah blah</P>
> >    </Doc>
> >    <Doc>
> >        <P>blah blah blah<pg>3</pg>blah blah</P>
> >        <P>blah blah blah<pg>4</pg>blah blah</P>
> >    </Doc>
> > </Docs>
> >
> > Cheers,
> > Geert
> >
> > M.Sc. G.P.H. (Geert) Josten
> > Senior Developer
> >
> >
> > Dayon B.V.
> > Delftechpark 37b
> > 2628 XJ Delft
> > The Netherlands
> >
> > T +31 (0)88 26 82 570
> >
> > geert.josten at dayon.nl
> > www.dayon.nl
> >
> > De informatie - verzonden in of met dit e-mailbericht - is afkomstig
van
> > Dayon BV en is uitsluitend bestemd voor de geadresseerde. Indien u dit
> > bericht onbedoeld hebt ontvangen, verzoeken wij u het te verwijderen.
> Aan
> > dit bericht kunnen geen rechten worden ontleend.
> > _______________________________________________
> > talk at x-query.com
> > http://x-query.com/mailman/listinfo/talk


More information about the talk mailing list