<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="no"/>
	
	<!--
		Stylesheet to canonicalize the WSDL Component Model Interchange format (wsdlcm)
			- canonicalizes xml:id and @ref attributes
		Created: March 27, 2006, Jonathan Marsh, Microsoft.  jmarsh@microsoft.com
	-->

	<!-- identity transform -->
	<xsl:template match="*[not(node())]">
		<!-- prevent unwanted whitespace from distracting human readers ;-) -->
		<xsl:copy><xsl:apply-templates select="@*"/></xsl:copy>
	</xsl:template>
	<xsl:template match="*">
		<xsl:copy>
			<xsl:apply-templates select="@*"/>
			<xsl:apply-templates/>
		</xsl:copy>
	</xsl:template>
	<xsl:template match="@*">
		<xsl:copy-of select="."/>
	</xsl:template>
	<xsl:template match="comment()">
		<xsl:copy-of select="."/>
	</xsl:template>
	
	<!-- rules to canonicalize xml:id attribute values and references to them -->
	<xsl:template match="@xml:id">
		<xsl:attribute name="xml:id">c<xsl:value-of select="count(ancestor::*[@xml:id] | preceding::*[@xml:id])"/></xsl:attribute>
	</xsl:template>
	<xsl:template match="@ref">
		<xsl:for-each select="//*/@xml:id[.=current()]">
			<xsl:attribute name="ref">c<xsl:value-of select="count(ancestor::*[@xml:id] | preceding::*[@xml:id])"/></xsl:attribute>
		</xsl:for-each>
	</xsl:template>
	
</xsl:stylesheet>
