osoft:feed-normalization"
  exclude-result-prefixes="sy atom03 msfeednorm"
  >

<!-- Shared templates -->
  <xsl:import href="res://msfeeds.dll/SharedNormalizer.xsl" />

<!-- Output XML UTF-8 -->
  <xsl:output method="xml" encoding="utf-8" />
  
<!-- Entry point -->
  <xsl:template match="/atom03:feed">
    <rss version="2.0">
      <channel>

        <xsl:call-template name="_OutputXmlBase"><xsl:with-param name="value" select="@xml:base" /></xsl:call-template>
        <xsl:call-template name="_OutputXmlLang"><xsl:with-param name="value" select="@xml:lang" /></xsl:call-template>
        <xsl:call-template name="_OutputLanguage"><xsl:with-param name="value" select="@xml:lang" /></xsl:call-template>

        <!-- Figure out the base URL value; xml:base overrides the download URL -->
        <xsl:variable name="baseChannelUrl">
          <xsl:call-template name="_GetFeedBaseUrl">
            <xsl:with-param name="downloadUrl" select="$downloadUrl" />
            <xsl:with-param name="rootNode" select="." />
          </xsl:call-template>
        </xsl:variable>

        <!-- Try to find the main link - one that has an href pointing to html and with type alternate -->
        <xsl:variable name="mainLink" select="atom03:link[(@rel = 'alternate' or @rel = '' or not(@rel)) and (@type = 'text/html' or @type = '' or not(@type))]" />

        <!-- Output link -->
        <xsl:if test="$mainLink">
          <!-- Combine base URL with the main link -->
          <xsl:variable name="channelLinkUrl">
            <xsl:call-template name="_CompleteUrlTwoBase">
              <xsl:with-param name="baseUrl1" select="$baseChannelUrl" />
              <xsl:with-param name="baseUrl2" select="$mainLink/@xml:base" />
              <xsl:with-param name="Url" select="$mainLink/@href" />
            </xsl:call-template>
          </xsl:variable>
          <xsl:call-template name="_OutputLink"><xsl:with-param name="value" select="$channelLinkUrl" /></xsl:call-template>
        </xsl:if>

        <!-- Build a list of referenced nodes from cf:sort and cf:group -->
        <xsl:variable name="cfReferences">
          <xsl:call-template name="_BuildCfReferenceList"><xsl:with-param name="value" select="." /></xsl:call-template>
        </xsl:variable>

        <!-- Process channel elements -->
        <xsl:apply-templates select="*" mode="channel">
          <xsl:with-param name="baseChannelUrl" select="$baseChannelUrl" />
        </xsl:apply-templates>

        <!-- Process items -->
        <xsl:apply-templates select="atom03:entry">
          <xsl:with-param name="baseChannelUrl" select="$baseChannelUrl" />
          <xsl:with-param name="references" select="$cfReferences" />
        </xsl:apply-templates>

      </channel>
    </rss>
  </xsl:template>

