<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE xsl:stylesheet [
<!ENTITY emdash "&#x2014;">
]>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  extension-element-prefixes="p"
  xmlns:fo="http://www.w3.org/1999/XSL/Format"
  xmlns:p="http://relaxng.org/ns/proofsystem">

<xsl:output encoding="utf-8"/>

<xsl:key name="id" match="clause|annex|referenced-document|li" use="@id"/>

<xsl:variable name="head" select="/document/head"/>

<xsl:template match="this|This">
  <xsl:apply-templates/>
</xsl:template>

<!--
<xsl:template match="this">
  <xsl:call-template name="this"/>
</xsl:template>

<xsl:template match="This">
  <xsl:call-template name="This"/>
</xsl:template>
-->

<xsl:template name="This">
<xsl:choose>
  <xsl:when test="$head/part-number">
    <xsl:text>This part of </xsl:text>
    <xsl:value-of select="$head/organization"/>
    <xsl:text> </xsl:text>
    <xsl:value-of select="$head/document-number"/>
  </xsl:when>
  <xsl:otherwise>This International Standard</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template match="strong">
  <fo:inline font-weight="bold">
    <xsl:apply-templates/>
  </fo:inline>
</xsl:template>

<xsl:template match="notation">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="head">

</xsl:template>

<xsl:template match="foreword">
  <fo:block xsl:use-attribute-sets="Heading1" id="{generate-id()}" break-before="page">Foreword</fo:block>

<fo:block xsl:use-attribute-sets="para">ISO (the International Organization for
Standardization) and IEC (the International Electrotechnical
Commission) form the specialized system for worldwide
standardization. National bodies that are members of ISO or IEC
participate in the development of International Standards through
technical committees established by the respective organization to
deal with particular fields of technical activity. ISO and IEC
technical committees collaborate in fields of mutual interest. Other
international organizations, governmental and non-governmental, in
liaison with ISO and IEC, also take part in the work. In the field of
information technology, ISO and IEC have established a joint technical
committee, ISO/IEC JTC 1.</fo:block>

<fo:block xsl:use-attribute-sets="para">International Standards are drafted in
accordance with the rules given in the ISO/IEC Directives, Part
3.</fo:block>

<fo:block xsl:use-attribute-sets="para">
  <xsl:value-of select="$head/organization"/>
  <xsl:text> </xsl:text>
  <xsl:value-of select="$head/document-number"/>
  <xsl:if test="$head/part-number">
    <xsl:text>-</xsl:text>
    <xsl:value-of select="$head/part-number"/>
  </xsl:if>
  <xsl:text> was prepared by </xsl:text>
  <xsl:if test="$head/tc-number=1">Joint </xsl:if>
  <xsl:text>Technical Committee </xsl:text>
  <xsl:value-of select="$head/organization"/>
  <xsl:text> </xsl:text>
  <xsl:if test="$head/tc-number=1">J</xsl:if>
  <xsl:text>TC </xsl:text>
  <xsl:value-of select="$head/tc-number"/>
  <xsl:text>, </xsl:text>
  <fo:wrapper font-style="italic">
    <xsl:value-of select="$head/tc-name"/>
  </fo:wrapper>
  <xsl:text>, Subcommittee SC </xsl:text>
  <xsl:value-of select="$head/sc-number"/>
  <xsl:text>, </xsl:text>
  <xsl:value-of select="$head/sc-name"/>
  <xsl:text>.</xsl:text>
</fo:block>
  <xsl:apply-templates select="*"/>
</xsl:template>

<xsl:template match="introduction"> 
  <fo:block xsl:use-attribute-sets="Heading1"  id="{generate-id()}" break-before="page">Introduction</fo:block>
  <xsl:apply-templates select="*"/>
</xsl:template>

<xsl:template name="std-title">
  <xsl:if test="$head/title/introductory">
    <xsl:value-of select="$head/title/introductory"/>
    <xsl:text> &emdash; </xsl:text>
  </xsl:if>
  <xsl:value-of select="$head/title/main"/>
  <xsl:if test="$head/title/complementary">
    <xsl:text> &emdash; </xsl:text>
    <xsl:if test="$head/part-number">
      <xsl:text>Part </xsl:text>
      <xsl:value-of select="$head/part-number"/>
      <xsl:text>: </xsl:text>
    </xsl:if>
    <xsl:value-of select="$head/title/complementary"/>
  </xsl:if>
</xsl:template>

<xsl:template name="document-number">
  <xsl:value-of select="$head/organization"/>
  <xsl:text> </xsl:text>
  <xsl:choose>
    <xsl:when test="$head/stage='enquiry'">FCD </xsl:when>
    <xsl:when test="$head/stage='approval'">FDIS </xsl:when>
    <xsl:otherwise/>
  </xsl:choose>
  <xsl:value-of select="$head/document-number"/>
  <xsl:if test="$head/part-number">
    <xsl:text>-</xsl:text>
    <xsl:value-of select="$head/part-number"/>
  </xsl:if>
