<!DOCTYPE xsl:stylesheet [
<!--* 
<!DOCTYPE xsl:stylesheet PUBLIC 'http://www.w3.org/1999/XSL/Transform'
      '/SGML/Public/W3C/xslt10.dtd' [
*-->
<!ENTITY x-top-level-atts "
  xmlns:saxon CDATA #FIXED 'http://icl.com/saxon'
">

<!ENTITY % qname "NMTOKEN">
<!ENTITY % result-elements "">
<!ENTITY % non-xsl-instructions "| saxon:output">

<!--* I assume saxon:output gets same content as xsl:template *-->
<!--* content model should be %template; *-->
<!ELEMENT saxon:output  (#PCDATA
  | xsl:apply-templates
  | xsl:call-template
  | xsl:apply-imports
  | xsl:for-each
  | xsl:value-of
  | xsl:copy-of
  | xsl:number
  | xsl:choose
  | xsl:if
  | xsl:text
  | xsl:copy
  | xsl:variable
  | xsl:message
  | xsl:fallback
  | xsl:processing-instruction
  | xsl:comment
  | xsl:element
  | xsl:attribute
  | saxon:output)*
>
<!--* I have added only the file attribute; saxon:output has more *-->
<!ATTLIST xsl:output
  file CDATA #REQUIRED
  method NMTOKEN #IMPLIED
  version NMTOKEN #IMPLIED
  encoding CDATA #IMPLIED
  omit-xml-declaration (yes|no) #IMPLIED
  standalone (yes|no) #IMPLIED
  doctype-public CDATA #IMPLIED
  doctype-system CDATA #IMPLIED
  cdata-section-elements NMTOKENS #IMPLIED
  indent (yes|no) #IMPLIED
  media-type CDATA #IMPLIED
>

]>
<xsl:stylesheet version="1.0"
                xmlns:xsl =  "http://www.w3.org/1999/XSL/Transform"
                xmlns:saxon = 'http://icl.com/saxon'
		extension-element-prefixes="saxon">

 <xsl:output method="text"
             encoding="us-ascii"
	     indent="no"
 />

 <xsl:template match="text()"/>

 <xsl:template match="text()" mode="scrap">
  <!--*
  <xsl:message><xsl:text>   Text node </xsl:text>
   <xsl:value-of select="position()"/>
   <xsl:text>: </xsl:text>
   <xsl:call-template name="crush-newlines">
    <xsl:with-param name="s"><xsl:value-of select="substring(.,1,40)"/></xsl:with-param>
   </xsl:call-template>
   <xsl:text> ...</xsl:text>
  </xsl:message>
  *-->
  <xsl:choose>
   <xsl:when test="position() = 1 and starts-with(.,'&#xA;')">
    <xsl:value-of select="substring(.,2)"/>
   </xsl:when>
   <xsl:otherwise><xsl:value-of select="."/></xsl:otherwise>
  </xsl:choose>
 </xsl:template>

 <xsl:template name="crush-newlines">
  <xsl:param name="s"></xsl:param>
  <xsl:choose>
   <xsl:when test="contains($s,'&#xA;')">
    <xsl:value-of select="substring-before($s,'&#xA;')"/>
    <xsl:text>\n</xsl:text>
    <xsl:call-template name="crush-newlines">
     <xsl:with-param name="s"><xsl:value-of select="substring-after($s,'&#xA;')"/></xsl:with-param>
    </xsl:call-template>
   </xsl:when>
   <xsl:otherwise><xsl:value-of select="$s"/></xsl:otherwise>
  </xsl:choose>
 </xsl:template>

 <!--* for each scrap that has a 'file' attribute, write it
     * out to that file *--> 
 <xsl:template match="scrap[@file]" mode="not-in-use">
  <saxon:output file="{@file}" method="text">
   <xsl:apply-templates mode="scrap"/>
  </saxon:output>  
 </xsl:template>
 <!--* This would work ok, except that the saxon:output element
     * erases any existing file of the name, rather than appending.
     * So we need to do it a slightly different way:
     * For each scrap with a file attribute, if it is the first
     * with that file attribute, we write it out, then we write
     * out all the others which name it as their prev, then we
     * write out all the others with that file value.
     *-->

 <xsl:template match="scrap[@file]">
  <xsl:param name="filename"><xsl:value-of select="@file"/></xsl:param>
  <xsl:if test="not(./preceding::scrap[@file=$filename])">
   <xsl:message>Writing file <xsl:value-of select="@file"/> ... </xsl:message>
   <saxon:output file="{@file}" method="text">
    <xsl:call-template name="scrap"/>
    <xsl:apply-templates mode="file-append" 
			 select="./following::scrap[@file=$filename]"/>
   </saxon:output>  
  </xsl:if>
 </xsl:template>
 <xsl:template match="scrap[@file]" mode="file-append">
  <xsl:call-template name="scrap"/>
 </xsl:template>

 <!--* within a scrap, ptr and ref mean recursive embedding *-->
 <xsl:template match="scrap/ptr | scrap/ref" mode="scrap">
  <!--* 
  <xsl:message>Following pointer to <xsl:value-of select="@target"/>:</xsl:message>
  *-->
  <xsl:apply-templates select="id(@target)" mode="scrap"/>
  <!--* 
  <xsl:message>... <xsl:value-of select="@target"/> done.</xsl:message>
  *-->
 </xsl:template>

 <xsl:template match="scrap" mode="scrap" name="scrap">
  <xsl:param name="idCur"><xsl:value-of select="@id"/></xsl:param>
  <xsl:apply-templates mode="scrap"/>
  <!--* if this scrap has an ID, we should now go and fetch all of
      * its continuations, which are the elements which have the 
      * id of this scrap as their prev value 
      *-->
  <xsl:if test="@id">
   <xsl:comment>Here we should be looking for follow-ons,
    that is scraps with prev="<xsl:value-of select="$idCur"
    />".</xsl:comment>
   <xsl:apply-templates mode="scrap" 
			select="/descendant-or-self::scrap[@prev=$idCur]"/>
  </xsl:if>
 </xsl:template>

</xsl:stylesheet>
<!-- Keep this comment at the end of the file
Local variables:
mode: xml
sgml-default-dtd-file:(concat sgmlvol "/SGML/Public/Emacs/xslt.ced")
sgml-omittag:t
sgml-shorttag:t
sgml-indent-data:t
sgml-indent-step:1
End:
-->
