Re: [xsl] Fwd: Grouping siblings

Subject: Re: [xsl] Fwd: Grouping siblings
From: "Martin Honnen martin.honnen@xxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 13 Apr 2023 12:57:34 -0000
Am 4/13/2023 um 2:44 PM schrieb nick public nickpubl@xxxxxxxxx:
> Hi guys.
> I have a problem transforming this structure into one another in which
> a tag <rset> contains all the tags and own children
> HEADER, ORDER, PARCEL, PARLINE until the next HEADER tag:
> *<rset>
> B  <HEADER>...</HEADER> // 1:1
> B  <ORDER>...</ORDER> // 1:n
> B  <PARCEL>...</PARCEL> // 1:n
> B  <PARLINE>...</PARLINE> // 1:n
> </rset>*
>
> The source structure can be, for example, like this
> *<rset>
> B  <HEADER>...</HEADER>
> B  <ORDER>...</ORDER>
> B  <PARCEL>...</PARCEL>
> B  <PARLINE>...</PARLINE>
> </rset>
> <rset>
> B  <ORDER>...</ORDER>
> B  <PARCEL>...</PARCEL>
> B  <PARLINE>...</PARLINE>
> </rset>
> <rset>
> B  <PARCEL>...</PARCEL>
> B  <PARLINE>...</PARLINE>
> </rset>
> <rset>
> B  <HEADER>...</HEADER>
> B  <ORDER>...</ORDER>
> B  <PARCEL>...</PARCEL>
> B  <PARLINE>...</PARLINE>
> </rset>
> <rset>
> B  <ORDER>...</ORDER>
> B  <PARCEL>...</PARCEL>
> B  <PARLINE>...</PARLINE>
> B  <PARLINE>...</PARLINE>
> B  <PARCEL>...</PARCEL>
> B  <PARLINE>...</PARLINE>
> </rset>
> <rset>
> B  <PARCEL>...</PARCEL>
> B  <PARLINE>...</PARLINE>
> </rset>*
>
> I tried to remove all the <rset> tags with this
>
> *<xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
> B  B  xmlns:msxsl="urn:schemas-microsoft-com:xslt"
> exclude-result-prefixes="msxsl"
> B  B  xmlns:ns1="urn:my.com:MM:COMET:ASN_LX"
> B  B  xmlns:ns0="urn:my.com:MM:COMET:ASN_LX">
> B  <xsl:output method="xml" indent="yes"/>
>
> *
> *B  <xsl:template match="*">
> B  B  <ns1:MT_ASN_LX_JM >
> B  B  B  <xsl:apply-templates select="rset"/>
> B  B  </ns1:MT_ASN_LX_JM>
> B  </xsl:template>
> B  <xsl:template match="rset">
> B  B  <xsl:copy-of select="*"/>
> B  </xsl:template>
> </xsl:stylesheet>*
>
> to obtain this
>
> *<HEADER>...</HEADER>
> B  <ORDER>...</ORDER>
> B  <PARCEL>...</PARCEL>
> B  <PARLINE>...</PARLINE>
> B  <ORDER>...</ORDER>
> B  <PARCEL>...</PARCEL>
> B  <PARLINE>...</PARLINE>
> B  <PARCEL>...</PARCEL>
> B  <PARLINE>...</PARLINE>
> B  <HEADER>...</HEADER>
> B  <ORDER>...</ORDER>
> B  <PARCEL>...</PARCEL>
> B  <PARLINE>...</PARLINE>
> B  <ORDER>...</ORDER>
> B  <PARCEL>...</PARCEL>
> B  <PARLINE>...</PARLINE>
> B  <PARLINE>...</PARLINE>
> B  <PARCEL>...</PARCEL>
> B  <PARLINE>...</PARLINE>
> B  <PARCEL>...</PARCEL>
> B  <PARLINE>...</PARLINE>
> *
> At this point, I have a problem grouping the HEADER tags and siblings
> in a <rset> tag until the next HEADER one.
> I don't know how to use the following-sibling / preceding-sibling
> statements because all tags are siblings.
> Is it possible to solve this in *XSLT 1.0*?


It sounds like an easy task for XSLT 2 or later with e.g.

 B  <xsl:for-each-group select="*" group-starting-with="HEADER">


XSLT 2 or 3 is available on various platforms using Saxon Java, SaxonC,
SaxonCS or SaxonJS.

With XSLT 1.0 you can try to key the siblings that are not HEADERs on
the generated id of the preceding HEADER sibling e.g.

 B  <xsl:key name="sib-group"
match="container-element-name/*[not(self::HEADER)]"
use="generate-id(preceding-sibling::HEADER[1])"/>

and then you process e.g. <xsl:apply-templates select="HEADER"/> and in
the template for that element you can pull in e.g.

 B  <xsl:template match="HEADER">

 B B B  <rset>

 B B B B B  <xsl:copy-of select=". | key('sib-group', generate-id())"/>

 B B B  </rset>

 B  </xsl:template>

Current Thread