</xsl:template>

<xsl:template match="scope">
  <fo:block xsl:use-attribute-sets="STDTitle">
    <xsl:call-template name="std-title"/>
  </fo:block>
  <xsl:call-template name="numbered-clause">
    <xsl:with-param name="content">Scope</xsl:with-param>
  </xsl:call-template>
  <xsl:apply-templates select="*"/>
</xsl:template>

<xsl:template match="normative-references">
  <xsl:call-template name="numbered-clause">
    <xsl:with-param name="content">Normative references</xsl:with-param>
  </xsl:call-template>
<fo:block xsl:use-attribute-sets="para">
<xsl:text>The following normative documents contain provisions which,
through reference in this text, constitute provisions of </xsl:text>
<xsl:call-template name="this"/>
<xsl:text>. For dated references, subsequent amendments to, or
revisions of, any of these publications do not apply. However, parties
to agreements based on </xsl:text>
<xsl:call-template name="this"/>
<xsl:text> are encouraged to investigate the possibility of applying
the most recent editions of the normative documents indicated
below. For undated references, the latest edition of the normative
document referred to applies. Members of ISO and IEC maintain
registers of currently valid International Standards.</xsl:text>
</fo:block>
<xsl:apply-templates select="*"/>
</xsl:template>

<xsl:template name="this">
<xsl:choose>
  <xsl:when test="$head/part-number">
    <xsl:text>this part of </xsl:text>
    <xsl:value-of select="$head/organization"/>
    <xsl:text> </xsl:text>
    <xsl:value-of select="$head/document-number"/>
  </xsl:when>
  <xsl:otherwise>this International Standard</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template match="terms-and-definitions">
  <xsl:call-template name="numbered-clause">
    <xsl:with-param name="content">Terms and definitions</xsl:with-param>
  </xsl:call-template>
</xsl:template>

<xsl:template match="clause">
  <xsl:call-template name="numbered-clause">
    <xsl:with-param name="content"><xsl:apply-templates select="title"/></xsl:with-param>
  </xsl:call-template>
  <xsl:apply-templates select="*[not(self::title)]"/>
</xsl:template>

<xsl:template name="numbered-clause">
  <xsl:param name="content"/>
  <xsl:variable name="level" select="count(ancestor::*)"/>
  <xsl:choose>
    <xsl:when test="$level=1">
      <fo:list-block xsl:use-attribute-sets="Heading1">
        <fo:list-item keep-with-next="always">
          <fo:list-item-label>
            <fo:block id="{generate-id()}">
	      <xsl:call-template name="clause-number"/>
            </fo:block>
          </fo:list-item-label>
          <fo:list-item-body>
            <fo:block start-indent=".71cm">
	      <xsl:copy-of select="$content"/>
            </fo:block>
          </fo:list-item-body>
        </fo:list-item>
      </fo:list-block>
    </xsl:when>
    <xsl:when test="$level=2">
      <fo:list-block xsl:use-attribute-sets="Heading2">
        <fo:list-item keep-with-next="always">
          <fo:list-item-label>
            <fo:block id="{generate-id()}">
	      <xsl:call-template name="clause-number"/>
            </fo:block>
          </fo:list-item-label>
          <fo:list-item-body>
            <fo:block start-indent=".95cm">
	      <xsl:copy-of select="$content"/>
            </fo:block>
          </fo:list-item-body>
        </fo:list-item>
      </fo:list-block>
    </xsl:when>
    <xsl:otherwise>
      <fo:list-block xsl:use-attribute-sets="Heading3">
        <fo:list-item keep-with-next="always">
          <fo:list-item-label>
            <fo:block id="{generate-id()}">
	      <xsl:call-template name="clause-number"/>
            </fo:block>
          </fo:list-item-label>
          <fo:list-item-body>
            <fo:block start-indent="{1.16 + ($level - 3)*.5}cm">
	      <xsl:copy-of select="$content"/>
            </fo:block>
          </fo:list-item-body>
        </fo:list-item>
      </fo:list-block>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>


<xsl:template name="clause-number">
  <xsl:choose>
    <xsl:when test="ancestor::annex">
      <xsl:number count="annex" format="A"/>
      <xsl:text>.</xsl:text>
      <xsl:number count="clause" level="multiple" format="1.1"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:number count="scope|normative-references|terms-and-definitions|clause" level="multiple" format="1.1"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template match="p">
  <fo:block xsl:use-attribute-sets="para"><xsl:apply-templates/></fo:block>
</xsl:template>

