<?xml version="1.0" encoding="UTF-8"?>
<Layout xmlns="urn:speedata.de:2009/publisher/en"
  xmlns:sd="urn:speedata:2009/publisher/functions/en">
  <Options
    show-gridallocation="no"
    show-grid="no"
    trace="no"
  />

  <LoadFontfile name="Helvetica" marginprotrusion="100" filename="texgyreheros-regular.otf" />
  <LoadFontfile name="Helvetica Bold" filename="texgyreheros-bold.otf" />
  <LoadFontfile name="Helvetica Italic" filename="texgyreheros-italic.otf" />
  <LoadFontfile name="Helvetica Bold Italic" filename="texgyreheros-bolditalic.otf" />


  <DefineFontfamily name="Title" fontsize="12" leading="16">
    <Regular fontface="Helvetica"></Regular>
    <Bold fontface="Helvetica Bold" />
  </DefineFontfamily>

  <DefineFontfamily name="text" fontsize="12" leading="13">
    <Regular fontface="Helvetica" />
    <Bold fontface="Helvetica Bold" />
  </DefineFontfamily>

  <Pageformat width="150mm" height="200mm"/>
  <SetGrid width="4mm" height="14pt"/>

  <DefineTextformat name="leftaligned" alignment="leftaligned"/>
  <DefineTextformat name="rightaligned" alignment="rightaligned"/>
  <DefineTextformat name="text" alignment="justified" />
  <DefineTextformat name="text with indentation" alignment="justified" indentation="2em" />

  <Pagetype name="right page" test="sd:odd( sd:current-page() )">
    <Margin left="2cm" right="1cm" top="1cm" bottom="1cm" />
    <AtPageShipout>
      <PlaceObject column="{ sd:number-of-columns() - 3 }" row="{ sd:number-of-rows() - 1}">
        <Textblock width="3">
          <Paragraph fontface="text" textformat="rightaligned"><Value select="sd:current-page()"/></Paragraph>
        </Textblock>
      </PlaceObject>
      <Message select="sd:current-page()" />
    </AtPageShipout>
  </Pagetype>

  <Pagetype name="left page" test="sd:even( sd:current-page() )">
    <Margin left="1cm" right="2cm" top="1cm" bottom="1cm" />
    <AtPageShipout>
      <PlaceObject column="1" row="{ sd:number-of-rows() - 1 }">
        <Textblock width="3">
          <Paragraph fontface="text" textformat="leftaligned"><Value select="sd:current-page()"/></Paragraph>
        </Textblock>
      </PlaceObject>
      <Message select="sd:current-page()" />
    </AtPageShipout>
  </Pagetype>


  <Record element="tableofcontents">
    <PlaceObject column="3">
      <Textblock width="20" fontface="Title">
        <Paragraph>
          <Value>Contents</Value>
        </Paragraph>
      </Textblock>
    </PlaceObject>
    <NextRow/>
    <PlaceObject column="3">
      <Textblock width="20">
        <ForAll select="planetlisting">
          <Paragraph>
            <Value select="@name" />
            <Value>, page </Value>
            <Value select="@pagenumber" />
          </Paragraph>
        </ForAll>
      </Textblock>
    </PlaceObject>
  </Record>

  <!-- Root element -->
  <Record element="planets">
    <SetVariable variable="column" select="2" />
    <LoadDataset name="toc"/>
    <SetVariable variable="contents"/>
    <NewPage/>

    <ProcessNode select="planet" />

    <SaveDataset filename="toc" elementname="tableofcontents" select="$contents" />

  </Record>

  <Record element="planet">
    <SetVariable variable="contents">
      <Copy-of select="$contents" />
      <Element name="planetlisting">
        <Attribute name="name" select=" @name " />
        <Attribute name="pagenumber" select=" sd:current-page()" />
      </Element>
    </SetVariable>

    <PlaceObject column="1">
      <Textblock width="20">
        <Bookmark select="@name" level="1"/>
        <Paragraph>
          <Value select="@name" />
        </Paragraph>
      </Textblock>
    </PlaceObject>

    <ProcessNode select="url" />
    <ProcessNode select="image" />
    <ProcessNode select="moons" />
    <ProcessNode select="description" />

    <NewPage />
  </Record>

  <Record element="url">
    <PlaceObject column="{ $column }">
      <Textblock width="29" textformat="text" fontface="Title">
        <Paragraph>
          <Value>URL: </Value>
          <A href="{ string(.) }">
            <Value select="string(.)" />
          </A>
        </Paragraph>
      </Textblock>
    </PlaceObject>

  </Record>

  <Record element="image">
    <NextRow />
    <NextRow />
    <PlaceObject column="{ $column }">
      <Image width="10" file="{.}" />
    </PlaceObject>
  </Record>

  <Record element="moons">
    <PlaceObject column="18">
      <Textblock width="7">
        <Paragraph>
          <B>
            <Value>Moons</Value>
          </B>
        </Paragraph>
        <Ul>
          <ForAll select="moon">
            <Li><Value select="string(.)"></Value></Li>
          </ForAll>
        </Ul>
      </Textblock>
    </PlaceObject>
    <NextRow />
    <NextRow />
  </Record>

  <Record element="description">
    <PlaceObject column="{ $column }">
      <Textblock width="28" fontface="text" textformat="text with indentation">
        <Paragraph>
          <Value select="normalize-space(string(.))" />
        </Paragraph>
      </Textblock>
    </PlaceObject>
  </Record>

</Layout>
