<?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="/command">
    <xsl:processing-instruction name="DOCTYPE">
    COMPLETION SYSTEM "completion.dtd"
    </xsl:processing-instruction>
    <completion version='.02'>
    <meta>
    <filename>_<xsl:value-of select="@command"/></filename>
    <xsl:variable name="include_file">
        <xsl:choose>
        <xsl:when test="@include"><xsl:value-of select="@include"/></xsl:when>
    <xsl:otherwise><xsl:value-of select="@command"/>_defines.xml</xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
    <includes>
        <include file="{$include_file}"/>
    </includes>
    </meta>
       <state_prolog/>
       <services>
        <service name='{@command}'>
            <arguments>
                <xsl:for-each select="arg">
                    <argoption>
                        <name><xsl:value-of select="@name"/></name>
                    </argoption>
                </xsl:for-each>
            </arguments>
            <argspecs>
                <xsl:for-each select="flag">
                    <argspec>
                        <optspec>
                                <xsl:for-each select="exclusion_list|same_word">
                                    <xsl:copy-of select="."/>
                                </xsl:for-each>
                            <flagarg name="{@name}">
                                <xsl:for-each select="more_than_once|plus_sign">
                                    <xsl:copy-of select="."/>
                                </xsl:for-each>

                                <explanation>
                                    <xsl:value-of select="@flaghelp"/>
                                </explanation>
                            </flagarg>
                        </optspec>
                        <xsl:if test="@taghelp != '' or @argtag != '' or ./action">
                            <description>
                                <message>
                                        <xsl:value-of select="@taghelp"/>
                                </message>
                                <xsl:choose>
                                    <xsl:when test="./action">
                                        <xsl:copy-of select="./action"/>
                                    </xsl:when>
                                    <xsl:when test="@argtag">
                                        <action tag="{@argtag}">
                                        </action>
                                    </xsl:when>
                                    <xsl:otherwise/>
                                </xsl:choose>
                            </description>
                        </xsl:if>
                    </argspec>
                </xsl:for-each>
                <xsl:for-each select="arg">
                </xsl:for-each>
            </argspecs>
        </service>
    </services>
    <xsl:variable name="states">
        <xsl:for-each select="flag|arg">
        </xsl:for-each>
    </xsl:variable>
    <xsl:if test="$states != ''">
        <xsl:value-of select="$states"/>
    </xsl:if>
    </completion>

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

<xsl:template match="flag">
    <!-- Do per flag stuff here -->
</xsl:template>

<xsl:template match="arg">
    <!-- Do per arg stuff here -->
</xsl:template>
</xsl:stylesheet>