<xsl:attribute-set name="pre">
  <xsl:attribute name="white-space-collapse">false</xsl:attribute>
  <xsl:attribute name="linefeed-treatment">preserve</xsl:attribute>
  <xsl:attribute name="white-space-treatment">preserve</xsl:attribute>
  <xsl:attribute name="wrap-option">no-wrap</xsl:attribute>
  <xsl:attribute name="text-align">left</xsl:attribute>
  <xsl:attribute name="font-family">Courier, LucidaUnicode</xsl:attribute>
  <xsl:attribute name="font-selection-strategy">character-by-character</xsl:attribute>
</xsl:attribute-set>

<xsl:template match="pre">
  <fo:block xsl:use-attribute-sets="pre para">
    <xsl:apply-templates/>
  </fo:block>
</xsl:template>

<xsl:template match="note/p[1]">
  <fo:list-block xsl:use-attribute-sets="Note">
    <fo:list-item>
      <fo:list-item-label>
	<fo:block>NOTE</fo:block>
      </fo:list-item-label>
      <fo:list-item-body>
	<fo:block text-indent="1.69cm">
          <xsl:apply-templates/>
	</fo:block>
      </fo:list-item-body>
    </fo:list-item>
  </fo:list-block>
</xsl:template>

<xsl:template match="example|note">
  <fo:wrapper font-size="9pt">
    <xsl:apply-templates/>
  </fo:wrapper>
</xsl:template>

<xsl:template match="example/p[1]">
  <fo:block>
    <xsl:text>EXAMPLE</xsl:text>
    <!--tab-stop-->
    <xsl:apply-templates/>
  </fo:block>
</xsl:template>

<xsl:template match="ul|ol">
  <fo:list-block xsl:use-attribute-sets="para">
    <xsl:apply-templates select="*"/>
  </fo:list-block>
</xsl:template>

<xsl:template match="li">
  <fo:list-item>
    <fo:list-item-label end-indent="label-end()">
       <fo:block>&emdash;</fo:block>
    </fo:list-item-label>
    <fo:list-item-body start-indent="body-start()">
      <xsl:apply-templates select="*"/>
    </fo:list-item-body>
  </fo:list-item>
</xsl:template>

<xsl:template match="url">
  <xsl:text>available at &lt;</xsl:text>
  <fo:basic-link external-destination="{.}">
    <xsl:apply-templates/>
  </fo:basic-link>
  <xsl:text>&gt;</xsl:text>
</xsl:template>

<xsl:template match="title" mode="field">
  <fo:wrapper font-style="italic"><xsl:apply-templates/></fo:wrapper>
</xsl:template>

<xsl:template match="var">
  <fo:wrapper font-style="italic"><xsl:apply-templates/></fo:wrapper>
</xsl:template>

<xsl:template match="code">
  <fo:wrapper font-family="Courier"><xsl:apply-templates/></fo:wrapper>
</xsl:template>

<xsl:template match="xref">
  <xsl:apply-templates select="key('id',@to)" mode="xref"/>
</xsl:template>

<xsl:template match="Xref">
  <xsl:apply-templates select="key('id',@to)" mode="Xref"/>
</xsl:template>

<xsl:template match="*" mode="Xref">
  <xsl:apply-templates mode="xref" select="."/>
</xsl:template>

<xsl:template match="clause//clause|annex//clause" mode="xref">
  <fo:basic-link internal-destination="{generate-id()}">
    <xsl:call-template name="clause-number"/>
  </fo:basic-link>
</xsl:template>

<xsl:template match="clause" mode="xref">
  <fo:basic-link internal-destination="{generate-id()}">
    <xsl:text>Clause </xsl:text>
    <xsl:call-template name="clause-number"/>
  </fo:basic-link>
</xsl:template>

<xsl:template match="annex" mode="xref">
  <fo:basic-link internal-destination="{generate-id()}">
    <xsl:text>Annex </xsl:text>
    <xsl:number format="A"/>
  </fo:basic-link>
</xsl:template>

<xsl:template match="referenced-document" mode="xref">
  <fo:basic-link internal-destination="{generate-id()}">
    <xsl:value-of select="abbrev"/>
  </fo:basic-link>
</xsl:template>

<xsl:template match="bibliography/referenced-document" mode="xref">
  <fo:basic-link internal-destination="{generate-id()}">
    <xsl:value-of select="abbrev"/>
    <fo:inline font-size="8pt" baseline-shift="3pt">[<xsl:number/>]</fo:inline>
  </fo:basic-link>
</xsl:template>

<xsl:template match="referenced-document">
  <fo:block xsl:use-attribute-sets="para" id="{generate-id()}">
    <xsl:for-each select="*">
      <xsl:if test="not(position()=1)">, </xsl:if>
      <xsl:apply-templates select="." mode="field"/>
    </xsl:for-each>
  </fo:block>
</xsl:template>

<xsl:template match="annex">
<fo:block xsl:use-attribute-sets="ANNEX" break-before="page">
<xsl:text>Annex </xsl:text>
<xsl:number format="A"/>
</fo:block>

