﻿<?xml version="1.0" encoding="utf-8"?>
<!--
  (c) 2010 Nesterovsky Bros. 

  A tree test.
-->
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:tuple="http://www.nesterovsky-bros.com/xslt/tuple"
  xmlns:tree="http://www.nesterovsky-bros.com/xslt/tree"
  xmlns:p="http://www.nesterovsky-bros.com/xslt/private/test"
  exclude-result-prefixes="xs tuple tree p">

  <xsl:include href="tuple.xslt"/>
  <xsl:include href="tree.xslt"/>

  <!-- Entry point. No input is important. -->
  <xsl:template match="/">
    <xsl:variable name="map" as="item()*" select="p:build-tree((), 1, 17)"/>

    <xsl:message select="$map"/>
  </xsl:template>

  <!--
    Builds a tree.
      $tree - a current tree.
      $index - a current index.
      $end-index - end index.
  -->
  <xsl:function name="p:build-tree" as="item()">
    <xsl:param name="tree" as="item()?"/>
    <xsl:param name="index" as="xs:integer"/>
    <xsl:param name="end-index" as="xs:integer"/>

    <xsl:variable name="next-tree" as="item()" select="
      tree:insert-ordered
      (
        $tree,
        (
          (
            if ($index >= 10) then
              xs:string($index)
            else
              concat('0', $index)
          ),
          $index
        ),
        true()
      )"/>

    <xsl:message select="$next-tree"/>

    <xsl:sequence select="
      if ($index >= $end-index) then
        $next-tree
      else
        p:build-tree($next-tree, $index + 1, $end-index)"/>
  </xsl:function>
  
</xsl:stylesheet>
