<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns="http://www.w3.org/2001/DOM-Test-Suite/Level-1"
                exclude-result-prefixes="#default">


<xsl:output method="xml" indent="yes" doctype-system="dom1.dtd"/>
<xsl:variable name="specURI">http://www.w3.org/TR/1998/REC-DOM-Level-1-19981001/level-one-core#</xsl:variable>

<xsl:variable name="interfaces" select="document('dom1-interfaces.xml')"/>

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

Copyright (c) 2001 World Wide Web Consortium, 
(Massachusetts Institute of Technology, Institut National de
Recherche en Informatique et en Automatique, Keio University).  All 
Rights Reserved.  This program is distributed under the W3C's Software
Intellectual Property License.  This program is distributed in the 
hope that it will be useful, but WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
PURPOSE.  

See W3C License http://www.w3.org/Consortium/Legal/ for more details.

</xsl:comment>
	<xsl:text>
</xsl:text>
	<xsl:apply-templates/>
</xsl:template>

<!--   these attributes have been dropped   -->
<xsl:template match="@package"/>
<xsl:template match="@targetURI"/>
<xsl:template match="@*">
	<xsl:attribute name="{local-name(.)}">
		<xsl:value-of select="."/>
	</xsl:attribute>
</xsl:template>

<xsl:template match="*[local-name()='test']">
	<xsl:variable name="testname" select="@name" />

	<xsl:variable name="newname" select="document('mary.xml')/tests/name[@id=$testname]" />

	<xsl:element namespace="http://www.w3.org/2001/DOM-Test-Suite/Level-1"
	             name="{local-name(.)}">
		<xsl:apply-templates select="@*"/>
		<xsl:if test="$newname">
			<metadata>
				<title><xsl:value-of select="$testname"/></title>
				<creator>NIST</creator>
				<description>
				<xsl:text>
    </xsl:text>
				<xsl:value-of select="$newname"/>
				<xsl:text>
</xsl:text>
				</description>

				<contributor>Mary Brady</contributor>
				<date qualifier="created">2001-08-17</date>
                <xsl:variable name="assertException" select="/*[local-name() = 'test']/*[local-name() = 'assertDOMException']"/>
                <xsl:choose>
                    <xsl:when test="$assertException">
                        <subject>
                            <!--  add a subject for the exception code  -->
                            <xsl:attribute name="resource">
                                <xsl:value-of select="$specURI"/>
                                <xsl:text>xpointer(id('ID-258A00AF')/constant[@name='</xsl:text>
                                <xsl:value-of select="local-name($assertException/*)"/>
                                <xsl:text>'])</xsl:text>
                            </xsl:attribute>
                        </subject>
                        <!--  add a subject for the call that raises the exception  -->
                        <xsl:variable name="feature" select="$assertException/*/*"/>
                        <xsl:apply-templates select="$feature" mode="subject"/>
                        <xsl:variable name="subject" 
                            select="$interfaces/library/interface[not($feature/@interface) or @name=$feature/@interface]/*[(local-name() = 'method' or local-name() = 'attribute') and @name=local-name($feature)]"/>
                        <subject>
                            <xsl:attribute name="resource">
                                <xsl:value-of select="$specURI"/>
                                <xsl:text>xpointer(id('</xsl:text>
                                <xsl:value-of select="$subject/@id"/>
                                <xsl:text>')/</xsl:text>
                                <xsl:choose>
                                    <xsl:when test="local-name($subject)='method'">
                                        <xsl:text>raises</xsl:text>
                                    </xsl:when>
                                    <xsl:when test="$feature/@var">
                                        <xsl:text>getraises</xsl:text>
                                    </xsl:when>
                                    <xsl:otherwise>
                                        <xsl:text>setraises</xsl:text>
                                    </xsl:otherwise>
                                </xsl:choose>
                                <xsl:text>/exception[@name='DOMException']/descr/p[substring-before(.,':')='</xsl:text>
                                <xsl:value-of select="local-name($assertException/*)"/>
                                <xsl:text>'])</xsl:text>
                            </xsl:attribute>
                        </subject>

                    </xsl:when>

                    <xsl:otherwise>
                        <!--  find the last non-assert element, that is probably our subject  -->
                        <xsl:variable name="nonasserts" select="/*[local-name() = 'test']/*[not(contains(local-name(.),'assert'))]"/>
                        <xsl:variable name="lastNA" select="$nonasserts[count($nonasserts)]"/>
                        <xsl:apply-templates select="$lastNA" mode="subject"/>
                    </xsl:otherwise>
                </xsl:choose>
			</metadata>
		</xsl:if>
		<xsl:apply-templates select="*"/>
	</xsl:element>
</xsl:template>

<xsl:template match="*">
	<xsl:element namespace="http://www.w3.org/2001/DOM-Test-Suite/Level-1"
	             name="{local-name(.)}">
	     <xsl:apply-templates select="@*"/>
	     <xsl:apply-templates select="*"/>
	</xsl:element>
</xsl:template>	    

<xsl:template match="*" mode="subject">
    <xsl:variable name="feature" select="."/>
    <xsl:variable name="subject" 
         select="$interfaces/library/interface[not($feature/@interface) or @name=$feature/@interface]/*[(local-name() = 'method' or local-name() = 'attribute') and @name=local-name($feature)]"/>
    <subject resource="{concat($specURI,$subject/@id)}"/>
</xsl:template>

</xsl:stylesheet>