<fo:block xsl:use-attribute-sets="ANNEX" font-weight="normal">
<xsl:text>(</xsl:text>
<xsl:choose>
  <xsl:when test="@normative='true'">normative</xsl:when>
  <xsl:otherwise>informative</xsl:otherwise>
</xsl:choose>
<xsl:text>)</xsl:text>
</fo:block>
<fo:block xsl:use-attribute-sets="ANNEX" space-before="15.5pt" space-after="38pt" id="{generate-id()}">
<xsl:apply-templates select="title"/>
</fo:block>
<xsl:apply-templates select="*[not(self::title)]"/>
</xsl:template>

<xsl:template match="bibliography">

<fo:block xsl:use-attribute-sets="ANNEX" break-before="page" space-after="38pt" id="{generate-id()}">
<xsl:text>Bibliography</xsl:text>
</fo:block>

<fo:list-block provisional-distance-between-starts="1.16cm">
  <xsl:for-each select="referenced-document">
    <fo:list-item xsl:use-attribute-sets="para" id="{generate-id()}">
      <fo:list-item-label end-indent="label-end()">
	 <fo:block>[<xsl:value-of select="position()"/>]</fo:block>
      </fo:list-item-label>
      <fo:list-item-body start-indent="body-start()">
	<fo:block>
	  <xsl:for-each select="*[not(self::abbrev)]">
	    <xsl:if test="not(position()=1)">, </xsl:if>
	    <xsl:apply-templates select="." mode="field"/>
	  </xsl:for-each>
        </fo:block>
      </fo:list-item-body>
    </fo:list-item>
  </xsl:for-each>
</fo:list-block>
</xsl:template>

<xsl:template match="notation-list">
  <fo:list-block>
    <xsl:for-each select="notation-item">
      <fo:list-item xsl:use-attribute-sets="para">
        <fo:list-item-label end-indent="label-end()">
          <fo:block>&emdash;</fo:block>
        </fo:list-item-label>
        <fo:list-item-body start-indent="body-start()">
          <fo:block>
   	    <xsl:apply-templates select="notation"/>
	    <xsl:text> </xsl:text>
	    <xsl:apply-templates select="notation-definition/p[1]/node()"/>
          </fo:block>
	  <xsl:apply-templates select="notation-definition/*[position()!=1]"/>
        </fo:list-item-body>
      </fo:list-item>
    </xsl:for-each>
  </fo:list-block>
</xsl:template>

<xsl:template match="p:*"/>

<xsl:attribute-set name="A4">
 <xsl:attribute name="page-width">210mm</xsl:attribute>
 <xsl:attribute name="page-height">297mm</xsl:attribute>
</xsl:attribute-set>

<xsl:attribute-set name="default">
  <xsl:attribute name="font-family">Helvetica, LucidaUnicode</xsl:attribute>
  <xsl:attribute name="font-size">10pt</xsl:attribute>
  <xsl:attribute name="line-height">11.5pt</xsl:attribute>
  <xsl:attribute name="text-align">justify</xsl:attribute>
  <xsl:attribute name="language">en</xsl:attribute>
  <xsl:attribute name="country">US</xsl:attribute>
  <xsl:attribute name="orphans">2</xsl:attribute>
  <xsl:attribute name="widows">2</xsl:attribute>
  <xsl:attribute name="provisional-distance-between-starts">.71cm</xsl:attribute>
  <xsl:attribute name="font-selection-strategy">character-by-character</xsl:attribute>
</xsl:attribute-set>

<xsl:attribute-set name="para">
  <xsl:attribute name="space-after">12pt</xsl:attribute>
</xsl:attribute-set>

<xsl:attribute-set name="Note" use-attribute-sets="para">
  <xsl:attribute name="font-size">9pt</xsl:attribute>
  <xsl:attribute name="line-height">10.5pt</xsl:attribute>
</xsl:attribute-set>

<xsl:attribute-set name="STDTitle">
  <xsl:attribute name="text-align">left</xsl:attribute>
  <xsl:attribute name="font-size">16pt</xsl:attribute>
  <xsl:attribute name="font-weight">bold</xsl:attribute>
  <xsl:attribute name="hyphenate">false</xsl:attribute>
  <xsl:attribute name="line-height">13.5pt</xsl:attribute>
  <xsl:attribute name="space-before">20pt</xsl:attribute>
  <xsl:attribute name="space-after">17.5pt</xsl:attribute>
</xsl:attribute-set>

<xsl:attribute-set name="Heading1">
  <xsl:attribute name="text-align">left</xsl:attribute>
  <xsl:attribute name="font-size">12pt</xsl:attribute>
  <xsl:attribute name="font-weight">bold</xsl:attribute>
  <xsl:attribute name="hyphenate">false</xsl:attribute>
  <xsl:attribute name="line-height">13.5pt</xsl:attribute>
  <xsl:attribute name="space-before">13.5pt</xsl:attribute>
  <xsl:attribute name="space-after">12pt</xsl:attribute>
