<!-- Document format stylesheet:  remap.xsl -->
<!-- written by and copyright Thomas E Leathrum -->
<!-- July 2008 -->
<!-- see articlestyledoc.xml for details of use -->
<xsl:stylesheet version="1.0"  
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 >
    
<xsl:output method="xml" version="1.0" indent="yes"
  encoding="utf-8"
  omit-xml-declaration="no"
  doctype-system="http://www.w3.org/2002/04/xhtml-math-svg/xhtml-math-svg.dtd"
  doctype-public="-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN"
  media-type="text/xml"
  standalone="no"/>
  
  <!-- base URL, location of all files -->
  <xsl:variable name="baseurl"></xsl:variable>
  <!-- necessary files:  remap.xsl (this file), cpt-inpage.js, mathtrans.jar -->
  <!-- mathml.xsl, ctop.xsl, pmathml.xsl, pmathmlcss.xsl  -->

  <!-- hard-coded displayed strings placed here for localization -->
  <xsl:variable name="abstr-s">Abstract</xsl:variable>
  <xsl:variable name="cnts-s">Contents</xsl:variable>
  <xsl:variable name="thm-s">Theorem</xsl:variable>
  <xsl:variable name="pf-s">Proof</xsl:variable>
  <xsl:variable name="defn-s">Definition</xsl:variable>
  <xsl:variable name="exp-s">Example</xsl:variable>
  <xsl:variable name="exs-s">Exercise</xsl:variable>
  <xsl:variable name="prob-s">Problem</xsl:variable>
  <xsl:variable name="claim-s">Claim</xsl:variable>
  <xsl:variable name="lemma-s">Lemma</xsl:variable>
  <xsl:variable name="cor-s">Corollary</xsl:variable>
  <xsl:variable name="prpn-s">Proposition</xsl:variable>
  <xsl:variable name="chap-s">Chapter</xsl:variable>
  <xsl:variable name="chap-abbr-s">chap</xsl:variable>
  <xsl:variable name="fig-s">Figure</xsl:variable>
  <xsl:variable name="app-s">Appendix</xsl:variable>
  <xsl:variable name="edt-s">ed</xsl:variable>
  <xsl:variable name="edn-s">ed</xsl:variable>
  <xsl:variable name="vol-s">vol</xsl:variable>
  <xsl:variable name="num-s">num</xsl:variable>
  <xsl:variable name="pg-s">p</xsl:variable>
  <xsl:variable name="pgs-s">pp</xsl:variable>

  <xsl:template match="/">
    <xsl:apply-templates mode="remap"/>
  </xsl:template>
  
  <xsl:variable name="ns-xhtml">http://www.w3.org/1999/xhtml</xsl:variable>
  <xsl:variable name="ns-svg">http://www.w3.org/2000/svg</xsl:variable>
  <xsl:variable name="ns-mml">http://www.w3.org/1998/Math/MathML</xsl:variable>
  <xsl:variable name="ns-xlink">http://www.w3.org/1999/xlink</xsl:variable>
  <xsl:variable name="cpt-js"><xsl:value-of select="$baseurl"/>cpt-inpage.js</xsl:variable>
  <xsl:variable name="cpt-ar"><xsl:value-of select="$baseurl"/>mathtrans.jar</xsl:variable>
  <xsl:variable name="mml-st"><xsl:value-of select="$baseurl"/>mathml.xsl</xsl:variable>
  <xsl:variable name="jv-class">MathTransApplet.class</xsl:variable>
  
  <xsl:template name="generic">
  <xsl:param name="tg">html</xsl:param>
  <xsl:param name="ns">html</xsl:param>
  <xsl:element name="{$tg}" namespace="{$ns}">
  <xsl:copy-of select="@*"/>
  <xsl:apply-templates mode="remap"/>
  </xsl:element>
  </xsl:template>
  
  <!-- remapping: basic xhtml elements -->
  <xsl:template mode="remap" match="p|em|strong|sub|sup">
  <xsl:call-template name="generic">
  <xsl:with-param name="tg"><xsl:copy-of select="local-name()"/></xsl:with-param>
  <xsl:with-param name="ns"><xsl:copy-of select="$ns-xhtml"/></xsl:with-param>
  </xsl:call-template>
  </xsl:template>
  
  <!-- downsizing the heading elements -->
  <!-- h1 centered used for title -->
  <!-- also need special code for punctuation after section numbers -->
  <!-- and internal links -->
  <xsl:template name="hh">
  <xsl:param name="hn">h1</xsl:param>
  <xsl:if test="/document/contents"><xsl:element name="div"  namespace="{$ns-xhtml}"><xsl:attribute name="style">float:right;</xsl:attribute>
  [<xsl:element name="a"  namespace="{$ns-xhtml}"><xsl:attribute name="href">#toc</xsl:attribute><xsl:value-of select="$cnts-s"/></xsl:element>]
  </xsl:element></xsl:if>
  <xsl:element name="{$hn}" namespace="{$ns-xhtml}">
  <xsl:copy-of select="@*"/>
  <xsl:element name="a" namespace="{$ns-xhtml}">
  <xsl:attribute name="id"><xsl:text>toclink</xsl:text>
  <xsl:value-of select="count(preceding::h1|preceding::h2|preceding::h3|preceding::h4|preceding::h5)+1"/>
  </xsl:attribute></xsl:element>
  <xsl:choose><xsl:when test="section|subsection|subsubsection|subsubsubsection|subsubsubsubsection|
           subsubsubsubsubsection|appendixsection|appendixsubsection|appendixsubsubsection|appendixsubsubsubsection|appendixsubsubsubsection">
  <xsl:apply-templates mode="remap" select="section|subsection|subsubsection|subsubsubsection|subsubsubsubsection|
           subsubsubsubsubsection|appendixsection|appendixsubsection|appendixsubsubsection|appendixsubsubsubsection|appendixsubsubsubsection"/><xsl:text>. </xsl:text>
  <xsl:apply-templates mode="remap" select="*[position()>1]|text()"/></xsl:when>
  <xsl:otherwise><xsl:apply-templates mode="remap"/></xsl:otherwise></xsl:choose>
  </xsl:element>
  </xsl:template>
  <xsl:template mode="remap" match="h1">
  <xsl:call-template name="hh"><xsl:with-param name="hn">h2</xsl:with-param>
  </xsl:call-template>
  </xsl:template>
  <xsl:template mode="remap" match="h2">
  <xsl:call-template name="hh"><xsl:with-param name="hn">h3</xsl:with-param>
  </xsl:call-template>
  </xsl:template>
  <xsl:template mode="remap" match="h3">
  <xsl:call-template name="hh"><xsl:with-param name="hn">h4</xsl:with-param>
  </xsl:call-template>
  </xsl:template>
  <xsl:template mode="remap" match="h4">
  <xsl:call-template name="hh"><xsl:with-param name="hn">h5</xsl:with-param>
  </xsl:call-template>
  </xsl:template>
  <xsl:template mode="remap" match="h5">
  <xsl:call-template name="hh"><xsl:with-param name="hn">h6</xsl:with-param>
  </xsl:call-template>
  </xsl:template>
  
  <!-- remapping: xhtml formatting elements -->
  <xsl:template mode="remap" match="abbr|acronym|address|bdo|blockquote|cite|code|dfn|div|span|kbd|pre|q|samp|var">
  <xsl:call-template name="generic">
  <xsl:with-param name="tg"><xsl:copy-of select="local-name()"/></xsl:with-param>
  <xsl:with-param name="ns"><xsl:copy-of select="$ns-xhtml"/></xsl:with-param>
  </xsl:call-template>
  </xsl:template>
  
  <!-- remapping: xhtml bi-directional text and editing elements -->
  <xsl:template mode="remap" match="bdo|del|ins">
  <xsl:call-template name="generic">
  <xsl:with-param name="tg"><xsl:copy-of select="local-name()"/></xsl:with-param>
  <xsl:with-param name="ns"><xsl:copy-of select="$ns-xhtml"/></xsl:with-param>
  </xsl:call-template>
  </xsl:template>
  
  <!-- remapping: xhtml list-related elements -->
  <xsl:template mode="remap" match="dl|dt|dd|ol|ul|li">
  <xsl:call-template name="generic">
  <xsl:with-param name="tg"><xsl:copy-of select="local-name()"/></xsl:with-param>
  <xsl:with-param name="ns"><xsl:copy-of select="$ns-xhtml"/></xsl:with-param>
  </xsl:call-template>
  </xsl:template>
  
  <!-- remapping: embedded content and advanced xhtml elements -->
  <xsl:template mode="remap" match="img|area|map|applet|object|param|iframe|link|base">
  <xsl:call-template name="generic">
  <xsl:with-param name="tg"><xsl:copy-of select="local-name()"/></xsl:with-param>
  <xsl:with-param name="ns"><xsl:copy-of select="$ns-xhtml"/></xsl:with-param>
  </xsl:call-template>
  </xsl:template>
  
  <!-- remapping: xhtml table-related elements -->
  <xsl:template mode="remap" match="table|tr|td|th|caption|col|colgroup|tbody|thead|tfoot">
  <xsl:call-template name="generic">
  <xsl:with-param name="tg"><xsl:copy-of select="local-name()"/></xsl:with-param>
  <xsl:with-param name="ns"><xsl:copy-of select="$ns-xhtml"/></xsl:with-param>
  </xsl:call-template>
  </xsl:template>
  
  <!-- remapping: xhtml form-related elements -->
  <xsl:template mode="remap" match="form|input|label|select|option|textarea|button|fieldset|optgroup">
  <xsl:call-template name="generic">
  <xsl:with-param name="tg"><xsl:copy-of select="local-name()"/></xsl:with-param>
  <xsl:with-param name="ns"><xsl:copy-of select="$ns-xhtml"/></xsl:with-param>
  </xsl:call-template>
  </xsl:template>
  
  <!-- xhtml elements *not* remapped: -->
  <!-- big small font br hr b i tt meta basefont center dir isindex menu s strike u -->
  <!-- all attributes are copied, whether intended for support or not -->
  
  <!-- special cases in xhtml: html body -->
  <!-- html special case to get MathML stylesheet processing instruction -->
  <!-- body special case to get script and applet tags -->
  <!-- to handle together, both *replaced* here with document tag -->
  <xsl:template mode="remap" match="/document/style"/>
  <xsl:template mode="remap" match="document">
  <xsl:processing-instruction name="xml-stylesheet">type="text/xsl" 
  href="<xsl:value-of select="$mml-st"/>"</xsl:processing-instruction>
  <xsl:element name="html"  namespace="{$ns-xhtml}">
  <xsl:element name="head" namespace="{$ns-xhtml}">
  <xsl:element name="title" namespace="{$ns-xhtml}"><xsl:value-of select="/document/title"/>
  </xsl:element>
  <xsl:if test="/document/style"><xsl:element name="style" namespace="{$ns-xhtml}">
  <xsl:copy-of select="/document/style/@*"/>
  <xsl:apply-templates mode="remap" select="/document/style/text()"/></xsl:element></xsl:if>
  </xsl:element>
  <xsl:element name="body"  namespace="{$ns-xhtml}">
  <xsl:attribute name="style">width:7.5in;margin:1in;</xsl:attribute>
  <xsl:element name="p" namespace="{$ns-xhtml}"><xsl:attribute name="style">height:0px;</xsl:attribute>
  <xsl:element name="script" namespace="{$ns-xhtml}">
  <xsl:attribute name="type">text/javascript</xsl:attribute>
  <xsl:attribute name="src"><xsl:value-of select="$cpt-js"/></xsl:attribute>
  </xsl:element>
  <xsl:element name="object" namespace="{$ns-xhtml}">
  <xsl:attribute name="classid">java:<xsl:value-of select="$jv-class"/></xsl:attribute>
  <xsl:attribute name="type">application/x-java-applet</xsl:attribute>
  <xsl:attribute name="archive"><xsl:value-of select="$cpt-ar"/></xsl:attribute>
  <xsl:attribute name="height">1</xsl:attribute>
  <xsl:attribute name="width">1</xsl:attribute>
  </xsl:element></xsl:element>
  <xsl:apply-templates mode="remap"/>
  </xsl:element>
  </xsl:element>
  </xsl:template>
  
  <!-- new elements:  math dmath (both contain CPT) -->
  <xsl:template mode="remap" match="math">
  <xsl:if test="label and @display='block'"><xsl:element name="div"  namespace="{$ns-xhtml}">
  <xsl:attribute name="style">float:left;</xsl:attribute>(<xsl:apply-templates select="label/*|label/text()" mode="remap"/>)</xsl:element></xsl:if>
  <xsl:variable name="ds"><xsl:choose><xsl:when test="@display='block'">div</xsl:when>
  <xsl:otherwise>span</xsl:otherwise></xsl:choose></xsl:variable>
  <xsl:element name="{$ds}" namespace="{$ns-xhtml}">
  <xsl:attribute name="class">math</xsl:attribute>
  <xsl:copy-of select="@*"/>
  <xsl:value-of  disable-output-escaping="yes" select="text()"/>
  </xsl:element>
  </xsl:template>

  <!-- dmath is abbreviation for math display="block -->
  <xsl:template mode="remap" match="dmath">
  <xsl:if test="label"><xsl:element name="div"  namespace="{$ns-xhtml}">
  <xsl:attribute name="style">float:left;</xsl:attribute>(<xsl:apply-templates select="label/*|label/text()" mode="remap"/>)</xsl:element></xsl:if>
  <xsl:element name="div" namespace="{$ns-xhtml}">
  <xsl:attribute name="class">math</xsl:attribute>
  <xsl:copy-of select="@*"/>
  <xsl:value-of disable-output-escaping="yes" select="text()"/>
  </xsl:element>
  </xsl:template>
  
  <!-- latex-like structure elements -->
  <xsl:template mode="remap" match="proof">
  <xsl:element name="p"  namespace="{$ns-xhtml}"><xsl:element name="em"  namespace="{$ns-xhtml}">
  <xsl:choose><xsl:when test="name"><xsl:value-of select="name"/></xsl:when>
  <xsl:otherwise><xsl:value-of select="$pf-s"/></xsl:otherwise></xsl:choose>:
  </xsl:element><xsl:text> </xsl:text>
  <xsl:apply-templates mode="remap"/><xsl:text> </xsl:text>
  &#x220E; <!-- QED block --></xsl:element>
  </xsl:template>
  <!-- generic definition element -->
  <xsl:template name="gen-defn">
  <xsl:param name="dn"><xsl:value-of select="$defn-s"/></xsl:param>
  <xsl:element name="p"  namespace="{$ns-xhtml}"><xsl:element name="strong"  namespace="{$ns-xhtml}">
  <xsl:choose><xsl:when test="name"><xsl:value-of select="name"/></xsl:when>
  <xsl:otherwise><xsl:value-of select="$dn"/></xsl:otherwise></xsl:choose>
  <xsl:if test="label"><xsl:text> </xsl:text>(<xsl:apply-templates select="label/*|label/text()" mode="remap"/>)</xsl:if>:</xsl:element><xsl:text> </xsl:text>
  <xsl:apply-templates mode="remap"/></xsl:element>
  </xsl:template>
  <!-- abbreviations for some definition types -->
  <xsl:template mode="remap" match="definition">
  <xsl:call-template name="gen-defn"/>
  </xsl:template>
  <xsl:template mode="remap" match="exercise">
  <xsl:call-template name="gen-defn">
  <xsl:with-param name="dn"><xsl:value-of select="$exs-s"/></xsl:with-param>
  </xsl:call-template>
  </xsl:template>
  <xsl:template mode="remap" match="example">
  <xsl:call-template name="gen-defn">
  <xsl:with-param name="dn"><xsl:value-of select="$exp-s"/></xsl:with-param>
  </xsl:call-template>
  </xsl:template>
  <xsl:template mode="remap" match="problem">
  <xsl:call-template name="gen-defn">
  <xsl:with-param name="dn"><xsl:value-of select="$prob-s"/></xsl:with-param>
  </xsl:call-template>
  </xsl:template>
  <!-- generic theorem element -->
  <xsl:template mode="remap" match="label"/>
  <xsl:template mode="remap" match="name"/>
  <xsl:template name="gen-thm">
  <xsl:param name="tn"><xsl:value-of select="$thm-s"/></xsl:param>
  <xsl:element name="p"  namespace="{$ns-xhtml}"><xsl:element name="strong"  namespace="{$ns-xhtml}">
  <xsl:choose><xsl:when test="name"><xsl:value-of select="name"/></xsl:when>
  <xsl:otherwise><xsl:value-of select="$tn"/></xsl:otherwise></xsl:choose>
  <xsl:if test="label"><xsl:text> </xsl:text>(<xsl:apply-templates select="label/*|label/text()" mode="remap"/>)</xsl:if>:</xsl:element><xsl:text> </xsl:text>
  <xsl:element name="em"  namespace="{$ns-xhtml}"><xsl:apply-templates mode="remap"/></xsl:element></xsl:element>
  </xsl:template>
  <!-- abbreviations for some theorem types -->
  <xsl:template mode="remap" match="theorem">
  <xsl:call-template name="gen-thm"/>
  </xsl:template>
  <xsl:template mode="remap" match="lemma">
  <xsl:call-template name="gen-thm">
  <xsl:with-param name="tn"><xsl:value-of select="$lemma-s"/></xsl:with-param>
  </xsl:call-template>
  </xsl:template>
  <xsl:template mode="remap" match="claim">
  <xsl:call-template name="gen-thm">
  <xsl:with-param name="tn"><xsl:value-of select="$claim-s"/></xsl:with-param>
  </xsl:call-template>
  </xsl:template>
  <xsl:template mode="remap" match="proposition">
  <xsl:call-template name="gen-thm">
  <xsl:with-param name="tn"><xsl:value-of select="$prpn-s"/></xsl:with-param>
  </xsl:call-template>
  </xsl:template>
  <xsl:template mode="remap" match="corollary">
  <xsl:call-template name="gen-thm">
  <xsl:with-param name="tn"><xsl:value-of select="$cor-s"/></xsl:with-param>
  </xsl:call-template>
  </xsl:template>
  
  <!-- topmatter elements -->
  <xsl:template mode="remap" match="document/title">
  <xsl:element name="h1" namespace="{$ns-xhtml}"><xsl:attribute name="style">text-align:center;</xsl:attribute>
  <xsl:apply-templates mode="remap"/></xsl:element>
  </xsl:template>
  <xsl:template mode="remap" match="subtitle">
  <xsl:element name="h2"  namespace="{$ns-xhtml}"><xsl:attribute name="style">text-align:center;</xsl:attribute><xsl:apply-templates mode="remap"/></xsl:element>
  </xsl:template>
  <xsl:template mode="remap" match="affiliation"/>
  <xsl:template mode="remap" match="user"/>
  <xsl:template mode="remap" match="host"/>
  <xsl:template mode="remap" match="email"/>
  <xsl:template mode="remap" match="url"/>
  <xsl:template mode="remap" match="author">
  <xsl:element name="div"  namespace="{$ns-xhtml}"><xsl:attribute name="style">text-align:center;</xsl:attribute><xsl:apply-templates mode="remap"/></xsl:element>
  <xsl:if test="affiliation">
  <xsl:element name="div"  namespace="{$ns-xhtml}"><xsl:attribute name="style">text-align:center;</xsl:attribute>
  (<xsl:apply-templates mode="remap" select="affiliation/*|affiliation/text()"/>)</xsl:element>
  </xsl:if>
  <xsl:if test="email">
    <xsl:element name="div"  namespace="{$ns-xhtml}"><xsl:attribute name="style">text-align:center;</xsl:attribute>
    <xsl:element name="kbd"  namespace="{$ns-xhtml}">
    <xsl:element name="a" namespace="{$ns-xhtml}">
    <xsl:attribute name="href">
    mailto:<xsl:value-of select="email/user/text()"/>@<xsl:value-of select="email/host/text()"/>
    </xsl:attribute>
    <xsl:value-of select="email/user/text()"/>
    <xsl:text>@</xsl:text>
    <xsl:value-of select="email/host/text()"/></xsl:element>
    </xsl:element></xsl:element>
  </xsl:if>
  <xsl:if test="url">
    <xsl:element name="div" namespace="{$ns-xhtml}"><xsl:attribute name="style">text-align:center;</xsl:attribute><xsl:element name="kbd" namespace="{$ns-xhtml}">
    <xsl:element name="a" namespace="{$ns-xhtml}">
    <xsl:attribute name="href">
    <xsl:value-of select="url/text()"/>
    </xsl:attribute>
    <xsl:value-of select="url/text()"/></xsl:element>
    </xsl:element></xsl:element>
  </xsl:if>
  <xsl:element name="div" namespace="{$ns-xhtml}"><xsl:attribute name="style">height:1em;"</xsl:attribute></xsl:element>
  </xsl:template>
  <xsl:template mode="remap" match="date">
  <xsl:element name="div" namespace="{$ns-xhtml}"><xsl:attribute name="style">text-align:center;</xsl:attribute><xsl:apply-templates mode="remap"/></xsl:element>
  </xsl:template>
  <xsl:template mode="remap" match="abstract">
  <xsl:element name="blockquote" namespace="{$ns-xhtml}"><xsl:attribute name="style">font-size:smaller;</xsl:attribute>
  <xsl:element name="p" namespace="{$ns-xhtml}">
  <xsl:element name="span" namespace="{$ns-xhtml}"><xsl:attribute name="style">font-variant:small-caps;</xsl:attribute>
  <xsl:choose><xsl:when test="name"><xsl:value-of select="name"/></xsl:when>
  <xsl:otherwise><xsl:value-of select="$abstr-s"/></xsl:otherwise></xsl:choose></xsl:element><xsl:text>:</xsl:text>
  <xsl:apply-templates mode="remap"/></xsl:element></xsl:element>
  </xsl:template>
  <xsl:template mode="remap" match="metadata"/>
  <xsl:template mode="remap" match="contents">
  <xsl:element name="h4" namespace="{$ns-xhtml}">
  <xsl:element name="a" namespace="{$ns-xhtml}"><xsl:attribute name="id">toc</xsl:attribute></xsl:element>
  <xsl:value-of select="$cnts-s"/>:</xsl:element>
  <xsl:element name="div" namespace="{$ns-xhtml}"><xsl:attribute name="style">position:relative;left:2em;</xsl:attribute>
  <xsl:for-each select="//h1|//h2|//h3|//h4|//h5">
  <xsl:element name="span" namespace="{$ns-xhtml}">
  <xsl:if test="self::h2"><xsl:attribute name="style">position:relative;left:2em;</xsl:attribute></xsl:if>
  <xsl:if test="self::h3"><xsl:attribute name="style">position:relative;left:4em;</xsl:attribute></xsl:if>
  <xsl:if test="self::h4"><xsl:attribute name="style">position:relative;left:6em;</xsl:attribute></xsl:if>
  <xsl:if test="self::h5"><xsl:attribute name="style">position:relative;left:8em;</xsl:attribute></xsl:if>
  <xsl:if test="section|subsection|subsubsection|subsubsubsection|subsubsubsubsection|subsubsubsubsubsection">
  <xsl:apply-templates mode="remap" select="./*[local-name()]"><xsl:with-param name="qq">skip</xsl:with-param></xsl:apply-templates>.<xsl:text> </xsl:text></xsl:if>
  <xsl:if test="chapter|appendix|appendixsection|appendixsubsection|appendixsubsubsection|appendixsubsubsubsection|appendixsubsubsubsubsection">
  <xsl:apply-templates mode="remap" select="./*[local-name()]"><xsl:with-param name="qq">skip</xsl:with-param></xsl:apply-templates>
  <xsl:text> </xsl:text></xsl:if>
  <xsl:element name="a" namespace="{$ns-xhtml}"><xsl:attribute name="href">
  <xsl:text>#toclink</xsl:text>
  <xsl:value-of select="count(preceding::h1|preceding::h2|preceding::h3|preceding::h4|preceding::h5)+1"/>
  </xsl:attribute>
  <xsl:apply-templates mode="remap" select="./text()|./*[position()>1]"/>
  </xsl:element></xsl:element><xsl:element name="br" namespace="{$ns-xhtml}"/></xsl:for-each>
  </xsl:element></xsl:template>
  
  <!-- centered caption for figures -->
  <xsl:template mode="remap" match="caption">
    <xsl:element name="div" namespace="{$ns-xhtml}"><xsl:attribute name="style">text-align:center;</xsl:attribute>
    <xsl:choose>
    <xsl:when test="figure|equation">  
    <xsl:apply-templates mode="remap" select="figure|equation"/>:<xsl:text> </xsl:text>
    <xsl:apply-templates mode="remap" select="./text()|./*[position()>1]"/>
    </xsl:when>
    <xsl:otherwise><xsl:apply-templates mode="remap"/></xsl:otherwise>
    </xsl:choose></xsl:element>
  </xsl:template>
  
  <!-- elements for autonumbering -->
  <xsl:template mode="remap" match="chapter">
    <xsl:param name="qq">noskip</xsl:param>
    <xsl:if test="@id and $qq!='skip'">
    <xsl:element name="a" namespace="{$ns-xhtml}">
    <xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute></xsl:element>
    </xsl:if>
    <xsl:value-of select="$chap-s"/><xsl:text> </xsl:text> 
    <xsl:number level="any" format="I" count="chap"/>:
  </xsl:template>
  <xsl:template mode="remap" match="section">
    <xsl:param name="qq">noskip</xsl:param>
    <xsl:if test="@id and $qq!='skip'">
    <xsl:element name="a" namespace="{$ns-xhtml}">
    <xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute></xsl:element>
    </xsl:if>
    <xsl:choose><xsl:when test="preceding::chapter">
    <xsl:number level="any" count="section" from="chapter"/></xsl:when>
    <xsl:otherwise><xsl:number level="any" count="section"/></xsl:otherwise></xsl:choose>
  </xsl:template>
  <xsl:template mode="remap" match="subsection">
    <xsl:param name="qq">noskip</xsl:param>
    <xsl:if test="@id and $qq!='skip'">
    <xsl:element name="a" namespace="{$ns-xhtml}">
    <xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute></xsl:element>
    </xsl:if>
    <xsl:choose><xsl:when test="preceding::chapter">
    <xsl:number level="any" count="section" from="chapter"/></xsl:when>
    <xsl:otherwise><xsl:number level="any" count="section"/></xsl:otherwise>
    </xsl:choose>.<xsl:number level="any" count="subsection" from="section"/>
  </xsl:template>
  <xsl:template mode="remap" match="subsubsection">
    <xsl:param name="qq">noskip</xsl:param>
    <xsl:if test="@id and $qq!='skip'">
    <xsl:element name="a" namespace="{$ns-xhtml}">
    <xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute></xsl:element>
    </xsl:if>
    <xsl:choose><xsl:when test="preceding::chapter">
    <xsl:number level="any" count="section" from="chapter"/></xsl:when>
    <xsl:otherwise><xsl:number level="any" count="section"/></xsl:otherwise>
    </xsl:choose>.<xsl:number 
      level="any" count="subsection" from="section"/>.<xsl:number 
      level="any" count="subsubsection" from="subsection"/>
  </xsl:template>
  <xsl:template mode="remap" match="subsubsubsection">
    <xsl:param name="qq">noskip</xsl:param>
    <xsl:if test="@id and $qq!='skip'">
    <xsl:element name="a" namespace="{$ns-xhtml}">
    <xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute></xsl:element>
    </xsl:if>
    <xsl:choose><xsl:when test="preceding::chapter">
    <xsl:number level="any" count="section" from="chapter"/></xsl:when>
    <xsl:otherwise><xsl:number level="any" count="section"/></xsl:otherwise>
    </xsl:choose>.<xsl:number 
      level="any" count="subsection" from="section"/>.<xsl:number 
      level="any" count="subsubsection" from="subsection"/>.<xsl:number 
      level="any" count="subsubsubsection" from="subsubsection"/>
  </xsl:template>
  <xsl:template mode="remap" match="subsubsubsubsection">
    <xsl:param name="qq">noskip</xsl:param>
    <xsl:if test="@id and $qq!='skip'">
    <xsl:element name="a" namespace="{$ns-xhtml}">
    <xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute></xsl:element>
    </xsl:if>
    <xsl:choose><xsl:when test="preceding::chapter">
    <xsl:number level="any" count="section" from="chaption"/></xsl:when>
    <xsl:otherwise><xsl:number level="any" count="section"/></xsl:otherwise>
    </xsl:choose>.<xsl:number 
      level="any" count="subsection" from="section"/>.<xsl:number 
      level="any" count="subsubsection" from="subsection"/>.<xsl:number 
      level="any" count="subsubsubsection" from="subsubsection"/>.<xsl:number 
      level="any" count="subsubsubsubsection" from="subsubsubsection"/>
  </xsl:template>
  <xsl:template mode="remap" match="subsubsubsubsubsection">
    <xsl:param name="qq">noskip</xsl:param>
    <xsl:if test="@id and $qq!='skip'">
    <xsl:element name="a" namespace="{$ns-xhtml}">
    <xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute></xsl:element>
    </xsl:if>
    <xsl:choose><xsl:when test="preceding::chapter">
    <xsl:number level="any" count="section" from="chapter"/></xsl:when>
    <xsl:otherwise><xsl:number level="any" count="section"/></xsl:otherwise>
    </xsl:choose>.<xsl:number 
      level="any" count="subsection" from="section"/>.<xsl:number 
      level="any" count="subsubsection" from="subsection"/>.<xsl:number 
      level="any" count="subsubsubsection" from="subsubsection"/>.<xsl:number 
      level="any" count="subsubsubsubsection" from="subsubsubsection"/>.<xsl:number 
      level="any" count="subsubsubsubsubsection" from="subsubsubsubsection"/>
  </xsl:template>
  <xsl:template mode="remap" match="figure">
    <xsl:param name="qq">noskip</xsl:param>
    <xsl:if test="@id and $qq!='skip'">
    <xsl:element name="a" namespace="{$ns-xhtml}">
    <xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute></xsl:element>
    </xsl:if>
    <xsl:value-of select="$fig-s"/><xsl:text> </xsl:text>
    <xsl:choose><xsl:when test="preceding::chapter">
    <xsl:number level="any" count="section" from="chapter"/></xsl:when>
    <xsl:otherwise><xsl:number level="any" count="section"/></xsl:otherwise>
    </xsl:choose>.<xsl:number 
      level="any" count="figure" from="section"/> 
  </xsl:template>
  <xsl:template mode="remap" match="equation">
    <xsl:param name="qq">noskip</xsl:param>
    <xsl:if test="@id and $qq!='skip'">
    <xsl:element name="a" namespace="{$ns-xhtml}">
    <xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute></xsl:element>
    </xsl:if>
    <xsl:choose><xsl:when test="preceding::chapter">
    <xsl:number level="any" count="section" from="chapter"/></xsl:when>
    <xsl:otherwise><xsl:number level="any" count="section"/></xsl:otherwise>
    </xsl:choose>.<xsl:number 
      level="any" count="equation" from="section"/> 
  </xsl:template>
  <xsl:template mode="remap" match="numref">
    <xsl:choose>
    <xsl:when test="@link='false'">
    <xsl:variable name="ss" select="@refid" />
    <xsl:apply-templates mode="remap" select="//chapter[@id=$ss]|//figure[@id=$ss]|//section[@id=$ss]|
          //equation[@id=$ss]|//subsection[@id=$ss]|//subsubsection[@id=$ss]|//subsubsubsection[@id]|
	  //subsubsubsubsection[@id=$ss]|//subsubsubsubsubsection[@id=$ss]|
	  //appendix[@id=$ss]|//appendixsection[@id=$ss]|//appendixsubsection[@id=$ss]|//appendixsubsubsection[@id=$ss]|
	  //appendixsubsubsubsection[@id=$ss]|//count[@id=$ss]" >
    <xsl:with-param name="qq">skip</xsl:with-param></xsl:apply-templates>
    </xsl:when>
    <xsl:otherwise>
    <xsl:element name="a" namespace="{$ns-xhtml}">
    <xsl:attribute name="href">#<xsl:value-of select="@refid"/></xsl:attribute>
    <xsl:variable name="ss" select="@refid" />
    <xsl:apply-templates mode="remap" select="//chapter[@id=$ss]|//figure[@id=$ss]|//section[@id=$ss]|
          //equation[@id=$ss]|//subsection[@id=$ss]|//subsubsection[@id=$ss]|//subsubsubsection[@id]|
	  //subsubsubsubsection[@id=$ss]|//subsubsubsubsubsection[@id=$ss]|
	  //appendix[@id=$ss]|//appendixsection[@id=$ss]|//appendixsubsection[@id=$ss]|//appendixsubsubsection[@id=$ss]|
	  //appendixsubsubsubsection[@id=$ss]|//count[@id=$ss]" >
    <xsl:with-param name="qq">skip</xsl:with-param></xsl:apply-templates>
    </xsl:element>
    </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template mode="remap" match="appendix">
    <xsl:param name="qq">noskip</xsl:param>
    <xsl:if test="@id and $qq!='skip'">
    <xsl:element name="a" namespace="{$ns-xhtml}">
    <xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute></xsl:element>
    </xsl:if>
    <xsl:value-of select="$app-s"/><xsl:text> </xsl:text> 
    <xsl:number level="any" count="appendix" format="A"/>:
  </xsl:template>
  <xsl:template mode="remap" match="appendixsection">
    <xsl:param name="qq">noskip</xsl:param>
    <xsl:if test="@id and $qq!='skip'">
    <xsl:element name="a" namespace="{$ns-xhtml}">
    <xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute></xsl:element>
    </xsl:if>
    <xsl:number level="any" count="appendix" format="A"/>.<xsl:number 
       level="any" count="appendixsection" from="appendix"/>
  </xsl:template>
  <xsl:template mode="remap" match="appendixsubsection">
    <xsl:param name="qq">noskip</xsl:param>
    <xsl:if test="@id and $qq!='skip'">
    <xsl:element name="a" namespace="{$ns-xhtml}">
    <xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute></xsl:element>
    </xsl:if>
    <xsl:number level="any" count="appendix" format="A"/>.<xsl:number 
       level="any" count="appendixsection" from="appendix"/>.<xsl:number 
       level="any" count="appendixsubsection" from="appendixsection"/>
  </xsl:template>
  <xsl:template mode="remap" match="appendixsubsubsection">
    <xsl:param name="qq">noskip</xsl:param>
    <xsl:if test="@id and $qq!='skip'">
    <xsl:element name="a" namespace="{$ns-xhtml}">
    <xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute></xsl:element>
    </xsl:if>
    <xsl:number level="any" count="appendix" format="A"/>.<xsl:number 
       level="any" count="appendixsection" from="appendix"/>.<xsl:number 
       level="any" count="appendixsubsection" from="appendixsection"/>.<xsl:number 
       level="any" count="appendixsubsubsection" from="appendixsubsection"/>
  </xsl:template>
  <xsl:template mode="remap" match="appendixsubsubsubsection">
    <xsl:param name="qq">noskip</xsl:param>
    <xsl:if test="@id and $qq!='skip'">
    <xsl:element name="a" namespace="{$ns-xhtml}">
    <xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute></xsl:element>
    </xsl:if>
    <xsl:number level="any" count="appendix" format="A"/>.<xsl:number 
       level="any" count="appendixsection" from="appendix"/>.<xsl:number 
       level="any" count="appendixsubsection" from="appendixsection"/>.<xsl:number 
       level="any" count="appendixsubsubsection" from="appendixsubsection"/>.<xsl:number 
       level="any" count="appendixsubsubsubsection" from="appendixsubsubsection"/>
  </xsl:template>
  <xsl:template mode="remap" match="count">
    <xsl:param name="qq">noskip</xsl:param>
    <xsl:if test="@id and $qq!='skip'">
    <xsl:element name="a" namespace="{$ns-xhtml}">
    <xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute></xsl:element>
    </xsl:if>
    <xsl:variable name="ss" select="@countid" />
    <xsl:choose>
    <xsl:when test="@pre">
    <xsl:choose>
    <xsl:when test="@pre='chapter'"><xsl:number level="any" format="I" count="chapter"/></xsl:when>
    <xsl:when test="@pre='appendix'"><xsl:number level="any" format="A" count="appendix"/></xsl:when>
    <xsl:when test="contains(@pre,'section')"><xsl:variable name="tt" select="@pre"/>
    <xsl:apply-templates mode="remap" select="preceding::*[local-name()=$tt][1]"/></xsl:when>
    </xsl:choose>.</xsl:when>
    <xsl:otherwise>
    <xsl:apply-templates mode="remap" select="*|text()"/>.</xsl:otherwise>
    </xsl:choose>
    <xsl:number level="any" count="count[@countid=$ss]"/>
  </xsl:template>
  
  <!-- elements common between xhtml and svg -->
  <!-- need to manage namespace correctly -->
  <xsl:template mode="remap" match="a">
  <xsl:choose><xsl:when test="ancestor::svg">
  <xsl:element name="a" namespace="{$ns-svg}"><xsl:attribute name="href" namespace="{$ns-xlink}"><xsl:value-of select="@href"/></xsl:attribute>
  <xsl:apply-templates mode="remap"/></xsl:element>
  </xsl:when>
  <xsl:otherwise>
  <xsl:call-template name="generic">
  <xsl:with-param name="tg"><xsl:copy-of select="local-name()"/></xsl:with-param>
  <xsl:with-param name="ns"><xsl:value-of select="$ns-xhtml"/></xsl:with-param>
  </xsl:call-template>
  </xsl:otherwise>
  </xsl:choose>
  </xsl:template>
  
  <xsl:template mode="remap" match="script">
  <xsl:variable name="nns">
  <xsl:choose><xsl:when test="ancestor::svg"><xsl:copy-of select="$ns-svg"/></xsl:when>
  <xsl:otherwise><xsl:copy-of select="$ns-xhtml"/></xsl:otherwise>
  </xsl:choose></xsl:variable>
  <xsl:call-template name="generic">
  <xsl:with-param name="tg"><xsl:if test="ancestor::svg">svg:</xsl:if><xsl:copy-of select="local-name()"/></xsl:with-param>
  <xsl:with-param name="ns"><xsl:value-of select="$nns"/></xsl:with-param>
  </xsl:call-template>
  </xsl:template>
  
  <xsl:template mode="remap" match="svg//style">
  <xsl:call-template name="generic">
  <xsl:with-param name="tg">svg:<xsl:copy-of select="local-name()"/></xsl:with-param>
  <xsl:with-param name="ns"><xsl:value-of select="$ns-svg"/></xsl:with-param>
  </xsl:call-template>
  </xsl:template>
  
  <!-- no need  to remap mathml since using cpt -->
  <!-- but will need to remap svg -->
  <!-- covered elsewhere: a script style -->
  <xsl:template mode="remap" match="svg">
  <xsl:call-template name="generic">
  <xsl:with-param name="tg">svg:<xsl:copy-of select="local-name()"/></xsl:with-param>
  <xsl:with-param name="ns"><xsl:copy-of select="$ns-svg"/></xsl:with-param>
  </xsl:call-template>
  </xsl:template>
  <xsl:template mode="remap" match="altGlyph|altGlyphDef|altGlyphItem|animate|animateColor|animateMotion|animateTransform">
  <xsl:call-template name="generic">
  <xsl:with-param name="tg">svg:<xsl:copy-of select="local-name()"/></xsl:with-param>
  <xsl:with-param name="ns"><xsl:copy-of select="$ns-svg"/></xsl:with-param>
  </xsl:call-template>
  </xsl:template>
  <xsl:template mode="remap" match="circle|clipPath|color-profile|cursor|definition-src|defs|desc">
  <xsl:call-template name="generic">
  <xsl:with-param name="tg">svg:<xsl:copy-of select="local-name()"/></xsl:with-param>
  <xsl:with-param name="ns"><xsl:copy-of select="$ns-svg"/></xsl:with-param>
  </xsl:call-template>
  </xsl:template>
  <xsl:template mode="remap" match="ellipse|feBlend|feColorMatrix|feComponentTransfer|feComposite|feConvolveMatrix">
  <xsl:call-template name="generic">
  <xsl:with-param name="tg">svg:<xsl:copy-of select="local-name()"/></xsl:with-param>
  <xsl:with-param name="ns"><xsl:copy-of select="$ns-svg"/></xsl:with-param>
  </xsl:call-template>
  </xsl:template>
  <xsl:template mode="remap" match="feDiffuseLighting|feDisplacementMap|feDistantLight|feFlood">
  <xsl:call-template name="generic">
  <xsl:with-param name="tg">svg:<xsl:copy-of select="local-name()"/></xsl:with-param>
  <xsl:with-param name="ns"><xsl:copy-of select="$ns-svg"/></xsl:with-param>
  </xsl:call-template>
  </xsl:template>
  <xsl:template mode="remap" match="feFuncA|feFuncB|feFuncG|feFuncR|feGaussianBlur|feImage">
  <xsl:call-template name="generic">
  <xsl:with-param name="tg">svg:<xsl:copy-of select="local-name()"/></xsl:with-param>
  <xsl:with-param name="ns"><xsl:copy-of select="$ns-svg"/></xsl:with-param>
  </xsl:call-template>
  </xsl:template>
  <xsl:template mode="remap" match="feMerge|feMergeNode|feMorphology|feOffset|fePointLight">
  <xsl:call-template name="generic">
  <xsl:with-param name="tg">svg:<xsl:copy-of select="local-name()"/></xsl:with-param>
  <xsl:with-param name="ns"><xsl:copy-of select="$ns-svg"/></xsl:with-param>
  </xsl:call-template>
  </xsl:template>
  <xsl:template mode="remap" match="feSpecularLighting|feSpotLight|feTile|feTurbulence">
  <xsl:call-template name="generic">
  <xsl:with-param name="tg">svg:<xsl:copy-of select="local-name()"/></xsl:with-param>
  <xsl:with-param name="ns"><xsl:copy-of select="$ns-svg"/></xsl:with-param>
  </xsl:call-template>
  </xsl:template>
  <xsl:template mode="remap" match="filter|font|font-face|font-face-format|font-face-name">
  <xsl:call-template name="generic">
  <xsl:with-param name="tg">svg:<xsl:copy-of select="local-name()"/></xsl:with-param>
  <xsl:with-param name="ns"><xsl:copy-of select="$ns-svg"/></xsl:with-param>
  </xsl:call-template>
  </xsl:template>
  <xsl:template mode="remap" match="font-face-src|font-face-uri|foreignObject">
  <xsl:call-template name="generic">
  <xsl:with-param name="tg">svg:<xsl:copy-of select="local-name()"/></xsl:with-param>
  <xsl:with-param name="ns"><xsl:copy-of select="$ns-svg"/></xsl:with-param>
  </xsl:call-template>
  </xsl:template>
  <xsl:template mode="remap" match="g|glyph|glyphRef|hkern|image|line|linearGradient">
  <xsl:call-template name="generic">
  <xsl:with-param name="tg">svg:<xsl:copy-of select="local-name()"/></xsl:with-param>
  <xsl:with-param name="ns"><xsl:copy-of select="$ns-svg"/></xsl:with-param>
  </xsl:call-template>
  </xsl:template>
  <xsl:template mode="remap" match="marker|mask|missing-glyph|mpath|path">
  <xsl:call-template name="generic">
  <xsl:with-param name="tg">svg:<xsl:copy-of select="local-name()"/></xsl:with-param>
  <xsl:with-param name="ns"><xsl:copy-of select="$ns-svg"/></xsl:with-param>
  </xsl:call-template>
  </xsl:template>
  <xsl:template mode="remap" match="pattern|polygon|polyline|radialGradient|rect">
  <xsl:call-template name="generic">
  <xsl:with-param name="tg">svg:<xsl:copy-of select="local-name()"/></xsl:with-param>
  <xsl:with-param name="ns"><xsl:copy-of select="$ns-svg"/></xsl:with-param>
  </xsl:call-template>
  </xsl:template>
  <xsl:template mode="remap" match="set|stop|switch|symbol|text|textPath">
  <xsl:call-template name="generic">
  <xsl:with-param name="tg">svg:<xsl:copy-of select="local-name()"/></xsl:with-param>
  <xsl:with-param name="ns"><xsl:copy-of select="$ns-svg"/></xsl:with-param>
  </xsl:call-template>
  </xsl:template>
  <xsl:template mode="remap" match="tref|tspan|use|view|kern">
  <xsl:call-template name="generic">
  <xsl:with-param name="tg">svg:<xsl:copy-of select="local-name()"/></xsl:with-param>
  <xsl:with-param name="ns"><xsl:copy-of select="$ns-svg"/></xsl:with-param>
  </xsl:call-template>
  </xsl:template>
  <xsl:template mode="remap" match="svg//title|svg//metadata">
  <xsl:call-template name="generic">
  <xsl:with-param name="tg">svg:<xsl:copy-of select="local-name()"/></xsl:with-param>
  <xsl:with-param name="ns"><xsl:copy-of select="$ns-svg"/></xsl:with-param>
  </xsl:call-template>
  </xsl:template>
  
  <!-- citations, bibliography, etc. -->
  <xsl:template mode="remap" match="publisher/city"/>
  <xsl:template mode="remap" match="cite">
  <xsl:element name="p" namespace="{$ns-xhtml}">
    <xsl:if test="@id"><xsl:element name="a" namespace="{$ns-xhtml}">
    <xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute></xsl:element></xsl:if>
  [<xsl:apply-templates select="tag/*|tag/text()" mode="remap"/>]<xsl:text> </xsl:text>
  <xsl:for-each select="author"><xsl:apply-templates select="./*|./text()" mode="remap"/>,
  <xsl:text> </xsl:text></xsl:for-each>
  <xsl:for-each select="editor"><xsl:apply-templates select="./*|./text()" mode="remap"/>
  <xsl:text> </xsl:text><xsl:value-of select="$edt-s"/>,
  <xsl:text> </xsl:text></xsl:for-each>
  <xsl:if test="article">"<xsl:apply-templates select="article/*|article/text()" mode="remap"/>,"<xsl:text> </xsl:text></xsl:if>
  <xsl:if test="chapternum"><xsl:value-of select="$chap-abbr-s"/>.<xsl:text> </xsl:text><xsl:apply-templates select="chapternum/*|chapternum/text()" mode="remap"/>
  <xsl:choose><xsl:when test="chapter">:</xsl:when><xsl:otherwise>,</xsl:otherwise></xsl:choose>
  <xsl:text> </xsl:text></xsl:if>
  <xsl:if test="chapter">"<xsl:apply-templates select="chapter/*|chapter/text()" mode="remap"/>,"<xsl:text> </xsl:text></xsl:if>
  <xsl:if test="journal"><xsl:element name="em" namespace="{$ns-xhtml}"><xsl:apply-templates select="journal/*|journal/text()" mode="remap"/></xsl:element>,<xsl:text> </xsl:text></xsl:if>
  <xsl:if test="book"><xsl:element name="em" namespace="{$ns-xhtml}"><xsl:apply-templates select="book/*|book/text()" mode="remap"/></xsl:element>,<xsl:text> </xsl:text></xsl:if>
  <xsl:choose>
  <xsl:when test="volume"><xsl:value-of select="$vol-s"/>.<xsl:text> </xsl:text>
  <xsl:apply-templates select="volume/*|volume/text()" mode="remap"/>
  <xsl:if test="number"><xsl:text> </xsl:text>
  <xsl:value-of select="$num-s"/>.<xsl:text> </xsl:text>
  <xsl:apply-templates select="number/*|number/text()" mode="remap"/>
  </xsl:if>,<xsl:text> </xsl:text>
  </xsl:when>
  <xsl:otherwise>
  <xsl:if test="number"><xsl:value-of select="$num-s"/>.<xsl:text> </xsl:text>
  <xsl:apply-templates select="number/*|number/text()" mode="remap"/>,<xsl:text> </xsl:text>
  </xsl:if>
  </xsl:otherwise>
  </xsl:choose>
  <xsl:if test="edition"><xsl:apply-templates select="edition/*|edition/text()" mode="remap"/>
  <xsl:text> </xsl:text><xsl:value-of select="$edn-s"/>.,<xsl:text> </xsl:text></xsl:if>
  <xsl:if test="publisher/city"><xsl:apply-templates select="publisher/city/*|publisher/city/text()" mode="remap"/>:<xsl:text> </xsl:text></xsl:if>
  <xsl:if test="publisher"><xsl:apply-templates select="publisher/*|publisher/text()" mode="remap"/>,<xsl:text> </xsl:text></xsl:if>
  <xsl:if test="date"><xsl:apply-templates select="date/*|date/text()" mode="remap"/>
  <xsl:choose><xsl:when test="url|pages|page">,</xsl:when><xsl:otherwise>.</xsl:otherwise>
  </xsl:choose><xsl:text> </xsl:text></xsl:if>
  <xsl:if test="url"><xsl:element name="kbd" namespace="{$ns-xhtml}"><xsl:element name="a" namespace="{$ns-xhtml}"><xsl:attribute name="href"><xsl:apply-templates select="url/*|url/text()" mode="remap"/></xsl:attribute>
  <xsl:apply-templates select="url/*|url/text()" mode="remap"/></xsl:element></xsl:element>.</xsl:if>
  <xsl:if test="page">
  <xsl:choose><xsl:when test="contains(page/text(),'-') or contains(page/text(),',')">
  <xsl:value-of select="$pgs-s"/></xsl:when>
  <xsl:otherwise><xsl:value-of select="$pg-s"/></xsl:otherwise></xsl:choose>.<xsl:text> </xsl:text>
  <xsl:apply-templates select="page/*|page/text()" mode="remap"/>.</xsl:if>
  </xsl:element></xsl:template>
  <xsl:template mode="remap" match="citeref">
    <xsl:variable name="cc" select="@refid" />
    [<xsl:element name="a" namespace="{$ns-xhtml}">
    <xsl:attribute name="href">#<xsl:value-of select="@refid"/></xsl:attribute>
    <xsl:apply-templates mode="remap" select="//cite[@id=$cc]/tag/*|//cite[@id=$cc]/tag/text()" /></xsl:element>
    <xsl:if test="count(*|text())>0">, <xsl:apply-templates select="./*|./text()" mode="remap"/></xsl:if>]
  </xsl:template>
  
  <!-- utility elements for describing tags, environments, and attributes -->
  <xsl:template mode="remap" match="xtag">
  <xsl:element name="code" namespace="{$ns-xhtml}">&lt;<xsl:value-of select="@name"/>&gt;</xsl:element>
  </xsl:template>
  <xsl:template mode="remap" match="xempty">
  <xsl:element name="code" namespace="{$ns-xhtml}">&lt;<xsl:value-of select="@name"/>/&gt;</xsl:element>
  </xsl:template>
  <xsl:template mode="remap" match="xenv">
  <xsl:element name="code" namespace="{$ns-xhtml}">&lt;<xsl:value-of select="@name"/>&gt;...&lt;/<xsl:value-of select="@name"/>&gt;</xsl:element>
  </xsl:template>
  <xsl:template mode="remap" match="xatt">
  <xsl:element name="code" namespace="{$ns-xhtml}"><xsl:value-of select="@name"/>="..."</xsl:element>
  </xsl:template>

</xsl:stylesheet>


