<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>

<!-- Start -->
<xsl:template match="/">
   <xsl:apply-templates select="*"/>
</xsl:template>

<xsl:template match="subcommand">
  <xsl:copy>
    <xsl:copy-of select="@*"/>
      <xsl:call-template name="AddFlagList">
        <xsl:with-param name="root" select="."/>
      </xsl:call-template>
    <contents>
      <xsl:choose>
        <xsl:when test="./or">
          <xsl:call-template name="handleOr"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:apply-templates select="*"/>
        </xsl:otherwise>
      </xsl:choose>
    </contents>
  </xsl:copy>
</xsl:template>

<xsl:template match="commands">
  <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates select="*"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="brace|flag|word|ellipses|const">
  <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:choose>
      <xsl:when test="./or">
        <xsl:call-template name="handleOr"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates select="*"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:copy>
</xsl:template>

<xsl:template name="handleOr">
        <xsl:variable name="here" select="."/>
        <xsl:variable name="firstor" select="generate-id($here/or[1])"/>
        <xsl:variable name="posfirstor">
                <xsl:for-each select="*">
                        <xsl:if test="$firstor = generate-id()">
                                <xsl:value-of select="position()"/>
                        </xsl:if>
                </xsl:for-each>
        </xsl:variable>
        <group>
          <or end="{$posfirstor}">
            <xsl:apply-templates select="*[position() &lt; $posfirstor]"/>
          </or>

          <xsl:for-each select="$here/or">
            <xsl:variable name="here2" select="."/>
            <xsl:variable name="here2position" select="position()"/>
            <xsl:variable name="posStartOr">
              <xsl:for-each select="$here/*">
                <xsl:if test="generate-id($here2) = generate-id()">
                  <xsl:value-of select="position()"/>
                </xsl:if>
              </xsl:for-each>
            </xsl:variable>

            <xsl:variable name="posNextOr">
              <xsl:choose>
                <xsl:when test="position()=last()">
                  <xsl:for-each select="$here/*">
                    <xsl:if test="position() = last()">
                      <xsl:value-of select="position()+1"/>
                    </xsl:if>
                  </xsl:for-each>
                </xsl:when>
                 <xsl:otherwise>
                   <xsl:for-each select="$here/*">
                     <xsl:if
               test="generate-id($here/or[$here2position+1]) =  generate-id()">
                       <xsl:value-of select="position()"/>
                     </xsl:if>
                   </xsl:for-each>
                 </xsl:otherwise>
               </xsl:choose>
            </xsl:variable>
            <or start="{$posStartOr}" end="{$posNextOr}">
              <xsl:apply-templates
 select="$here/*[$posStartOr &lt; position() and  $posNextOr &gt; position()]"/>
            </or>
          </xsl:for-each>
       </group>
</xsl:template>

<xsl:template name="AddFlagList">
    <xsl:param name="root"/>
    <flagset>
      <xsl:variable name="flags" select="$root/descendant::flag"/>
       <xsl:for-each select="$flags">
         <xsl:sort select="@name"/>
         <flag name="{@name}"/>
       </xsl:for-each>
    </flagset>
</xsl:template>

</xsl:stylesheet>