<!DOCTYPE xsl:stylesheet PUBLIC 'http://www.w3.org/1999/XSL/Transform'
      'file:///D:/SGML/Public/W3C/xslt10.dtd' >
<xsl:stylesheet 
 version="1.0" 
 xmlns:saxon="http://icl.com/saxon"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

 <!--* From XML, produce S-expression representation of components
     * in the infoset
     * Revisions:
     * 2001-07-30 : CMSMcQ : made file 
     *-->
<!--* To do:
      * add (slots for) type information
      * make twin stylesheet to emit components
         * generate unique IDs for each attribute and element
         * generate one component at a time, then recur to atts and children
    *-->

 <!--* Output and whitespace details *-->
 <xsl:output method="text" 
  encoding="us-ascii"
  indent="no"
  />
 <xsl:strip-space 
  elements="purchaseOrder shipTo billTo items item"
  />
 <xsl:preserve-space elements="xsl:text"/>

 <!--* Emit document with 'document', children *-->
 <xsl:template match="/">
  <xsl:text>;;; S-expressions in item-property Form </xsl:text>
  <xsl:text>&#xA;;;; generated by stylesheet xmliset.xsl </xsl:text>
  <xsl:text>&#xA;&#xA;(document document-node (</xsl:text>

  <xsl:text>&#xA;(children . (</xsl:text>
  <xsl:apply-templates select="node()|comment()|processing-instruction()|text()" mode="getid"/>
  <xsl:text>))</xsl:text>

  <xsl:text>&#xA;(document-element .</xsl:text>
  <xsl:apply-templates select="*" mode="getid"/>
  <xsl:text>)</xsl:text>

  <xsl:text>&#xA;(notations . ())</xsl:text>
  <!--* we won't get notation information from XSLT.
      * Do it by hand, or not at all.
      *-->

  <xsl:text>&#xA;(unparsed-entities . </xsl:text>
  <!--* I don't know how to get a dump of this list *-->
  <xsl:text>()</xsl:text>
  <xsl:text>)</xsl:text>


  <xsl:text>&#xA;(base-URI . "</xsl:text>
  <xsl:value-of select="saxon:systemId()"/><!--* get property *-->
  <xsl:text>")</xsl:text>

<!--*  The next few items are hard-coded for convenience in 
    *  generating examples.  Any real use of this stylesheet must
    *  suppress them all or find a way to get this information.
    *-->
<!--*
  We're not going to be a party to this idea of exposing the input
  encoding.  So there.
  <xsl:text>&#xA;(character-encoding-scheme . </xsl:text>
  <xsl:text>)</xsl:text>