</xsl:attribute-set>

<xsl:attribute-set name="Heading2" use-attribute-sets="Heading1">
  <xsl:attribute name="font-size">11pt</xsl:attribute>
  <xsl:attribute name="line-height">12.5pt</xsl:attribute>
  <xsl:attribute name="space-before">3pt</xsl:attribute>
</xsl:attribute-set>

<xsl:attribute-set name="Heading3" use-attribute-sets="Heading1">
  <xsl:attribute name="font-size">10pt</xsl:attribute>
  <xsl:attribute name="line-height">11.5pt</xsl:attribute>
  <xsl:attribute name="space-before">3pt</xsl:attribute>
</xsl:attribute-set>

<xsl:attribute-set name="ANNEX">
  <xsl:attribute name="text-align">center</xsl:attribute>
  <xsl:attribute name="font-size">14pt</xsl:attribute>
  <xsl:attribute name="font-weight">bold</xsl:attribute>
  <xsl:attribute name="line-height">15.5pt</xsl:attribute>
</xsl:attribute-set>

<xsl:attribute-set name="VerticalMargins">
  <xsl:attribute name="margin-top">1.25cm</xsl:attribute>
  <xsl:attribute name="margin-bottom">.89cm</xsl:attribute>
</xsl:attribute-set>

<xsl:attribute-set name="RightPageMargins" use-attribute-sets="VerticalMargins">
  <xsl:attribute name="margin-left">1.7cm</xsl:attribute>
  <xsl:attribute name="margin-right">1.3cm</xsl:attribute>
</xsl:attribute-set>

<xsl:attribute-set name="LeftPageMargins" use-attribute-sets="VerticalMargins">
  <xsl:attribute name="margin-left">1.3cm</xsl:attribute>
  <xsl:attribute name="margin-right">1.7cm</xsl:attribute>
</xsl:attribute-set>

<xsl:attribute-set name="BodyMargins">
  <xsl:attribute name="margin-top">48pt</xsl:attribute>
  <xsl:attribute name="margin-bottom">38pt</xsl:attribute>
</xsl:attribute-set>

<xsl:attribute-set name="BodyFooterPageNumber">
  <xsl:attribute name="font-size">11pt</xsl:attribute>
  <xsl:attribute name="font-weight">bold</xsl:attribute>
</xsl:attribute-set>

<xsl:attribute-set name="FrontFooterPageNumber">
  <xsl:attribute name="font-size">10pt</xsl:attribute>
</xsl:attribute-set>

<xsl:attribute-set name="BodyHeader">
  <xsl:attribute name="font-size">11pt</xsl:attribute>
  <xsl:attribute name="font-weight">bold</xsl:attribute>
</xsl:attribute-set>

<xsl:attribute-set name="BodyFirstPageHeader" use-attribute-sets="BodyHeader">
  <xsl:attribute name="font-size">11pt</xsl:attribute>
  <xsl:attribute name="font-weight">bold</xsl:attribute>
  <xsl:attribute name="line-height">11.5pt</xsl:attribute>
  <xsl:attribute name="padding-top">6pt</xsl:attribute>
  <xsl:attribute name="padding-bottom">6pt</xsl:attribute>
  <xsl:attribute name="border-top-style">solid</xsl:attribute>
  <xsl:attribute name="border-bottom-style">solid</xsl:attribute>
  <xsl:attribute name="border-top-width">2.25pt</xsl:attribute>
  <xsl:attribute name="border-bottom-width">2.25pt</xsl:attribute>
</xsl:attribute-set>

<xsl:attribute-set name="FooterCopyright">
  <xsl:attribute name="font-size">8pt</xsl:attribute>
</xsl:attribute-set>

