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

<xsl:template name="identity" match="node()">
  <!--
   ! This is an identity template in XSLT.
   | However, other templates with higer specificity
   | will override this where appropriate.
   +-->
  <xsl:copy>
    <xsl:for-each select="attribute::*">
      <xsl:copy/>
      </xsl:for-each>
    <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

<xsl:template match="list">
  <nonterm name="{@name}" epsilon="{string(@epsilon = 'true')}">
    <production>
      <xsl:call-template name="list-item-symbol"/>
      <xsl:call-template name="list-rlist-symbol"/>
      </production>
    </nonterm>
  <nonterm name="{concat(@name, '__rlist')}" epsilon="true">
    <production>
      <xsl:call-template name="list-rlist-symbol"/>
      <xsl:if test="delimiter">
        <xsl:call-template name="list-delim-symbol"/>
        </xsl:if>
      <xsl:call-template name="list-item-symbol"/>
      </production>
    </nonterm>
  <nonterm name="{concat(@name, '__item')}"
      epsilon="{string(@item_epsilon = 'true')}">
    <xsl:for-each select="production">
      <xsl:copy-of select="."/>
      </xsl:for-each>
    </nonterm>
  <xsl:if test="delimiter">
  <nonterm name="{concat(@name, '__delim')}"
      epsilon="{string(@delim_epsilon = 'true')}">
    <xsl:for-each select="delimiter">
      <production>
        <xsl:copy-of select="*"/>
        </production>
      </xsl:for-each>
    </nonterm>
    </xsl:if>
  </xsl:template>

<xsl:template name="list-rlist-symbol">
  <symbol action="content">
    <xsl:value-of select="concat(@name, '__rlist')"/>
    </symbol>
  </xsl:template>

<xsl:template name="list-item-symbol">
  <symbol>
    <xsl:if test="@item_name">
      <xsl:attribute name="rename">
        <xsl:value-of select="@item_name"/>
        </xsl:attribute>
      </xsl:if>
    <xsl:attribute name="action">
      <xsl:choose>
        <xsl:when test="@item_action">
          <xsl:value-of select="@item_action"/>
          </xsl:when>
        <xsl:when test="@item_name">preserve</xsl:when>
        <xsl:otherwise>content</xsl:otherwise>
        </xsl:choose>
      </xsl:attribute>
    <xsl:value-of select="concat(@name, '__item')"/>
    </symbol>
  </xsl:template>

<xsl:template name="list-delim-symbol">
  <symbol>
    <xsl:if test="@delim_name">
      <xsl:attribute name="rename">
        <xsl:value-of select="@delim_name"/>
        </xsl:attribute>
      </xsl:if>
    <xsl:attribute name="action">
      <xsl:choose>
        <xsl:when test="@delim_action">
          <xsl:value-of select="@delim_action"/>
          </xsl:when>
        <xsl:when test="@delim_name">preserve</xsl:when>
        <xsl:otherwise>drop</xsl:otherwise>
        </xsl:choose>
      </xsl:attribute>
    <xsl:value-of select="concat(@name, '__delim')"/>
    </symbol>
  </xsl:template>

</xsl:transform>

