<transform 
    xmlns:xsl="http://www.w3.org/1999/XSL/TransformAlias"
    xmlns="http://www.w3.org/1999/XSL/Transform"
    xmlns:sel="http://www.w3.org/2004/06/diselect"
    xmlns:html="http://www.w3.org/1999/xhtml"
    version="1.0">

<!-- 
this stylesheet transforms a DI select into an XSLT stylesheet.
Basically, it is an identity transform execpt that the DISelect
constructs (if, choice, etc.) are mapped to XSLT if, choice, etc.
-->

<param name="default-selid-attribute-name" select="'html:id'"/>
<!-- From DISelect: 
"Any language profile which includes DISelect must specify the default name of the attribute generated from the selid attribute by default. Typically this will be the name of the unique identifier for the host language."
-->

<namespace-alias stylesheet-prefix="xsl" result-prefix="#default"/>

<output indent="yes"/>

<template match="/*">
  <copy>
    <attribute name="version" namespace="http://www.w3.org/1999/XSL/Transform">1.0</attribute>
    <attribute name="exclude-result-prefixes" namespace="http://www.w3.org/1999/XSL/Transform">dcn sel</attribute>
    <apply-templates select="@*|node()"/>
  </copy>
</template>

<template match="@*|node()">
  <copy>
    <apply-templates select="@*|node()"/>
  </copy>
</template>


<!-- ############################################################ -->
<!-- 4 Attributes for Conditional Processing
    http://www.w3.org/TR/cselection/#sec-attributes-cond-pocessing -->

<template match="*[@sel:expr]">
  <xsl:if test="{@sel:expr}">
    <copy>
      <apply-templates select="@*|node()"/>
    </copy>
  </xsl:if>
</template>

<template match="*[@sel:selid]">
  <copy>
    <apply-templates select="@*|node()"/>
    <variable name="idreplace">
      <choose>
        <when test="ancestor-or-self::*/@sel:selidName">
          <value-of select="ancestor-or-self::*/@sel:selidName"/>
        </when>
        <otherwise>
          <value-of select="$default-selid-attribute-name"/>
        </otherwise>
      </choose>
    </variable>

    <xsl:attribute name="{$idreplace}"><value-of select="@sel:selid"/></xsl:attribute>
  </copy>
</template>


<!-- don't copy the attributes -->
<template match="@sel:expr"/>
<template match="@sel:selid"/>
<template match="@sel:selidName"/>

<!-- ############################################################ -->
<!-- 5 Elements for Conditional Processing
http://www.w3.org/TR/cselection/#sec-element-cond-processing -->

<template match="sel:if">
  <xsl:if test="{@expr}">
    <apply-templates select="node()"/>
  </xsl:if>
</template>

<template match="sel:select[@expr]">
  <xsl:if test="{@expr}">
    <call-template name="select-contents"/>
  </xsl:if>
</template>

<template match="sel:select">
  <call-template name="select-contents"/>
</template>

<template name="select-contents">
  <choose>
    <when test="not(@precept) or @precept='matchfirst'">
      <xsl:choose>
        <apply-templates select="node()"/>
      </xsl:choose>
    </when>
    <when test="@precept='matchevery'">
      <apply-templates select="sel:when" mode="matchevery"/>
      <apply-templates select="sel:otherwise" mode="matchevery"/>
    </when>
  </choose>
</template>

<template match="sel:when">
  <xsl:when test="{@expr}">
    <apply-templates/>
  </xsl:when>
</template>

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

<template match="sel:when" mode="matchevery">
  <xsl:if test="{@expr}">
    <apply-templates/>
  </xsl:if>
</template>

<template match="sel:otherwise" mode="matchevery">
  <xsl:if>
    <attribute name="test">
      <text>not(</text>
      <for-each select="preceding-sibling::sel:when">
        <value-of select="@expr"/>
        <if test="position() &lt; last()">
          <text> or </text>
        </if>
      </for-each>
      <text>)</text>
    </attribute>
    <apply-templates/>
  </xsl:if>
</template>


<!-- ############################################################ -->
<!-- Variables -->

<template match="sel:variable">
      <!-- generating XSLT to make it behave like the variable value is changed would too hairy -->
      <!-- since we'd need an XPath parser to replace variable names in subsequent XPath exprs -->
  <choose>
    <when test="@name">
      <xsl:variable name="{@name}" select="{@value}" saxon:assignable="yes" xmlns:saxon="http://icl.com/saxon"/>
    </when>
    <when test="@ref">
      <saxon:assign xmlns:saxon="http://icl.com/saxon" name="{@ref}" select="{@value}" xsl:extension-element-prefixes="saxon"/>
    </when>
    <otherwise>
      <message>Error: sel:variable must either have a 'name' or a 'ref' attribute.</message>
    </otherwise>
  </choose>
</template>

<template match="sel:value">
  <xsl:value-of select="{@expr}"/>
</template>

<!-- ############################################################ -->
<!-- Variables -->


</transform>
