<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:its="http://www.w3.org/2005/11/its"
                version="2.0">
   <xsl:output method="xml" indent="yes" encoding="utf-8"/>
   <xsl:strip-space elements="*"/>
   <xsl:template match="/">
      <xsl:apply-templates mode="Translate" select="@* | node()">
         <xsl:with-param name="existingValue">yes</xsl:with-param>
      </xsl:apply-templates>
   </xsl:template>
   <xsl:template name="output">
      <xsl:param name="value">notset</xsl:param>
      <xsl:param name="type">notset</xsl:param>
      <xsl:choose>
         <xsl:when test="self::element()">
            <xsl:copy>
               <xsl:attribute name="its-transval-elem">
                  <xsl:value-of select="$value"/>
               </xsl:attribute>
               <xsl:apply-templates select="@* | node()" mode="Translate">
                  <xsl:with-param name="existingValue">
                     <xsl:value-of select="$value"/>
                  </xsl:with-param>
               </xsl:apply-templates>
            </xsl:copy>
         </xsl:when>
         <xsl:when test="self::attribute()">
            <xsl:copy/>
            <xsl:variable name="attrval">
               <xsl:text>transval-attr-</xsl:text>
               <xsl:value-of select="concat('p--',substring-before(name(),':'),'--p-')"/>
               <xsl:value-of select="local-name()"/>
            </xsl:variable>
            <xsl:attribute name="{$attrval}">
               <xsl:value-of select="$value"/>
            </xsl:attribute>
         </xsl:when>
      </xsl:choose>
   </xsl:template>
   <xsl:template match="*[@its:translate]" priority="+1000" mode="Translate">
      <xsl:call-template name="output">
         <xsl:with-param name="value" select="@its:translate"/>
         <xsl:with-param name="type">local</xsl:with-param>
      </xsl:call-template>
   </xsl:template>
   <xsl:template match="*" priority="-1000" mode="Translate">
      <xsl:param name="existingValue">yes</xsl:param>
      <xsl:choose>
         <xsl:when test="$existingValue='yes'">
            <xsl:call-template name="output">
               <xsl:with-param name="value">yes</xsl:with-param>
               <xsl:with-param name="type">recurse-elem</xsl:with-param>
            </xsl:call-template>
         </xsl:when>
         <xsl:otherwise>
            <xsl:call-template name="output">
               <xsl:with-param name="value">no</xsl:with-param>
               <xsl:with-param name="type">recurse-elem</xsl:with-param>
            </xsl:call-template>
         </xsl:otherwise>
      </xsl:choose>
   </xsl:template>
   <xsl:template match="@*" priority="-1000" mode="Translate">
      <xsl:param name="existingValue">no</xsl:param>
      <xsl:choose>
         <xsl:when test="$existingValue='yes'">
            <xsl:call-template name="output">
               <xsl:with-param name="value">yes</xsl:with-param>
               <xsl:with-param name="type">recurse-attr</xsl:with-param>
            </xsl:call-template>
         </xsl:when>
         <xsl:otherwise>
            <xsl:call-template name="output">
               <xsl:with-param name="value">no</xsl:with-param>
               <xsl:with-param name="type">recurse-attr</xsl:with-param>
            </xsl:call-template>
         </xsl:otherwise>
      </xsl:choose>
   </xsl:template>
   <xsl:template xmlns:h="http://www.w3.org/1999/xhtml"
                 match="//h:pre[@translate='no'] | //pre[@translate='no']"
                 mode="Translate"
                 priority="1">
      <xsl:call-template name="output">
         <xsl:with-param name="value" select="'no'"/>
         <xsl:with-param name="type">global</xsl:with-param>
      </xsl:call-template>
   </xsl:template>
</xsl:stylesheet>