[xquery-talk] Group starting with in XQuery?

Geert Josten geert.josten at dayon.nl
Tue Nov 19 10:47:42 PST 2013


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.


More information about the talk mailing list