<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.1" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  
  xmlns:c="http://livestoryboard.com/lsb/schemas/2003/03/content" 
  xmlns="http://livestoryboard.com/lsb/schemas/2003/03/xhtml1">

  <xsl:output method="xml" indent="yes" encoding="utf-8"/>
  <xsl:strip-space elements="*"/>
  
  <xsl:template match="/">
  
    <xsl:apply-templates/>

  </xsl:template>

  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="c:section">
    <xsl:choose>
      <xsl:when test="not(boolean(child::c:section/c:section))">
        <xsl:variable name="cid" select="concat('c', position(), generate-id())"/>
        <xsl:document href="{concat('./conv_pieces/', $cid, '.xml')}">
          <c:article>
            <xsl:apply-templates/>
          </c:article>
        </xsl:document>
      </xsl:when>
      <xsl:otherwise>
        <xsl:copy>
          <xsl:copy-of select="@*"/>
          <xsl:apply-templates/>
        </xsl:copy>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  
</xsl:stylesheet>
