<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:html="http://www.w3.org/1999/xhtml"
  xmlns:dataview="http://www.w3.org/2003/g/data-view#"
  xmlns:uri="http://www.w3.org/2000/07/uri43/uri.xsl?template="
  exclude-result-prefixes="uri html">

  <!-- $Id: getTransforms.xsl,v 1.2 2004/06/04 20:16:01 connolly Exp $ -->
  <!-- @@open source license. Share and Enjoy. -->

  <xsl:import href="http://www.w3.org/2000/07/uri43/uri.xsl"/>
  <xsl:output method="text"/>

  <xsl:param name="xmlfile"/>

  <!--
  output is a sequence of lines ala
  C uri
  where C is
  R for the root namespace
  P for a profile
  T for a transformation
  -->

  <xsl:template match="/*">
    <xsl:call-template name="extractRootNS"/>
    <xsl:call-template name="extractTransformAttr"/>
  </xsl:template>


  <xsl:template match="/html:html">
    <xsl:call-template name="extractRootNS"/>
    <xsl:call-template name="extractTransformAttr"/>

    <xsl:call-template name="splitURIs">
      <xsl:with-param name="uris" select="html:head/@profile"/>
      <xsl:with-param name="pfx" select='"P "'/>
    </xsl:call-template>

    <xsl:call-template name="findTransforms">
      <xsl:with-param name="profile"
		      select="'http://www.w3.org/2003/11/rdf-in-xhtml'"/>
    </xsl:call-template>
    
    <!-- support older profile too -->
    <xsl:call-template name="findTransforms">
      <xsl:with-param name="profile"
		      select="'http://www.w3.org/2003/g/data-view'"/>
    </xsl:call-template>
    
  </xsl:template>


  <xsl:template name="extractRootNS">
    <xsl:variable name="rootNS" select="namespace-uri()" />
    <xsl:text>R </xsl:text>
    <xsl:value-of select='$rootNS'/>
    <xsl:text>&#10;</xsl:text>
  </xsl:template>


  <xsl:template name="extractTransformAttr">
    <xsl:call-template name="splitURIs">
      <xsl:with-param name="uris" select="@dataview:transformation"/>
      <xsl:with-param name="pfx" select='"T "'/>
    </xsl:call-template>
  </xsl:template>


  <xsl:template name="splitURIs">
    <xsl:param name="uris"/>
    <xsl:param name="pfx"/>

    <xsl:if test="normalize-space($uris)">
      <!-- uris is a list of white-space separated URIs -->
      <xsl:variable name="fullURI">
        <!-- Absolute URI of the XSLT -->
        <xsl:call-template name="uri:expand">
          <xsl:with-param name="base"
                          select="$xmlfile"/>
          <xsl:with-param name="there" select="substring-before(concat(normalize-space($uris),' '),' ')"/>
        </xsl:call-template>
      </xsl:variable>

      <xsl:value-of select="$pfx"/>
      <xsl:value-of select='$fullURI'/>
      <xsl:text>&#10;</xsl:text>

      <!-- recur... -->
      <xsl:call-template name="splitURIs">
        <xsl:with-param name="uris"
			select="substring-after(normalize-space($uris),' ')"/>
	<xsl:with-param name="pfx" select="$pfx"/>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>


  <xsl:template name="findTransforms">
    <xsl:param name="profile"/>
    <xsl:variable name="headProfile" select='html:head/@profile'/>

    <!-- this is a long way of saying:
         if $profile in split(html/head/@profile) ...
         -->
    <xsl:if
     test="contains($headProfile, $profile)
	   and ($headProfile = $profile
	        or contains($headProfile,
		            concat(' ',$profile,' '))
	        or starts-with($headProfile,
                               concat($profile,' '))
                or (contains($headProfile,
                             concat(' ',$profile))
	            and not(substring-after($headProfile,
		                            concat(' ',$profile)))))
          ">

      <!-- Using the XSLT servlet to include the content of the processing
	   of the XHTML document through the linked XSLTs -->

      <xsl:for-each select="/html:html//html:*[@href and
		    (@rel='xslt2rdf' or  @rel='transformation')]">
	<xsl:variable name="xslfile">
	  <!-- Absolute URI of the XSLT -->
	  <xsl:call-template name="uri:expand">
	    <xsl:with-param name="base"
			    select="$xmlfile"/>
	    <xsl:with-param name="there" select="@href"/>
	  </xsl:call-template>
	</xsl:variable>

	<xsl:text>T </xsl:text>
	<xsl:value-of select='$xslfile'/>
	<xsl:text>&#10;</xsl:text>

      </xsl:for-each>
    </xsl:if>
  </xsl:template>


<!-- don't pass text thru -->
<xsl:template match="text()|@*">
</xsl:template>
</xsl:stylesheet>

