<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">

  <xsl:output method="xml"/>

  <xsl:template match="/">
    <gxl>
      <graph id="G" edgeids="false" edgemode="directed">
        <xsl:apply-templates/>
      </graph>
    </gxl>
  </xsl:template>

  <xsl:template match="link[@name='reply']">
    <edge isdirected="true">
      <xsl:attribute name="to">
        <xsl:value-of  select="concat('M',@sourceid)"/>
      </xsl:attribute>
      <xsl:attribute name="from">
        <xsl:value-of  select="concat('M',@destid)"/>
      </xsl:attribute>
    </edge>
  </xsl:template>

  <xsl:template match="item[item[attribute[@name='message_id']]]">
    <xsl:choose>
      <xsl:when test="attribute[@name='Name' and text()='Messages']">
        <xsl:apply-templates/>
      </xsl:when>
      <xsl:otherwise>
        <node >
          <xsl:attribute name="id">
            <xsl:value-of  select="concat('N',@ID)"/>
          </xsl:attribute>
          <graph edgeids="false" edgemode="directed">
            <xsl:attribute name="id">
              <xsl:value-of  select="concat('cluster_G',@ID)"/>
            </xsl:attribute>
            <attr name="color" >
              <string>black</string>
            </attr>
            <attr name="style" >
              <string>dashed</string>
            </attr>
            <xsl:apply-templates/>
          </graph>
        </node>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="item[attribute[@name='message_id']]">
    <node>
      <xsl:attribute name="id">
        <xsl:value-of  select="concat('M',@ID)"/>
      </xsl:attribute>
      <attr name="label" >
        <string><xsl:value-of select="attribute[@name='Name']/text()"/></string>
      </attr>
    </node>
  </xsl:template>

  <xsl:template match="text()"/>
  <xsl:template match="*">
    <xsl:apply-templates/>
  </xsl:template>
</xsl:stylesheet>

