<?xml version="1.0"?>

<xsl:stylesheet
    version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:f="http://fxsl.sf.net/"
    exclude-result-prefixes="f xs">

  <xsl:import href="../f/func-XpathConstructors.xsl"/>
  <xsl:import href="../f/func-apply.xsl"/>

  <xsl:function name="f:copy-with-type" as="node()">
    <xsl:param name="arg" as="item()*"/>
    <copy xsl:use-when="system-property('xsl:is-schema-aware') eq 'yes'"
          xsl:validation="preserve">
      <xsl:sequence select="for $a in $arg return f:copy-with-type-1($a)"/>
    </copy>
    <copy xsl:use-when="system-property('xsl:is-schema-aware') ne 'yes'">
      <xsl:sequence select="for $a in $arg return f:copy-with-type-1($a)"/>
    </copy>
  </xsl:function>

  <xsl:function name="f:get-typed" as="item()*">
    <xsl:param name="arg" as="element(copy)"/>
    <xsl:apply-templates select="$arg/*" mode="f:get-typed"/>
  </xsl:function>

  <xsl:template match="node" mode="f:get-typed" as="node()">
    <xsl:sequence select="@*|node()"/>
  </xsl:template>

  <xsl:template match="atomic" mode="f:get-typed" as="item()"
                use-when="system-property('xsl:is-schema-aware') eq 'yes'">
    <xsl:sequence select="data(.)"/>
  </xsl:template>

  <xsl:template match="f:*" mode="f:get-typed" as="item()"
                use-when="system-property('xsl:is-schema-aware') ne 'yes'">
    <xsl:sequence select="f:apply(., data(.))"/>
  </xsl:template>

  <xsl:function name="f:copy-with-type-1" as="node()"
                use-when="system-property('xsl:is-schema-aware') eq 'yes'">
    <xsl:param name="arg" as="item()"/>
    <xsl:choose>
      <xsl:when test="$arg instance of node()">
        <node xsl:validation="preserve">
          <xsl:copy-of select="$arg" validation="preserve"/>
        </node>
      </xsl:when>
      <xsl:otherwise>
        <!--
            The following inclusion results in an xsl:choose
            instruction whose the when instructions are of the form:

                <xsl:when test="$arg instance of xs:a-type">
                   <atomic xsl:type="xs:a-type">
                      <xsl:copy-of select="$arg" validation="preserve"/>
                   </atomic>
                </xsl:when>

            That code is generated.  See ../data/func-copy-make-whens.xsl,
            ../data/func-copy-whens.xml, and ../data/xs-simple-types.xml
        -->
        <xi:include href="../data/func-copy-whens.xml#sa"
                    xmlns:xi="http://www.w3.org/2001/XInclude"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:function>

  <xsl:function name="f:copy-with-type-1" as="node()"
                use-when="system-property('xsl:is-schema-aware') ne 'yes'">
    <xsl:param name="arg" as="item()"/>
    <xsl:choose>
      <xsl:when test="$arg instance of node()">
        <node>
          <xsl:copy-of select="$arg"/>
        </node>
      </xsl:when>
      <xsl:otherwise>
        <!--
            The following inclusion results in an xsl:choose
            instruction whose the when instructions are of the form:

                <xsl:when test="$arg instance of xs:a-basic-type">
                   <f:a-basic-type>
                      <xsl:copy-of select="$arg"/>
                   </f:a-basic-type>
                </xsl:when>

            That code is generated.  See ../data/func-copy-make-whens.xsl,
            ../data/func-copy-whens.xml, and ../data/xs-simple-types.xml
        -->
        <xi:include href="../data/func-copy-whens.xml#basic"
                    xmlns:xi="http://www.w3.org/2001/XInclude"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:function>

</xsl:stylesheet>