<!-- Channel processing -->
  <xsl:template match="*" mode="channel">
    <xsl:param name="baseChannelUrl" />
    <xsl:variable name="isFirst">
      <xsl:call-template name="_IsFirstChildOfItsKind"><xsl:with-param name="value" select="." /></xsl:call-template>
    </xsl:variable>
    <xsl:choose>
      <xsl:when test="namespace-uri(.) = $_NSatom03 and local-name(.) = 'title' and $isFirst = 'true'">
        <xsl:call-template name="_OutputTitle">
          <xsl:with-param name="value" select="." />
          <xsl:with-param name="type" select="@type" />
        </xsl:call-template>
      </xsl:when>
      <xsl:when test="namespace-uri(.) = $_NSatom03 and local-name(.) = 'author'">
        <!-- First one gets converted to managing Editor -->
        <xsl:if test="$isFirst = 'true'">
          <xsl:variable name="manEditor">
            <xsl:call-template name="_ConvertAtomAuthorToAuthor">
              <xsl:with-param name="name" select="atom03:name" />
              <xsl:with-param name="email" select="atom03:email" />
            </xsl:call-template>
          </xsl:variable>
          <xsl:call-template name="_OutputManagingEditor"><xsl:with-param name="value" select="$manEditor" /></xsl:call-template>
        </xsl:if>
        <!-- Map to Atom 1.0 -->
        <xsl:call-template name="_OutputAtomAuthor">
          <xsl:with-param name="name" select="atom03:name" />
          <xsl:with-param name="email" select="atom03:email" />
          <xsl:with-param name="uri" select="atom03:url" />
        </xsl:call-template>
      </xsl:when>
      <xsl:when test="namespace-uri(.) = $_NSatom03 and local-name(.) = 'contributor'">
        <!-- Map to Atom 1.0 -->
        <xsl:call-template name="_OutputAtomContributor">
          <xsl:with-param name="name" select="atom03:name" />
          <xsl:with-param name="email" select="atom03:email" />
          <xsl:with-param name="uri" select="atom03:url" />
        </xsl:call-template>
      </xsl:when>
      <xsl:when test="namespace-uri(.) = $_NSatom03 and local-name(.) = 'tagline' and $isFirst = 'true'">
        <xsl:call-template name="_OutputTextDescription">
          <xsl:with-param name="value" select="." />
          <xsl:with-param name="type" select="@type" />
        </xsl:call-template>
      </xsl:when>
      <xsl:when test="namespace-uri(.) = $_NSatom03 and local-name(.) = 'id' and $isFirst = 'true'">
        <xsl:call-template name="_OutputGuid">
          <xsl:with-param name="value" select="." />
          <xsl:with-param name="isPermaLink" select="'false'" />
          <xsl:with-param name="channelGuid" select="'true'" />
        </xsl:call-template>
      </xsl:when>
      <xsl:when test="namespace-uri(.) = $_NSatom03 and local-name(.) = 'generator' and $isFirst = 'true'">
        <xsl:call-template name="_OutputGenerator">
          <xsl:with-param name="value" select="." />
          <xsl:with-param name="uri" select="@url" />
          <xsl:with-param name="version" select="@version" />
        </xsl:call-template>
      </xsl:when>
      <xsl:when test="namespace-uri(.) = $_NSatom03 and local-name(.) = 'modified' and $isFirst = 'true'">
        <xsl:call-template name="_OutputLastBuildDate"><xsl:with-param name="value" select="." /></xsl:call-template>
        <xsl:variable name="updatedExists">
          <xsl:choose>
            <xsl:when test="../atom:updated">true</xsl:when>
            <xsl:otherwise>false</xsl:otherwise>
          </xsl:choose>
        </xsl:variable>
        <xsl:if test="$updatedExists = 'false'">
          <xsl:call-template name="_OutputAtomUpdated"><xsl:with-param name="value" select="." /></xsl:call-template>
        </xsl:if>
      </xsl:when>
      <xsl:when test="namespace-uri(.) = $_NSatom1 and local-name(.) = 'published'">

      </xsl:when>
      <xsl:when test="namespace-uri(.) = $_NSatom1 and local-name(.) = 'updated'">
        <xsl:if test="$isFirst = 'true'">
          <xsl:call-template name="_OutputAtomUpdated"><xsl:with-param name="value" select="." /></xsl:call-template>
          <xsl:if test="not(../atom03:modified)">
            <xsl:call-template name="_OutputLastBuildDate"><xsl:with-param name="value" select="." /></xsl:call-template>
          </xsl:if>
        </xsl:if>
      </xsl:when>
      <xsl:when test="namespace-uri(.) = $_NSatom03 and local-name(.) = 'copyright' and $isFirst = 'true'">
        <xsl:call-template name="_OutputCopyright">
          <xsl:with-param name="value" select="." />
          <xsl:with-param name="type" select="@type" />
        </xsl:call-template>
      </xsl:when>
      <xsl:when test="namespace-uri(.) = $_NSdc and local-name(.) = 'subject'">
        <xsl:call-template name="_OutputCategory"><xsl:with-param name="value" select="." /></xsl:call-template>
      </xsl:when>
      <!-- First pair of updateFrequency and updatePeriod gets converted to ttl; all sy:* nodes are preserved as extensions -->
      <xsl:when test="namespace-uri(.) = $_NSsy and local-name(.) = 'updatePeriod' and $isFirst = 'true'">
        <xsl:if test="../sy:updateFrequency">
          <xsl:call-template name="_ConvertUpdatesToTtl">
            <xsl:with-param name="updatePeriod" select="string(.)" />
            <xsl:with-param name="updateFrequency" select="string(../sy:updateFrequency)" />
          </xsl:call-template>
          <xsl:call-template name="_OutputExtension"><xsl:with-param name="value" select="." /></xsl:call-template>
        </xsl:if>
      </xsl:when>
      <xsl:when test="namespace-uri(.) = $_NScf and local-name(.) = 'treatAs'">
        <xsl:if test="$isFirst = 'true'">
          <xsl:call-template name="_OutputCfTreatAs"><xsl:with-param name="value" select="." /></xsl:call-template>
        </xsl:if>
      </xsl:when>
      <xsl:when test="namespace-uri(.) = $_NScf and local-name(.) = 'listinfo'">
        <xsl:if test="$isFirst = 'true'">
          <xsl:call-template name="_OutputCfListinfo"><xsl:with-param name="value" select="." /></xsl:call-template>
        </xsl:if>
      </xsl:when>
      <xsl:when test="namespace-uri(.) = $_NSatom03 and local-name(.) = 'entry'">
        <!-- Do nothing here for items -->
      </xsl:when>
      <!-- Copy extension nodes -->
      <xsl:when test="namespace-uri(.) != $_NSatom03 and namespace-uri(.) != ''">
        <xsl:call-template name="_OutputExtension"><xsl:with-param name="value" select="." /></xsl:call-template>
      </xsl:when>
    </xsl:choose>
  </xsl:template>

