<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <!-- xml:base / xml:lang resolver -->
  <!-- $Id: baselangutils.xsl,v 1.4 2006/05/01 19:44:09 Dave Exp $ -->


  <xsl:include href="urires.xsl" />


  <xsl:template name="getXmlLang">
    <xsl:param name="element" />
    <xsl:param name="default" />

    <xsl:variable name="langNodes" select="$element/ancestor-or-self::node()[@xml:lang]" />
    <xsl:variable name="langNodeCount" select="count($langNodes)" />
    <xsl:choose>        
      <xsl:when test="$langNodeCount &gt; 0">
        <xsl:value-of select="($langNodes[last()]/@xml:lang)" />
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$default" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>


  <xsl:template name="getXmlBase">
    <xsl:param name="element" />
    <xsl:param name="default" select="''" />
    <xsl:param name="strict" select="'true'" />
    <xsl:param name="returnRef" select="'false'" />

    <xsl:variable name="baseNodes" select="$element/ancestor-or-self::node()[@xml:base]" />
    <xsl:call-template name="blu_resolvestep">
      <xsl:with-param name="i" select="1" />
      <xsl:with-param name="count" select="count($baseNodes)" />
      <xsl:with-param name="base" select="$default" />
      <xsl:with-param name="baseNodes" select="$baseNodes" />
      <xsl:with-param name="strict" select="$strict" />
      <xsl:with-param name="returnRef" select="$returnRef" />
    </xsl:call-template>

  </xsl:template>

  <xsl:template name="blu_resolvestep">
    <xsl:param name="i" />
    <xsl:param name="count" />
    <xsl:param name="base" />
    <xsl:param name="baseNodes" />
    <xsl:param name="strict" />
    <xsl:param name="returnRef" />

    <xsl:variable name="ret">
      <xsl:call-template name="resolveRef">
        <xsl:with-param name="base" select="$base" />
        <xsl:with-param name="ref" select="$baseNodes[$i]/@xml:base" />
        <xsl:with-param name="strict" select="$strict" />
        <xsl:with-param name="returnRef" select="$returnRef" />
      </xsl:call-template>
    </xsl:variable>

    <xsl:choose>
      <xsl:when test="$i = $count">
        <xsl:value-of select="$ret" />
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="blu_resolvestep">
          <xsl:with-param name="i" select="$i + 1" />
          <xsl:with-param name="count" select="$count" />
          <xsl:with-param name="base" select="$ret" />
          <xsl:with-param name="baseNodes" select="$baseNodes" />
          <xsl:with-param name="strict" select="$strict" />
          <xsl:with-param name="returnRef" select="$returnRef" />
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>


  <xsl:template name="blu_splitPath">
    <xsl:param name="h" select="''" />
    <xsl:param name="path" />
    <xsl:param name="strict" select="'true'" />

    <xsl:variable name="p1" select="substring-before($path, ' ')" />
    <xsl:variable name="tail" select="substring-after($path, ' ')" />

    <xsl:choose>
      <xsl:when test="$p1">
        <xsl:variable name="res">
          <xsl:call-template name="blu_splitPath">
            <xsl:with-param name="h" select="$p1" />
            <xsl:with-param name="path" select="$tail" />
          </xsl:call-template>
        </xsl:variable>

        <xsl:call-template name="resolveRef">
          <xsl:with-param name="base" select="$h" />
          <xsl:with-param name="ref" select="$res" />
          <xsl:with-param name="strict" select="$strict" />
        </xsl:call-template>

      </xsl:when>
      <xsl:otherwise>

        <xsl:if test="$path">
          <xsl:call-template name="resolveRef">
            <xsl:with-param name="base" select="$h" />
            <xsl:with-param name="ref" select="$path" />
            <xsl:with-param name="strict" select="$strict" />
          </xsl:call-template>
        </xsl:if>

      </xsl:otherwise>
    </xsl:choose>

  </xsl:template>


</xsl:stylesheet>

