<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" 
	xmlns:gco="http://www.isotc211.org/2005/gco" 
	xmlns:gmd="http://www.isotc211.org/2005/gmd" 
	xmlns:gml="http://www.opengis.net/gml"
	xmlns:gts="http://www.isotc211.org/2005/gts" 
	xmlns:gmi="http://www.isotc211.org/2005/gmi" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:xlink="http://www.w3.org/1999/xlink" 
	xmlns:saxon="http://saxon.sf.net/"
	xmlns:srv="http://www.isotc211.org/2005/srv">
	<xd:doc xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl" scope="stylesheet">
		<xd:desc>
			<xd:p><xd:b>Created on:</xd:b>Dec. 16, 2011</xd:p>
			<xd:p><xd:b>Author:</xd:b>ted.habermann@noaa.gov</xd:p>
			<xd:p/>
		</xd:desc>
	</xd:doc>
	<!-- Parameters to identify files to process -->
	<xsl:param name="recordSetPath"/>
	<xsl:param name="fileNamePattern"/>
	<xsl:variable name="rowStart" select="'{'"/>
	<xsl:variable name="rowEnd" select="'}'"/>
	<xsl:variable name="rowSeparator" select="'&#10;'"/>
	<xsl:variable name="itemStart" select="'&quot;'"/>
	<xsl:variable name="itemEnd" select="'&quot;'"/>
	<xsl:variable name="itemSeparator" select="','"/>
	<xsl:variable name="componentID" select="@id"></xsl:variable>
	<xsl:output method="text"/>
	<!--<xsl:key  use="concat(normalize-space(gmd:individualName/gco:CharacterString),normalize-space(gmd:organisationName/gco:CharacterString),gmd:role/gmd:CI_RoleCode)"/>-->
	<xsl:key name="uniqueServices" match="srv:SV_ServiceIdentification" use="concat(normalize-space(gmd:citation/gmd:CI_Citation/gmd:title/gco:CharacterString),normalize-space(srv:serviceType/gco:LocalName),normalize-space(srv:serviceTypeVersion[1]/gco:CharacterString))"/>
	<xsl:strip-space elements="*"/>
	<xsl:template match="/">
		<xsl:text>{"identifier":"rowIdentifier", "items":[&#10;</xsl:text>
		<xsl:for-each select="//srv:SV_ServiceIdentification[generate-id() = 
			generate-id(key('uniqueServices',concat(normalize-space(gmd:citation/gmd:CI_Citation/gmd:title/gco:CharacterString),normalize-space(srv:serviceType/gco:LocalName),normalize-space(srv:serviceTypeVersion[1]/gco:CharacterString)))[1])]">
				<xsl:variable name="rowIdentifier" select="position()"/>
				<xsl:value-of select="$rowStart"/>
				<xsl:call-template name="writeJsonItem">
					<xsl:with-param name="id" select="'rowIdentifier'"/>
					<xsl:with-param name="value" select="$rowIdentifier"/>
				</xsl:call-template>
			    <xsl:value-of select="$itemSeparator"/>
			    <xsl:call-template name="writeJsonItem">
				   <xsl:with-param name="id" select="'uuid'"/>
				   <xsl:with-param name="value" select="@uuid"/>
			    </xsl:call-template>
				<xsl:value-of select="$itemSeparator"/>
				<xsl:call-template name="writeJsonItem">
					<xsl:with-param name="id" select="'title'"/>
					<xsl:with-param name="value" select="normalize-space(replace(./gmd:citation/gmd:CI_Citation/gmd:title/gco:CharacterString,'&quot;','\\&quot;'))"/>
				</xsl:call-template>
				<xsl:value-of select="$itemSeparator"/>
				<xsl:call-template name="writeJsonItem">
					<xsl:with-param name="id" select="'serviceType'"/>
					<xsl:with-param name="value" select="normalize-space(replace(./srv:serviceType/gco:LocalName,'&quot;','\\&quot;'))"/>
				</xsl:call-template>
				<xsl:value-of select="$itemSeparator"/>
				<xsl:call-template name="writeJsonItem">
					<xsl:with-param name="id" select="'serviceTypeVersion'"/>
					<xsl:with-param name="value" select="normalize-space(./srv:serviceTypeVersion[1]/gco:CharacterString)"/>
				</xsl:call-template>
				<xsl:value-of select="$itemSeparator"/>
				<xsl:call-template name="writeJsonItem">
					<xsl:with-param name="id" select="'operationName'"/>
					<xsl:with-param name="value" select="normalize-space(./srv:containsOperations[1]/srv:SV_OperationMetadata/srv:operationName/gco:CharacterString)"/>
				</xsl:call-template>
				<xsl:value-of select="$itemSeparator"/>
				<xsl:call-template name="writeJsonNumberItem">
					<xsl:with-param name="id" select="'count'"/>
					<xsl:with-param name="value" select="count(key('uniqueServices',concat(normalize-space(gmd:citation/gmd:CI_Citation/gmd:title/gco:CharacterString),normalize-space(srv:serviceType/gco:LocalName),normalize-space(srv:serviceTypeVersion[1]/gco:CharacterString))))"/>
				</xsl:call-template>
				<xsl:value-of select="$rowEnd"/>
				<xsl:if test="not(position() = last())">,</xsl:if>
				<xsl:value-of select="$rowSeparator"/>
			</xsl:for-each>
		<xsl:text>]}</xsl:text>
	</xsl:template>
	<xsl:template name="writeJsonItem">
		<xsl:param name="id"/>
		<xsl:param name="value"/>
	<xsl:value-of select="concat('&quot;',$id,'&quot;:&quot;',$value,'&quot;')"/>
	</xsl:template>
	<xsl:template name="writeJsonNumberItem">
		<xsl:param name="id"/>
		<xsl:param name="value"/>
		<xsl:value-of select="concat('&quot;',$id,'&quot;',':',$value)"/>
	</xsl:template>
</xsl:stylesheet>