<!-- Item processing -->
  <xsl:template match="atom03:entry">
    <xsl:param name="baseChannelUrl" />
    <xsl:param name="references" />
    <item>

      <xsl:call-template name="_OutputXmlBase"><xsl:with-param name="value" select="@xml:base" /></xsl:call-template>

      <xsl:call-template name="_OutputXmlLang"><xsl:with-param name="value" select="@xml:lang" /></xsl:call-template>

      <!-- Item's base URL stacks onto channel's base URL -->
      <xsl:variable name="baseChannelItemUrl">
        <xsl:call-template name="_CombineUrls">
          <xsl:with-param name="baseUrl" select="$baseChannelUrl" />
          <xsl:with-param name="relUrl" select="@xml:base" />
        </xsl:call-template>
      </xsl:variable>

      <!-- Try to find the main link - one that has an href pointing to html and with type alternate -->
      <xsl:variable name="mainLink" select="atom03:link[(@rel = 'alternate' or @rel = '' or not(@rel)) and (@type = 'text/html' or @type = '' or not(@type))]" />

      <!-- Output link -->
      <xsl:if test="$mainLink">
        <!-- Combine base URL with the main link -->
        <xsl:variable name="itemLinkUrl">
          <xsl:call-template name="_CompleteUrlTwoBase">
            <xsl:with-param name="baseUrl1" select="$baseChannelItemUrl" />
            <xsl:with-param name="baseUrl2" select="$mainLink/@xml:base" />
            <xsl:with-param name="Url" select="$mainLink/@href" />
          </xsl:call-template>
        </xsl:variable>
        <xsl:call-template name="_OutputLink"><xsl:with-param name="value" select="$itemLinkUrl" /></xsl:call-template>
      </xsl:if>

      <xsl:apply-templates select="*" mode="item">
        <xsl:with-param name="baseItemUrl" select="$baseChannelItemUrl" />
        <xsl:with-param name="references" select="$references" />
      </xsl:apply-templates>

    </item>
  </xsl:template>

  <xsl:template match="*" mode="item">
    <xsl:param name="baseItemUrl" />
    <xsl:param name="references" />
    <xsl:variable name="isFirst">
      <xsl:call-template name="_IsFirstChildOfItsKind"><xsl:with-param name="value" select="." /></xsl:call-template>
    </xsl:variable>
    <xsl:choose>
      <xsl:when test="namespace-uri(.) = $_NSatom03 and local-name(.) = 'title' and $isFirst = 'true'">
        <xsl:call-template name="_OutputTitle">
          <xsl:with-param name="value" select="." />
          <xsl:with-param name="type" select="@type" />
        </xsl:call-template>
      </xsl:when>
      <xsl:when test="namespace-uri(.) = $_NSatom03 and local-name(.) = 'link' and @rel = 'enclosure' and (@href or @length or @type)">
        <xsl:variable name="enclosureUrl">
          <xsl:call-template name="_CompleteUrlTwoBase">
            <xsl:with-param name="baseUrl1" select="$baseItemUrl" />
            <xsl:with-param name="baseUrl2" select="@xml:base" />
            <xsl:with-param name="Url" select="@href" />
          </xsl:call-template>
        </xsl:variable>
        <xsl:call-template name="_OutputEnclosure">
          <xsl:with-param name="url" select="$enclosureUrl" />
          <xsl:with-param name="length" select="@length" />
          <xsl:with-param name="type" select="@type" />
        </xsl:call-template>
      </xsl:when>
      <xsl:when test="namespace-uri(.) = $_NSatom03 and local-name(.) = 'author'">
        <xsl:variable name="author">
          <xsl:call-template name="_ConvertAtomAuthorToAuthor">
            <xsl:with-param name="name" select="atom03:name" />
            <xsl:with-param name="email" select="atom03:email" />
          </xsl:call-template>
        </xsl:variable>
        <xsl:call-template name="_OutputAuthor"><xsl:with-param name="value" select="$author" /></xsl:call-template>
        <!-- Map to Atom 1.0 -->
        <xsl:call-template name="_OutputAtomAuthor">
          <xsl:with-param name="name" select="atom03:name" />
          <xsl:with-param name="email" select="atom03:email" />
          <xsl:with-param name="uri" select="atom03:url" />
        </xsl:call-template>
      </xsl:when>
      <xsl:when test="namespace-uri(.) = $_NSatom03 and local-name(.) = 'contributor'">
        <!-- Map to Atom 1.0 -->
        <xsl:call-template name="_OutputAtomContributor">
          <xsl:with-param name="name" select="atom03:name" />
          <xsl:with-param name="email" select="atom03:email" />
          <xsl:with-param name="uri" select="atom03:url" />
        </xsl:call-template>
      </xsl:when>
      <xsl:when test="namespace-uri(.) = $_NSatom03 and local-name(.) = 'id' and $isFirst = 'true'">
        <xsl:call-template name="_OutputGuid">
          <xsl:with-param name="value" select="." />
          <xsl:with-param name="isPermaLink" select="'false'" />
        </xsl:call-template>
      </xsl:when>
      <xsl:when test="namespace-uri(.) = $_NSatom1 and local-name(.) = 'published'">
        <xsl:if test="$isFirst = 'true'">
          <xsl:call-template name="_OutputAtomPublished"><xsl:with-param name="value" select="." /></xsl:call-template>
          <xsl:if test="not(../atom03:issued)">
            <xsl:call-template name="_OutputPubDate"><xsl:with-param name="value" select="." /></xsl:call-template>
          </xsl:if>
        </xsl:if>
      </xsl:when>
      <xsl:when test="namespace-uri(.) = $_NSatom1 and local-name(.) = 'updated'">
        <xsl:if test="$isFirst = 'true'">
          <xsl:call-template name="_OutputAtomUpdated"><xsl:with-param name="value" select="." /></xsl:call-template>
        </xsl:if>
      </xsl:when>
      <xsl:when test="namespace-uri(.) = $_NSatom03 and local-name(.) = 'modified' and $isFirst = 'true'">
        <xsl:if test="not(../atom:updated)">
          <xsl:call-template name="_OutputAtomUpdated"><xsl:with-param name="value" select="." /></xsl:call-template>
        </xsl:if>
      </xsl:when>
      <xsl:when test="namespace-uri(.) = $_NSatom03 and local-name(.) = 'issued' and $isFirst = 'true'">
        <xsl:call-template name="_OutputPubDate"><xsl:with-param name="value" select="." /></xsl:call-template>
        <xsl:if test="not(../atom:updated) and not(../atom03:modified)">
          <xsl:call-template name="_OutputAtomUpdated"><xsl:with-param name="value" select="." /></xsl:call-template>
        </xsl:if>
        <xsl:if test="not(../atom:published)">
          <xsl:call-template name="_OutputAtomPublished"><xsl:with-param name="value" select="." /></xsl:call-template>
        </xsl:if>
      </xsl:when>
      <xsl:when test="namespace-uri(.) = $_NSdc and local-name(.) = 'subject'">
        <xsl:call-template name="_OutputCategory"><xsl:with-param name="value" select="." /></xsl:call-template>
      </xsl:when>
      <!-- Handle content: printable goes into description; non-printable is preserved -->
      <xsl:when test="namespace-uri(.) = $_NSatom03 and local-name(.) = 'content' and $isFirst = 'true'">
          <xsl:if test="@type = '' or not(@type) or @type = 'text/plain' or @type = 'text/html' or @type = 'application/xhtml+xml'">
            <xsl:variable name="baseContentUrl">
              <xsl:call-template name="_CombineUrls">
                <xsl:with-param name="baseUrl" select="$baseItemUrl" />
                <xsl:with-param name="relUrl" select="@xml:base" />
              </xsl:call-template>
            </xsl:variable>
            <xsl:call-template name="_OutputDescription">
              <xsl:with-param name="value" select="." />
              <xsl:with-param name="type" select="@type" />
              <xsl:with-param name="baseUrl" select="$baseContentUrl" />
            </xsl:call-template>
          </xsl:if>
      </xsl:when>
      <!-- Handle summary: printable goes into description or atom:summary; non-printable is discarded -->
      <xsl:when test="namespace-uri(.) = $_NSatom03 and local-name(.) = 'summary' and $isFirst = 'true'">
        <xsl:if test="@type = '' or not(@type) or @type = 'text/plain' or @type = 'text/html' or @type = 'application/xhtml+xml'">
          <xsl:variable name="baseContentUrl">
            <xsl:call-template name="_CombineUrls">
              <xsl:with-param name="baseUrl" select="$baseItemUrl" />
              <xsl:with-param name="relUrl" select="@xml:base" />
            </xsl:call-template>
          </xsl:variable>
          <xsl:choose>
            <xsl:when test="not(../atom03:content[@type = '' or not(@type) or @type = 'text/plain' or @type = 'text/html' or @type = 'application/xhtml+xml'])">
              <xsl:call-template name="_OutputDescription">
                <xsl:with-param name="value" select="." />
                <xsl:with-param name="type" select="@type" />
                <xsl:with-param name="baseUrl" select="$baseContentUrl" />
              </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
              <!-- Map to Atom 1.0 -->
              <xsl:call-template name="_OutputAtomSummary">
                <xsl:with-param name="value" select="." />
                <xsl:with-param name="type" select="@type" />
                <xsl:with-param name="xmlBase" select="@xml:base" />
                <xsl:with-param name="baseUrl" select="$baseContentUrl" />
              </xsl:call-template>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:if>
      </xsl:when>
      <xsl:when test="namespace-uri(.) = $_NSslash and local-name(.) = 'comments'">
        <xsl:if test="$isFirst = 'true'">
          <xsl:call-template name="_OutputSlashComments"><xsl:with-param name="value" select="." /></xsl:call-template>
        </xsl:if>
      </xsl:when>
      <xsl:when test="namespace-uri(.) = $_NSwfw and (local-name(.) = 'commentRss' or local-name(.) = 'commentRSS')">
        <xsl:if test="$isFirst = 'true'">
          <xsl:variable name="commentRssUrl">
            <xsl:call-template name="_CompleteUrlTwoBase">
              <xsl:with-param name="baseUrl1" select="$baseItemUrl" />
              <xsl:with-param name="baseUrl2" select="@xml:base" />
              <xsl:with-param name="Url" select="." />
            </xsl:call-template>
          </xsl:variable>
          <xsl:call-template name="_OutputWfwCommentRSS"><xsl:with-param name="value" select="$commentRssUrl" /></xsl:call-template>
        </xsl:if>
      </xsl:when>
      <xsl:when test="namespace-uri(.) = $_NSatom1 and local-name(.) = 'link'">
        <!-- Special handling for item level atom:link with rel=entry-content: complete relative URL -->
        <xsl:variable name="atomLinkUrl">
          <xsl:choose>
            <xsl:when test="@rel = 'entry-content'">
              <xsl:call-template name="_CombineUrls">
                <xsl:with-param name="baseUrl" select="$baseItemUrl" />
                <xsl:with-param name="relUrl" select="@href" />
              </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="@href" />
            </xsl:otherwise>
          </xsl:choose>
        </xsl:variable>
        <xsl:call-template name="_OutputAtomLink">
          <xsl:with-param name="href" select="$atomLinkUrl" />
          <xsl:with-param name="rel" select="@rel" />
          <xsl:with-param name="type" select="@type" />
          <xsl:with-param name="title" select="@title" />
          <xsl:with-param name="hreflang" select="@hreflang" />
          <xsl:with-param name="length" select="@length" />
          <xsl:with-param name="xmlBase" select="@xml:base" />
        </xsl:call-template>
      </xsl:when>
      <!-- Copy extension nodes -->
      <xsl:when test="namespace-uri(.) != $_NSatom03 and namespace-uri(.) != ''">
        <xsl:call-template name="_OutputExtension">
          <xsl:with-param name="value" select="." />
          <xsl:with-param name="references" select="$references" />
        </xsl:call-template>
      </xsl:when>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>