<?xml version="1.0"?>
<!-- 

    Copyright (C) 2006 W3C (R) (MIT ERCIM Keio), All Rights Reserved.
    W3C liability, trademark and document use rules apply.

    http://www.w3.org/Consortium/Legal/ipr-notice
    http://www.w3.org/Consortium/Legal/copyright-documents

    $Header: /w3ccvs/WWW/2002/ws/databinding/edcopy/patterns/patterns2detector.xsl,v 1.22 2008/03/14 21:16:12 pdowney Exp $

    build patternsdetector.xsl

-->
<xsl:stylesheet 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:axsl="http://www.w3.org/1999/XSL/TransformAlias" 
    xmlns:pat="http://www.w3.org/2002/ws/databinding/patterns/6/09/"
    xmlns:ex="http://www.w3.org/2002/ws/databinding/examples/6/09/"
    version="1.0">

   <xsl:namespace-alias stylesheet-prefix="axsl" result-prefix="xsl"/>
   <xsl:output method="xml" indent="yes"/>

   <xsl:param name="patterns-uri">http://www.w3.org/2002/ws/databinding/patterns/6/09/</xsl:param>
   <xsl:param name="examples-uri">http://www.w3.org/2002/ws/databinding/examples/6/09/</xsl:param>
   <xsl:param name="version">$Revision: 1.22 $</xsl:param>

   <xsl:template match="/">
<xsl:comment>

    Copyright (C) 2006 W3C (R) (MIT ERCIM Keio), All Rights Reserved.
    W3C liability, trademark and document use rules apply.

    http://www.w3.org/Consortium/Legal/ipr-notice
    http://www.w3.org/Consortium/Legal/copyright-documents

    <xsl:value-of select="ex:version"/>

</xsl:comment>

	<!-- TBD - brand generated processor with patterns.xml version -->

    <axsl:stylesheet 
	xmlns:xs="http://www.w3.org/2001/XMLSchema"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
	xmlns:wsdl11="http://schemas.xmlsoap.org/wsdl/"
	xmlns:soap11enc="http://schemas.xmlsoap.org/soap/encoding/"
	version="2.0">

<!-- TBD: declare namesapces -->

   <xsl:comment>XSLT Version 2.0 to indicate our use of XPath 2.0 expression</xsl:comment>

   <axsl:param name="report-unknowns">1</axsl:param>
   <axsl:param name="document-name">input</axsl:param>
   <axsl:param name="document-uri"><xsl:value-of select="$examples-uri"/><axsl:value-of select="$document-name"/></axsl:param>

   <axsl:output method="xml" indent="yes"/>

   <axsl:template match="/">
	<axsl:variable name="detected">
	    <axsl:apply-templates select=".//xs:schema" mode="detect"/>
	</axsl:variable>
	<axsl:variable name="status">
	   <axsl:choose>
		<axsl:when test="$detected/pattern[@status='unknown']">unknown</axsl:when>
		<axsl:when test="$detected/pattern[@status='pending']">pending</axsl:when>
		<axsl:when test="$detected/pattern[@status='advanced']">advanced</axsl:when>
		<axsl:when test="$detected/pattern[@status='basic']">basic</axsl:when>
		<axsl:otherwise>empty</axsl:otherwise>
	   </axsl:choose>
	</axsl:variable>
	<axsl:processing-instruction name="xml-stylesheet"> 
	    rel="style sheet" href="http://www.w3.org/2002/ws/databinding/detector/results.xsl" type="application/xml"
	</axsl:processing-instruction>
      <detected> 
	 <axsl:attribute name="version">
	     <xsl:value-of select="$version"/>
	 </axsl:attribute>
         <axsl:attribute name="status">
            <axsl:value-of select="$status"/>
         </axsl:attribute>
         <axsl:for-each select="$detected/pattern">
	     <axsl:if test=".[not(preceding::*/@xpath = @xpath)]">
		 <axsl:copy-of select="." />
	     </axsl:if>
	 </axsl:for-each>
      </detected>
   </axsl:template>

   <axsl:template match="*|@*" mode="detect">

   <xsl:for-each select="//pat:pattern">

       <axsl:if>
	    <xsl:attribute name="test">count(<xsl:value-of select="pat:xpath"/>) gt 0</xsl:attribute>
	    <pattern name="{@xml:id}" 
		n="{position()}"
		status="{@status}" 
		pattern="{$patterns-uri}{@xml:id}"
		xpath="{pat:xpath}">
	     <axsl:for-each>
		<xsl:attribute name="select"><xsl:value-of select="pat:xpath"/></xsl:attribute>
		<node><axsl:attribute name="xpath"><axsl:apply-templates select="." mode="get-full-path"/></axsl:attribute></node>
	     </axsl:for-each>
	    </pattern>
       </axsl:if>

   </xsl:for-each>

   <xsl:comment>detect unknown nodes</xsl:comment>

   <axsl:if>
      <xsl:attribute name="test">xs:boolean($report-unknowns)</xsl:attribute>

      <axsl:variable name="nodes">
	<xsl:attribute name="select">
	(.//* | .//@*)   except (       
	   <xsl:for-each select="//pat:pattern"><xsl:value-of select="pat:xpath"/>|</xsl:for-each>
	   xs:schema )
	</xsl:attribute>
      </axsl:variable>

      <axsl:if test="count($nodes)">
         <axsl:for-each select="$nodes">

	    <axsl:variable name="n" select="generate-id()"/>
	    <axsl:variable name="name"><axsl:value-of select="concat('Unknown-',$n)"/></axsl:variable>
	    <axsl:variable name="xpath"><axsl:apply-templates select="." mode="get-full-path"/></axsl:variable>

	    <pattern status="unknown">
		<axsl:attribute name="n"><axsl:value-of select="$n"/></axsl:attribute>
		<axsl:attribute name="name"><axsl:value-of select="$name"/></axsl:attribute>
		<axsl:attribute name="pattern"><axsl:value-of select="concat('{$patterns-uri}',$name)"/></axsl:attribute>
		<axsl:attribute name="xpath"><axsl:apply-templates select="." mode="get-full-path"/></axsl:attribute>
		<node><axsl:attribute name="xpath"><axsl:value-of select="$xpath"/></axsl:attribute></node>
	    </pattern>

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

   <xsl:comment>print full path of current node</xsl:comment>

   <axsl:template match="*|@*" mode="get-full-path">
      <axsl:apply-templates select="parent::*" mode="get-full-path"/>
      <axsl:text>/</axsl:text>
      <axsl:choose>
	<axsl:when test="count(. | ../@*) = count(../@*)">@<axsl:value-of select="name()"/></axsl:when>
	<axsl:otherwise>
	  <axsl:value-of select="name()"/>
	  <axsl:if test="count(../*[name()=name(current())]) gt 1">
	  <axsl:text>[</axsl:text>
	      <axsl:value-of select="1+count(preceding-sibling::*[name()=name(current())])"/>
	  <axsl:text>]</axsl:text>
	  </axsl:if>
	</axsl:otherwise>
      </axsl:choose>
   </axsl:template>

   <axsl:template match="text()" />

    </axsl:stylesheet>
   </xsl:template>

</xsl:stylesheet>