<xsl:template match="/">
  <fo:root xsl:use-attribute-sets="default">
    <fo:layout-master-set>
      <fo:simple-page-master master-name="LeftPage" xsl:use-attribute-sets="A4 LeftPageMargins">
        <fo:region-body xsl:use-attribute-sets="BodyMargins"/>
        <fo:region-before region-name="LeftPageHeader" extent="48pt"/>
        <fo:region-after region-name="LeftPageFooter" extent="14pt"/>
      </fo:simple-page-master>
      <fo:simple-page-master master-name="RightPage" xsl:use-attribute-sets="A4 RightPageMargins">
        <fo:region-body xsl:use-attribute-sets="BodyMargins"/>
        <fo:region-before region-name="RightPageHeader" extent="48pt"/>
        <fo:region-after region-name="RightPageFooter" extent="14pt"/>
      </fo:simple-page-master>
      <fo:simple-page-master master-name="FirstPage" xsl:use-attribute-sets="A4 RightPageMargins">
        <fo:region-body xsl:use-attribute-sets="BodyMargins"/>
        <fo:region-before region-name="FirstPageHeader" extent="48pt"/>
        <fo:region-after region-name="RightPageFooter" extent="14pt"/>
      </fo:simple-page-master>
      <fo:page-sequence-master master-name="Front">
         <fo:repeatable-page-master-alternatives>
           <fo:conditional-page-master-reference master-reference="RightPage" odd-or-even="odd"/>
           <fo:conditional-page-master-reference master-reference="LeftPage" odd-or-even="even"/>
         </fo:repeatable-page-master-alternatives>
      </fo:page-sequence-master>
      <fo:page-sequence-master master-name="Body">
         <fo:repeatable-page-master-alternatives>
           <fo:conditional-page-master-reference master-reference="FirstPage" page-position="first"/>
           <fo:conditional-page-master-reference master-reference="RightPage" odd-or-even="odd"/>
           <fo:conditional-page-master-reference master-reference="LeftPage" odd-or-even="even"/>
         </fo:repeatable-page-master-alternatives>
      </fo:page-sequence-master>
    </fo:layout-master-set>
    <fo:page-sequence master-reference="Front" format="i" initial-page-number="3">
      <fo:static-content flow-name="LeftPageHeader">
        <fo:block xsl:use-attribute-sets="BodyHeader" text-align="left">
          <xsl:call-template name="document-number"/>
        </fo:block>
      </fo:static-content>
      <fo:static-content flow-name="RightPageHeader">
        <fo:block xsl:use-attribute-sets="BodyHeader" text-align="right">
          <xsl:call-template name="document-number"/>
        </fo:block>
      </fo:static-content>
      <fo:static-content flow-name="LeftPageFooter">
         <fo:list-block>
           <fo:list-item relative-align="baseline">
             <fo:list-item-label>
                <fo:block xsl:use-attribute-sets="FrontFooterPageNumber" text-align="left">
                  <fo:page-number/>
                </fo:block>
             </fo:list-item-label>
             <fo:list-item-body>
                <fo:block xsl:use-attribute-sets="FooterCopyright" text-align="right">
                  <xsl:call-template name="all-rights-reserved"/>
                </fo:block>
             </fo:list-item-body>
           </fo:list-item>
         </fo:list-block> 
      </fo:static-content>
      <fo:static-content flow-name="RightPageFooter">
         <fo:list-block>
           <fo:list-item relative-align="baseline">
             <fo:list-item-label>
                <fo:block xsl:use-attribute-sets="FrontFooterPageNumber" text-align="right">
                  <fo:page-number/>
                </fo:block>
             </fo:list-item-label>
             <fo:list-item-body>
                <fo:block xsl:use-attribute-sets="FooterCopyright" text-align="left">
                  <xsl:call-template name="all-rights-reserved"/>
                </fo:block>
             </fo:list-item-body>
           </fo:list-item>
         </fo:list-block> 
      </fo:static-content>
      <fo:flow flow-name="xsl-region-body">
        <xsl:apply-templates select="document" mode="toc"/>
        <xsl:apply-templates select="document/head"/>
        <xsl:apply-templates select="document/foreword"/>
        <xsl:apply-templates select="document/introduction"/>
      </fo:flow>
    </fo:page-sequence>
    <fo:page-sequence master-reference="Body" initial-page-number="1">
      <fo:static-content flow-name="LeftPageHeader">
        <fo:block xsl:use-attribute-sets="BodyHeader" text-align="left">
          <xsl:call-template name="document-number"/>
        </fo:block>
      </fo:static-content>
      <fo:static-content flow-name="FirstPageHeader">
         <fo:list-block xsl:use-attribute-sets="BodyFirstPageHeader">
           <fo:list-item relative-align="baseline">
             <fo:list-item-label>
                <fo:block text-align="left">
                  <xsl:text>DRAFT INTERNATIONAL STANDARD</xsl:text>
                </fo:block>
             </fo:list-item-label>
             <fo:list-item-body>
                <fo:block text-align="right">
                  <xsl:call-template name="document-number"/>
                </fo:block>
             </fo:list-item-body>
           </fo:list-item>
         </fo:list-block> 
      </fo:static-content>
      <fo:static-content flow-name="RightPageHeader">
        <fo:block xsl:use-attribute-sets="BodyHeader" text-align="right">
          <xsl:call-template name="document-number"/>
        </fo:block>
      </fo:static-content>
      <fo:static-content flow-name="LeftPageFooter">
         <fo:list-block>
           <fo:list-item relative-align="baseline">
             <fo:list-item-label>
                <fo:block xsl:use-attribute-sets="BodyFooterPageNumber" text-align="left">
                  <fo:page-number/>
                </fo:block>
             </fo:list-item-label>
             <fo:list-item-body>
                <fo:block xsl:use-attribute-sets="FooterCopyright" text-align="right">
                  <xsl:call-template name="all-rights-reserved"/>
                </fo:block>
             </fo:list-item-body>
           </fo:list-item>
         </fo:list-block> 
      </fo:static-content>
      <fo:static-content flow-name="RightPageFooter">
         <fo:list-block>
           <fo:list-item relative-align="baseline">
             <fo:list-item-label>
                <fo:block xsl:use-attribute-sets="BodyFooterPageNumber" text-align="right">
                  <fo:page-number/>
                </fo:block>
             </fo:list-item-label>
             <fo:list-item-body>
                <fo:block xsl:use-attribute-sets="FooterCopyright" text-align="left">
                  <xsl:call-template name="all-rights-reserved"/>
                </fo:block>
             </fo:list-item-body>
           </fo:list-item>
         </fo:list-block> 
      </fo:static-content>
      <fo:flow flow-name="xsl-region-body">
        <xsl:apply-templates select="document/*[not(self::foreword|self::introduction|self::head)]"/>
      </fo:flow>
    </fo:page-sequence>
    <fo:page-sequence master-reference="Body">
      <fo:flow flow-name="xsl-region-body">
        <fo:block font-size="2em" space-after="1em" color="red">
          <xsl:text>Summary of editorial comments:</xsl:text>
        </fo:block>
        <xsl:for-each select="//ed">
          <fo:block>
            <fo:basic-link internal-destination="note-{generate-id(.)}">
              <fo:block space-after="1em" id="summ-{generate-id(.)}">
                <xsl:text>[</xsl:text>
      <xsl:number count="scope|normative-references|terms-and-definitions|clause" level="multiple" format="1.1"/>

                <xsl:text>] </xsl:text>
               <xsl:apply-templates select="ancestor::clause[1]/title/node()"/>
             </fo:block>
             <xsl:apply-templates/>
           </fo:basic-link>
         </fo:block>
        </xsl:for-each>
      </fo:flow>
    </fo:page-sequence>
  </fo:root>
