<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" encoding="UTF-8"
media-type="text/xml"/>
<xsl:template match="/">
<rules>
<xsl:for-each select="//nt">
  <rule>
    <lhs>
      <cat>
	<xsl:value-of select="@cat"/>
      </cat>
      <xsl:variable name="curEdge">
	<xsl:value-of select="@id"/>
      </xsl:variable>
      <xsl:if test="../nt/edge[@idref=$curEdge]/@label">
      <role>
	<xsl:value-of select="../nt/edge[@idref=$curEdge]/@label"/>
      </role>
      </xsl:if>
    </lhs>
    <rhs>
     <xsl:for-each select="edge">
     <xsl:sort select="@lc"/>
      <xsl:variable name="curID">
	<xsl:value-of select="@idref"/>
      </xsl:variable>
      <rhsitem>
	<cat>
	  <!-- only one of these will ever expand -->
	  <xsl:value-of select="../../../terminals/t[@id=$curID]/@pos"/>
	  <xsl:value-of select="../../nt[@id=$curID]/@cat"/>
	</cat>
	<role>
	  <xsl:value-of select="@label"/>
	</role>
	<xsl:if test="../../../terminals/t[@id=$curID]/@word">
	<lex>
	  <xsl:value-of select="../../../terminals/t[@id=$curID]/@word"/>
	</lex>
	</xsl:if>
	</rhsitem>
       </xsl:for-each>
    </rhs>
  </rule>
</xsl:for-each>
</rules>
</xsl:template>
</xsl:stylesheet>

