<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:tb="http://eastgate.com/Tinderbox/maparent"
  xmlns:rpc="http://maparent.webhop.net/xmlrpc"
  xmlns:dyn="http://exslt.org/dynamic"
  xmlns:str="http://exslt.org/strings"
  xsl:extension-element-prefixes="tb dyn str rpc"
  version="1.1">
  <xsl:param name="program-prototype">Programme</xsl:param>
  <xsl:param name="xpathAttrib">xpath_alias_attribute</xsl:param>
  <xsl:param name="xml-rpc-prototype">XML-RPC</xsl:param>
  <xsl:param name="xml-rpc-url">tb:property('xml_rpc_url')</xsl:param>
  <xsl:param name="xml-rpc-xsltFile">tb:property('xsltFile')</xsl:param>
  <xsl:param name="xml-rpc-separator">,</xsl:param>
  <xsl:param name="xml-rpc-method-name">tb:property('xml_rpc_method_name')</xsl:param>
  <xsl:param name="xml-rpc-signature">tb:property('xml_rpc_signature')</xsl:param>
  <xsl:param name="xml-rpc-params">tb:property('xml_rpc_params')</xsl:param>
  <xsl:key name="itemId" match="item" use="@ID"/>

  <xsl:template match="*">
    <xsl:copy select=".">
      <xsl:apply-templates select="@*"/>
      <xsl:apply-templates select="*|text()"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="@*|text()">
    <xsl:copy select="."/>
  </xsl:template>

  <xsl:template match="item[tb:property($xpathAttrib) != '']">
    <xsl:copy select=".">
    <xsl:apply-templates select="@*"/>
    <xsl:apply-templates select="attribute|text|style|adornement|text()"/>
    <xsl:variable name="xpath" select="tb:property($xpathAttrib)"/>
    <xsl:apply-templates select="dyn:evaluate($xpath)" mode="alias"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="item[tb:prototype() = $xml-rpc-prototype]">
    <xsl:variable name="url" select="dyn:evaluate($xml-rpc-url)"/>
    <xsl:variable name="xsltFile" select="dyn:evaluate($xml-rpc-xsltFile)"/>
    <xsl:variable name="method-name" select="dyn:evaluate($xml-rpc-method-name)"/>
    <xsl:variable name="signature" select="str:split(dyn:evaluate($xml-rpc-signature),$xml-rpc-separator)"/>
    <xsl:variable name="params" select="str:split(dyn:evaluate($xml-rpc-params),$xml-rpc-separator)"/>
    <xsl:variable name="returnValue" select="rpc:evalXmlRpc($url,$method-name,$signature,$params)"/>
    <xsl:copy select=".">
      <xsl:apply-templates select="@*"/>
      <xsl:apply-templates select="attribute|style|adornement|text|text()"/>
      <rpc:callStylesheet select="$returnValue" href="$xsltFile"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="item[tb:prototype() = $program-prototype]">
    <xsl:copy select=".">
    <xsl:apply-templates select="@*"/>
    <xsl:apply-templates select="attribute|text|style|adornement|text()"/>
    <xsl:apply-templates select="tb:evalPythonFunction(text/text())" mode="alias"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="*" mode="alias">
    <item>
      <xsl:attribute name="ID">
        <xsl:value-of select="tb:newId()"/>
      </xsl:attribute>
      <xsl:attribute name="Creator">
        <xsl:value-of select="@Creator"/>
      </xsl:attribute>
      <attribute name="Created" >
        <xsl:value-of select="tb:property('Created')"/>
      </attribute>
      <attribute name="Alias" >
        <xsl:value-of select="tb:maybeNegate(./@ID)"/>
      </attribute>
      <attribute name="Modified" >7 Feb 2004 17:20:34</attribute>
    </item>
  </xsl:template>

  <xsl:template match="links">

    <links>
      <xsl:apply-templates/>
      <tb:implementNewLinks/>
    </links>
  </xsl:template>

</xsl:stylesheet>
