[Solved] Sibling as Child


In XSLT 2.0, you could have probably made use of xsl:for-each-group, using its group-starting-with attribute. In XSLT 1.0 though, this can be achieved by use of keys. You can define a key to look up the Loop-2000B elements by their first preceding Loop-2000A element

<xsl:key name="B" match="ex:Loop-2000B" use="generate-id(preceding-sibling::ex:Loop-2000A[1])" />

Similarlym you could do the same for associating Loop-2000C elements by their first preceding Loop-2000B element (Although this may not be necessary if you say there can be at most only one Loop-2000C element for each Loop-2000B

<xsl:key name="C" match="ex:Loop-2000C" use="generate-id(preceding-sibling::ex:Loop-2000B[1])" />

The generate-id function is an XSLT function that can be used to generate a unique id for each node (If one of their existing child elements could uniquely identify the element, you could also use that). Note the use of the ex: prefix is because all the elements in your XML are in a namespace, so XSLT needs to know which namespaces elements are in.

In your XSLT you would start off by selecting just the Loop-2000A elements.

<xsl:apply-templates select="ex:Loop-2000A" />

Then, within the template that matches ex:Loop-2000A you would select all the associated ex:Loop-2000B elements using the key

<xsl:apply-templates select="key('B', generate-id(current()))" />

You would take a similar approach for getting the ex:Loop-2000C in the template that matches ex:Loop-2000B. Although, if there was ever only going to be at most one such ex:Loop-2000C element, you could do this and not worry about the key in this case:

<xsl:apply-templates select="following-sibling::*[1][self::ex:Loop-2000C]" />

This gets the very following sibling element, but only if it is a Loop-2000C element.

Finally, other existing elements can be matched and copied with the Identity Transform

<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template> 

Try this XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
     xmlns:ex="http://www.example.org">

  <xsl:output method="xml" omit-xml-declaration="yes" indent="yes" />
  <xsl:key name="B" match="ex:Loop-2000B" use="generate-id(preceding-sibling::ex:Loop-2000A[1])" />
  <xsl:key name="C" match="ex:Loop-2000C" use="generate-id(preceding-sibling::ex:Loop-2000B[1])" />

  <xsl:template match="*[ex:Loop-2000A]">
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:apply-templates select="ex:Loop-2000A" />
      <xsl:apply-templates select="*[not(self::ex:Loop-2000A)][not(self::ex:Loop-2000B)][not(self::ex:Loop-2000C)]" />
    </xsl:copy>
  </xsl:template>

  <xsl:template match="ex:Loop-2000A">
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:apply-templates select="key('B', generate-id(current()))" />
      <xsl:apply-templates />
    </xsl:copy>
  </xsl:template>

  <xsl:template match="ex:Loop-2000B">
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:apply-templates select="key('C', generate-id(current()))" />
      <xsl:apply-templates />
    </xsl:copy>
  </xsl:template>

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

2

solved Sibling as Child