</xsl:template>

<xsl:template match="title">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="b">
  <fo:inline font-weight="bold"><xsl:apply-templates/></fo:inline>
</xsl:template>

<xsl:template match="i">
  <fo:inline font-style="italic"><xsl:apply-templates/></fo:inline>
</xsl:template>

<xsl:template match="part-list">
  <xsl:for-each select="part">
    <fo:block start-indent=".3cm" font-style="italic" 
              xsl:use-attribute-sets="para">&emdash; Part 
      <xsl:value-of select="number"/>:
      <xsl:apply-templates select="title"/>
    </fo:block>
  </xsl:for-each>
</xsl:template>

<xsl:template match="ed">
  <fo:block color="red">
    <fo:basic-link internal-destination="summ-{generate-id(.)}" 
                   id="note-{generate-id(.)}">
      <xsl:apply-templates/>
    </fo:basic-link>
  </fo:block>
</xsl:template>

<xsl:template name="all-rights-reserved">
   <xsl:text>© </xsl:text>
   <xsl:value-of select="$head/organization"/>
   <xsl:text> </xsl:text>
   <xsl:value-of select="substring-before($head/date, '-')"/>
   <xsl:text> &#8211; All rights reserved</xsl:text>
   <xsl:if test="$head/stage='preparatory'">
     <xsl:text> (Preparatory draft: </xsl:text>
     <xsl:value-of select="$head/date"/>
     <xsl:text>)</xsl:text>
   </xsl:if>
</xsl:template>

<xsl:template name="copyright-footer">
  <fo:wrapper font-size="8pt"><xsl:call-template name="all-rights-reserved"/></fo:wrapper>
</xsl:template>

<xsl:template match="document" mode="toc">
  <fo:list-block space-before="48pt" space-after="15.5pt" line-height="15.5pt" break-before="page">
    <fo:list-item relative-align="baseline">
      <fo:list-item-label>
	 <fo:block font-size="14pt" font-weight="bold" text-align="left">
	   <xsl:text>Contents</xsl:text>
	 </fo:block>
      </fo:list-item-label>
      <fo:list-item-body>
	 <fo:block text-align="right">
	   <xsl:text>Page</xsl:text>
	 </fo:block>
      </fo:list-item-body>
    </fo:list-item>
  </fo:list-block>
  <fo:wrapper font-weight="bold">
    <xsl:apply-templates select="foreword" mode="toc"/>
    <xsl:apply-templates select="introduction" mode="toc"/>
    <xsl:apply-templates select="*[not(self::foreword|self::introduction|self::head)]" mode="toc"/>
  </fo:wrapper>
</xsl:template>

<xsl:attribute-set name="TOC">
</xsl:attribute-set>

<xsl:template match="foreword" mode="toc">
  <xsl:call-template name="toc">
    <xsl:with-param name="content">Foreword</xsl:with-param>
  </xsl:call-template>
</xsl:template>

