<?xml version="1.0" encoding="ISO-8859-1" ?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
	<xsl:apply-templates select="./menu/pasta" />
</xsl:template>

<xsl:template match="pag">
  <xsl:call-template name="item"/>
</xsl:template>

<xsl:template match="pasta">
  <xsl:if test="count(./*) &gt; 0">
	HM_Array<xsl:value-of select="@sn" />=[
	[<xsl:if test="local-name(..) = 'menu'">150,"HMpeterLeft(HMpeter_TDid<xsl:value-of select="@sn" />)","HMpeterTop(HMpeter_TDid<xsl:value-of select="@sn" />)",,,,,,,,,,,,,,,,,1,1</xsl:if>],
	<xsl:for-each select="pasta|pag">
		<xsl:sort select="local-name()" order="ascending"/>
		<xsl:sort data-type="number" select="@posicao" order="ascending"/>
		<xsl:sort data-type="number" select="@n" order="ascending"/>
		<xsl:call-template name="item"/>
	</xsl:for-each>
	];    	
  </xsl:if>
  <xsl:apply-templates select="./pasta" />
</xsl:template>


<xsl:template name="item">
  <xsl:variable name="aux"><xsl:choose><xsl:when test="count(./*) &gt; 0">1</xsl:when><xsl:otherwise>0</xsl:otherwise></xsl:choose></xsl:variable>
  <xsl:variable name="aux2"><xsl:choose>
		<xsl:when test="@id_pagina">id_pagina=<xsl:value-of select="@id_pagina"/>&amp;assoc=<xsl:value-of select="@assoc"/></xsl:when>
		<xsl:otherwise>id_pasta=<xsl:value-of select="@id_pasta"/></xsl:otherwise>
	</xsl:choose></xsl:variable>
  <xsl:if test="not( normalize-space(../text()) = normalize-space(./text()) )">
	["<xsl:if test="local-name(.) = 'pag'">_LI_ABRE_</xsl:if><xsl:value-of select="normalize-space(text())" />","_X_URL_X_?<xsl:value-of select="normalize-space($aux2)"/>",1,0,<xsl:value-of select="$aux"/>]
	<xsl:if test="not(position()=last())">,</xsl:if>
  </xsl:if>
</xsl:template>

</xsl:stylesheet>