*-->
  <xsl:text>&#xA;(standalone . ())</xsl:text>
  <xsl:text>&#xA;(version . ())</xsl:text>
  <xsl:text>&#xA;(all-declarations-processed . nil)</xsl:text>

  <xsl:text>&#xA;)) ;;; end document</xsl:text>
  <xsl:apply-templates/> 
 </xsl:template>

 <!--* Emit comments with 'comment', content *-->
 <xsl:template match='comment()' mode="getid" name="commentid">
  <xsl:text> comment-</xsl:text>
  <xsl:number level="any" from="/"/>
 </xsl:template>
 <xsl:template match='comment()'>
  <xsl:text>&#xA;&#xA;(comment-ii</xsl:text>
  <xsl:call-template name="commentid"/>
  <xsl:text> (</xsl:text>

  <xsl:text>&#xA;(content "</xsl:text>
  <xsl:value-of select="."/>
  <xsl:text>")</xsl:text>

  <xsl:text>&#xA;(parent .</xsl:text>
  <xsl:choose>
   <xsl:when test="parent::*[node()]">
    <xsl:apply-templates select=".." mode="getid"/>
   </xsl:when>
   <xsl:otherwise>
    <xsl:text> document-node</xsl:text>
   </xsl:otherwise>
  </xsl:choose>
  <xsl:text>)</xsl:text>

  <xsl:text>))</xsl:text>
 </xsl:template>
	
 <!--* Emit processing instructions with 'pi', target, content *-->
 <xsl:template match='processing-instruction()' mode="getid" name="piid">
  <xsl:text> pi-</xsl:text>
  <xsl:value-of select="name()"/>
  <xsl:text>-</xsl:text>
  <xsl:number level="any" from="/"/>
 </xsl:template>
 <xsl:template match='processing-instruction()'>
  <xsl:text>&#xA;&#xA;(pi</xsl:text>
  <xsl:call-template name="piid"/>
  <xsl:text> (</xsl:text>

  <xsl:text>&#xA;(target . </xsl:text>
  <xsl:value-of select="name()"/>
  <xsl:text>)</xsl:text>

  <xsl:text>&#xA;(content . "</xsl:text>
  <xsl:value-of select="."/>
  <xsl:text>")</xsl:text>

  <xsl:text>&#xA;(base-URI . "</xsl:text>
  <xsl:value-of select="saxon:systemId()"/><!--* get property *-->
  <xsl:text>")</xsl:text>

  <xsl:text>&#xA;(notation . unknown)</xsl:text>
  <xsl:text> ;;; to be supplied by other means</xsl:text>

  <xsl:text>&#xA;(parent .</xsl:text>
  <xsl:choose>
   <xsl:when test="parent::*[node()]">
    <xsl:apply-templates select=".." mode="getid"/>
   </xsl:when>
   <xsl:otherwise>
    <xsl:text> document-node</xsl:text>
   </xsl:otherwise>
  </xsl:choose>
  <xsl:text>)</xsl:text>

  <xsl:text>))</xsl:text>
 </xsl:template>

 <!--* Emit elements with 'element', gi, atts, children *-->
 <xsl:template match='*' mode="getid" name="elemid">
  <xsl:text> elem-</xsl:text>
  <xsl:number level="any" from="/" count="*"/>
 </xsl:template>
 <xsl:template match="*">
  <xsl:text>&#xA;&#xA;(eii</xsl:text>
  <xsl:call-template name="elemid"/>
  <xsl:text> (</xsl:text>

  <xsl:text>&#xA;(namespace-name . "</xsl:text>
  <xsl:value-of select="namespace-uri()"/>
  <xsl:text>")</xsl:text>
  <xsl:if test="''=namespace-uri()">
   <xsl:text> ;;; N.B. the value "" may mean the name is unqualified.</xsl:text>
  </xsl:if>

  <xsl:text>&#xA;(local-name . </xsl:text>
  <xsl:value-of select="local-name()"/>
  <xsl:text>)</xsl:text>

  <xsl:text>&#xA;(prefix . </xsl:text>
  <xsl:value-of select="substring-before(name(),':')"/>
  <xsl:text>) ;;; omit this?</xsl:text>

  <xsl:text>&#xA;(children . (</xsl:text>
  <xsl:apply-templates select="node()|comment()|processing-instruction()|text()" mode="getid"/>
  <xsl:text>))</xsl:text>

  <xsl:text>&#xA;(attributes . (</xsl:text>
  <xsl:apply-templates select="@*" mode="getid"/>
  <xsl:text>))</xsl:text>

  <xsl:text>&#xA;(namespace-attributes . ())</xsl:text>
  <xsl:text> ;;; to be supplied by other means</xsl:text>

  <xsl:text>&#xA;(in-scope-namespaces . ())</xsl:text>
  <xsl:text> ;;; to be supplied by other means</xsl:text>

  <xsl:text>&#xA;(base-URI . "</xsl:text>
  <xsl:value-of select="saxon:systemId()"/>
  <xsl:text>")</xsl:text>

  <xsl:text>&#xA;(parent .</xsl:text>
  <xsl:choose>
   <xsl:when test="parent::*[node()]">
    <xsl:apply-templates select=".." mode="getid"/>
   </xsl:when>
   <xsl:otherwise>
    <xsl:text> document-node</xsl:text>
   </xsl:otherwise>
  </xsl:choose>
  <xsl:text>)</xsl:text>

  <xsl:text>))</xsl:text>
  <xsl:apply-templates select="node()|comment()|processing-instruction()|text()|@*"/> 
 </xsl:template>

 <!--* Emit attributes as pair of name, value *-->
 <xsl:template match='@*' mode="getid" name="attid">
  <xsl:apply-templates select=".." mode="getid"/>
  <xsl:text>@</xsl:text>
  <xsl:value-of select="name()"/>
 </xsl:template>
 <xsl:template match="@*">
  <xsl:text>&#xA;&#xA;(aii</xsl:text>
  <xsl:call-template name="attid"/>
  <xsl:text> (</xsl:text>

  <xsl:text>&#xA;(namespace-name . "</xsl:text>
  <xsl:value-of select="namespace-uri()"/>
  <xsl:text>")</xsl:text>
  <xsl:if test="''=namespace-uri()">
   <xsl:text> ;;; N.B. the value "" may mean the name is unqualified.</xsl:text>
  </xsl:if>

  <xsl:text>&#xA;(local-name . </xsl:text>
  <xsl:value-of select="local-name()"/>
  <xsl:text>)</xsl:text>

  <xsl:text>&#xA;(prefix . </xsl:text>
  <xsl:value-of select="substring-before(name(),':')"/>
  <xsl:text>) ;;; omit this?</xsl:text>

  <xsl:text>&#xA;(normalized-value . "</xsl:text>
  <xsl:value-of select="."/>
  <xsl:text>")</xsl:text>

  <xsl:text>&#xA;(specified . t)</xsl:text>

  <xsl:text>&#xA;(attribute-type . unknown)</xsl:text>
  <xsl:text> ;;; to be supplied by other means</xsl:text>

  <xsl:text>&#xA;(references . unknown)</xsl:text>
  <xsl:text> ;;; to be supplied by other means</xsl:text>

  <xsl:text>&#xA;(owner-element .</xsl:text>
  <xsl:apply-templates select=".." mode="getid"/>
  <xsl:text>)</xsl:text>

  <xsl:text>&#xA;)) </xsl:text>
 </xsl:template>

 <!--* Emit text nodes as string *-->
 <xsl:template match='text()' mode="getid" name="textid">
  <xsl:text> scii-</xsl:text>
  <xsl:number level="any" from="/"/>
 </xsl:template>
 <xsl:template match="text()">
  <xsl:text>&#xA;&#xA;(seq-cii</xsl:text>
  <xsl:call-template name="textid"/>
  <xsl:text> (</xsl:text>

  <xsl:text>&#xA;(character-codes "</xsl:text>
  <xsl:value-of select="."/>
  <xsl:text>")</xsl:text>

  <xsl:text>&#xA;(parent .</xsl:text>
  <xsl:apply-templates select=".." mode="getid"/>
  <xsl:text>)</xsl:text>

  <xsl:text>))</xsl:text>
 </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:
-->