<xsl:template match="introduction" mode="toc">
  <xsl:call-template name="toc">
    <xsl:with-param name="content">Introduction</xsl:with-param>
  </xsl:call-template>
</xsl:template>

<xsl:template match="bibliography" mode="toc">
  <xsl:call-template name="toc">
    <xsl:with-param name="content">Bibliography</xsl:with-param>
  </xsl:call-template>
</xsl:template>

<xsl:template match="scope" mode="toc">
  <xsl:call-template name="toc">
    <xsl:with-param name="content">Scope</xsl:with-param>
    <xsl:with-param name="number"><xsl:call-template name="clause-number"/></xsl:with-param>
  </xsl:call-template>
</xsl:template>

<xsl:template match="normative-references" mode="toc">
  <xsl:call-template name="toc">
    <xsl:with-param name="content">Normative references</xsl:with-param>
    <xsl:with-param name="number"><xsl:call-template name="clause-number"/></xsl:with-param>
  </xsl:call-template>
</xsl:template>

<xsl:template match="terms-and-definitions" mode="toc">
  <xsl:call-template name="toc">
    <xsl:with-param name="content">Terms and definitions</xsl:with-param>
    <xsl:with-param name="number"><xsl:call-template name="clause-number"/></xsl:with-param>
  </xsl:call-template>
</xsl:template>

<xsl:template match="clause" mode="toc">
  <xsl:call-template name="toc">
    <xsl:with-param name="content"><xsl:apply-templates select="title"/></xsl:with-param>
    <xsl:with-param name="number"><xsl:call-template name="clause-number"/></xsl:with-param>
  </xsl:call-template>
</xsl:template>

<xsl:template match="annex" mode="toc">
  <xsl:call-template name="toc">
    <xsl:with-param name="content">
      <xsl:text>Annex </xsl:text>
      <xsl:number count="annex" format="A"/>
      <fo:wrapper font-weight="normal">
	<xsl:text> (</xsl:text>
	<xsl:choose>
	  <xsl:when test="@normative='true'">normative</xsl:when>
	  <xsl:otherwise>informative</xsl:otherwise>
	</xsl:choose>
	<xsl:text>) </xsl:text>
      </fo:wrapper>
      <xsl:apply-templates select="title"/>
    </xsl:with-param>
  </xsl:call-template>
</xsl:template>

<xsl:template match="clause//clause|annex//clause" mode="toc">
  <xsl:call-template name="toc">
    <xsl:with-param name="content"><xsl:apply-templates select="title"/></xsl:with-param>
    <xsl:with-param name="number"><xsl:call-template name="clause-number"/></xsl:with-param>
    <xsl:with-param name="top" select="false()"/>
  </xsl:call-template>
</xsl:template>

<xsl:template name="toc">
  <xsl:param name="content"/>
  <xsl:param name="number" select="''"/>
  <xsl:param name="top" select="true()"/>
  <xsl:choose>
    <xsl:when test="$number">
      <fo:list-block>
	<xsl:if test="$top">
	  <xsl:attribute name="space-before">6pt</xsl:attribute>
	</xsl:if>
	<fo:list-item>
	  <fo:list-item-label>
	    <fo:block text-align="left"><xsl:copy-of select="$number"/></fo:block>
	  </fo:list-item-label>
	  <fo:list-item-body start-indent="1.27cm">
	    <xsl:call-template name="toc-line">
	      <xsl:with-param name="content" select="$content"/>
	      <xsl:with-param name="top" select="false()"/>
	    </xsl:call-template>
	  </fo:list-item-body>
	</fo:list-item>
      </fo:list-block>
    </xsl:when>
    <xsl:otherwise>
      <xsl:call-template name="toc-line">
	<xsl:with-param name="content" select="$content"/>
	<xsl:with-param name="top" select="$top"/>
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
  <xsl:apply-templates select="clause" mode="toc"/>
</xsl:template>

<xsl:template name="toc-line">
  <xsl:param name="content"/>
  <xsl:param name="top" select="true()"/>
  <fo:block text-align-last="justify">
    <xsl:if test="$top">
      <xsl:attribute name="space-before">6pt</xsl:attribute>
    </xsl:if>
    <fo:basic-link internal-destination="{generate-id()}">
      <xsl:copy-of select="$content"/>
    </fo:basic-link>
    <fo:leader leader-pattern="dots"/>
    <xsl:text> </xsl:text>
    <fo:page-number-citation ref-id="{generate-id()}"/>
  </fo:block>
</xsl:template>

<xsl:template match="*" mode="toc"/>

<xsl:template match="*">
  <xsl:message>
    <xsl:text disable-output-escaping="yes"
                                  >Construct not accommodated: &lt;</xsl:text>
      <xsl:value-of select="name(.)"/>
      <xsl:text disable-output-escaping="yes">&gt;</xsl:text>
  </xsl:message>
</xsl:template>

</xsl:stylesheet>

