<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:mets="http://www.loc.gov/METS/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:dcterms="http://purl.org/dc/terms/"
	xmlns:dnx="http://www.exlibrisgroup.com/dps/dnx">
	<xsl:output method="text" indent="no" />

	<xsl:variable name="PID"
		select="/mets:mets/mets:amdSec[@ID=$IE_AMD_ID]/mets:techMD/mets:mdWrap/mets:xmlData/dnx:dnx/dnx:section[@id='internalIdentifier']/dnx:record[dnx:key[@id='internalIdentifierType']='PID']/dnx:key[@id='internalIdentifierValue']" />
	<xsl:variable name="CONTEXT">"@context" : "http://iiif.io/api/presentation/2/context.json"</xsl:variable>
    <xsl:variable name="SERVICE_CONTEXT">"@context" : "http://iiif.io/api/image/2/context.json"</xsl:variable>
	<xsl:variable name="ID">"@id" : "</xsl:variable>
	<xsl:variable name="TYPE">"@type" : "</xsl:variable>
	<xsl:variable name="ON">"on" : "</xsl:variable>
	<xsl:variable name="DELIVERY_BASE_URL">baseURLPlaceHolderdelivery/</xsl:variable>
	<xsl:variable name="IMAGE_SERVER_BASE_URL">imageServerBaseURLPlaceHolder</xsl:variable>
    <xsl:variable name="UI_CUSTOMIZATION">UIcustomizatioIDplaceHolder</xsl:variable>
	<xsl:variable name="URI_PREFIX">iiif/presentation/2.1/</xsl:variable>
	<xsl:variable name="IMAGE_SERVER_PREFIX">cantaloupe/iiif/2/</xsl:variable>
	<xsl:variable name="DVS">DVSplaceHolder</xsl:variable>
	<xsl:variable name="OBJECT_TYPE_IE">IE</xsl:variable>
	<xsl:variable name="OBJECT_TYPE_REPRESENTATION">REP</xsl:variable>
	<xsl:variable name="OBJECT_TYPE_FILE">FILE</xsl:variable>
	<xsl:variable name="LABEL">"label" : "</xsl:variable>
	<xsl:variable name="VALUE">"value" : "</xsl:variable>
	<xsl:variable name="IE_AMD_ID">ie-amd</xsl:variable>
	<xsl:variable name="IE_DMD_ID">ie-dmd</xsl:variable>
	<xsl:variable name="DESCRIPTION">"description": "</xsl:variable>
	<xsl:variable name="ATTRIBUTION">"attribution": "</xsl:variable>
	<xsl:variable name="METADATA">"metadata": [</xsl:variable>
	<xsl:variable name="LOGO">"logo": "</xsl:variable>
	<xsl:variable name="LICENSE">"license": "</xsl:variable>
	<xsl:variable name="THUMBNAIL">"thumbnail": "</xsl:variable>
	<xsl:variable name="MOTIVATION">"motivation": "sc:painting",</xsl:variable>
	<xsl:variable name="VIEWING_DIRECTION">"viewingDirection": </xsl:variable>
	<xsl:variable name="VIEWING_HINT">"viewingHint": </xsl:variable>
	<xsl:variable name="WIDTH">"width": </xsl:variable>
	<xsl:variable name="HEIGHT">"height": </xsl:variable>
	<xsl:variable name="WIDTH_PLACE_HOLDER">"widthPlaceholder</xsl:variable>
	<xsl:variable name="HEIGHT_PLACE_HOLDER">"heightPlaceholder</xsl:variable>
	<xsl:variable name="INFO_JSON">/info.json</xsl:variable>
	<xsl:variable name="FILE_FORMAT">"format": "</xsl:variable>
	<xsl:variable name="PROFILE">"profile": "http://iiif.io/api/image/2/level1.json" }</xsl:variable>

	<xsl:template match="/">
		{
		<xsl:call-template name="MANIFEST" />
		<xsl:call-template name="SEQUENCES" />
		<xsl:call-template name="RANGES" />
		}
	</xsl:template>

	<xsl:template name="IE_PROPERTY">
		<xsl:param name="DC_FIELD_NAME" />
		<xsl:param name="DCTERMS_FIELD_NAME" />
		<xsl:choose>
			<xsl:when test="/mets:mets/mets:dmdSec[@ID=$IE_DMD_ID]/mets:mdWrap/mets:xmlData/dc:record/*[name()=$DC_FIELD_NAME]">
				<xsl:value-of select="/mets:mets/mets:dmdSec[@ID=$IE_DMD_ID]/mets:mdWrap/mets:xmlData/dc:record/*[name()=$DC_FIELD_NAME]" />
			</xsl:when>
			<xsl:when test="(//mets:dmdSec[@ID=$IE_DMD_ID]/mets:mdWrap/mets:xmlData/dc:record/*[name()=$DCTERMS_FIELD_NAME])[1]">
				<xsl:value-of select="(//mets:dmdSec[@ID=$IE_DMD_ID]/mets:mdWrap/mets:xmlData/dc:record/*[name()=$DCTERMS_FIELD_NAME])[1]" />
			</xsl:when>
			<xsl:otherwise></xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template name="IE_METADATA">
		<xsl:for-each select="/mets:mets/mets:dmdSec[@ID=$IE_DMD_ID]/mets:mdWrap/mets:xmlData/dc:record/dc:creator |
		                      /mets:mets/mets:dmdSec[@ID=$IE_DMD_ID]/mets:mdWrap/mets:xmlData/dc:record/dcterms:creator ">
			{<xsl:value-of select="$LABEL" />Author", <xsl:value-of select="$VALUE" />
			<xsl:value-of select="." />"}
            <xsl:if test="position()!=last() or count(//dc:subject) > 0 or count(//dcterms:subject) > 0">, </xsl:if>
		</xsl:for-each>
		<xsl:for-each select="/mets:mets/mets:dmdSec[@ID=$IE_DMD_ID]/mets:mdWrap/mets:xmlData/dc:record/dc:subject |
		                      /mets:mets/mets:dmdSec[@ID=$IE_DMD_ID]/mets:mdWrap/mets:xmlData/dc:record/dcterms:subject">
			{<xsl:value-of select="$LABEL" />Subject", <xsl:value-of select="$VALUE" />
		 <xsl:value-of select="." />"}
         <xsl:if test="position()!=last()">, </xsl:if>
		</xsl:for-each>
		]
	</xsl:template>

	<xsl:template name="MANIFEST">
		<xsl:value-of select="$CONTEXT" />,
		<xsl:value-of select="$ID" /><xsl:value-of select="$DELIVERY_BASE_URL" /><xsl:value-of select="$URI_PREFIX" /><xsl:value-of select="$PID" />/manifest",
		<xsl:value-of select="$TYPE" />sc:Manifest",
		<xsl:value-of select="$LABEL" />
		<xsl:call-template name="IE_PROPERTY">
			<xsl:with-param name="DC_FIELD_NAME" select='"dc:title"' />
			<xsl:with-param name="DCTERMS_FIELD_NAME" select='"dcterms:title"' />
		</xsl:call-template>",
		<xsl:value-of select="$DESCRIPTION" />
		<xsl:call-template name="IE_PROPERTY">
			<xsl:with-param name="DC_FIELD_NAME" select='"dc:description"' />
			<xsl:with-param name="DCTERMS_FIELD_NAME" select='"dcterms:description"' />
		</xsl:call-template>",
		<xsl:value-of select="$ATTRIBUTION" />
		<xsl:call-template name="IE_PROPERTY">
			<xsl:with-param name="DC_FIELD_NAME" select='"dc:rights"' />
			<xsl:with-param name="DCTERMS_FIELD_NAME" select='"dcterms:rights"' />
		</xsl:call-template>",
		<xsl:value-of select="$METADATA" />
		<xsl:call-template name="IE_METADATA" />,

		<xsl:choose>
			<xsl:when
				test="/mets:mets/mets:amdSec[@ID='ie-amd']/mets:techMD[@ID='ie-amd-tech']/mets:mdWrap/mets:xmlData/dnx:dnx/dnx:section[@id='objectCharacteristics']/dnx:record/dnx:key[@id='owner']">
				<xsl:value-of select="$LOGO" /><xsl:value-of select="$DELIVERY_BASE_URL" />images/<xsl:value-of select="$UI_CUSTOMIZATION" />/logo-main.png",
				<xsl:value-of select="$LICENSE" /><xsl:value-of select="$DELIVERY_BASE_URL" />general/termsofuse.jsp?dps_dvs=DVSplaceHolder",
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$LOGO" /><xsl:value-of select="$DELIVERY_BASE_URL" />images/1/logo-main.png",
				<xsl:value-of select="$LICENSE" /><xsl:value-of select="$DELIVERY_BASE_URL" />general/termsofuse.jsp",
			</xsl:otherwise>
		</xsl:choose>

		<xsl:value-of select="$THUMBNAIL" /><xsl:value-of select="$DELIVERY_BASE_URL" />DeliveryManagerServlet?dps_pid=<xsl:value-of select="$PID" />&amp;dps_func=thumbnail",
	</xsl:template>

	<xsl:template name="SEQUENCES">
		"sequences": [{
		<xsl:variable name="REP_ID">
			<xsl:value-of select="/mets:mets/mets:fileSec/mets:fileGrp/@ID" />
		</xsl:variable>
		<xsl:variable name="HAS_PHYSICAL_SM" select="count(/mets:mets/mets:structMap[@TYPE='PHYSICAL']) > 0" />
		<xsl:variable name="VIEWING_DIRECTION_VALUE">
			<xsl:choose>
				<xsl:when test="/mets:mets/mets:dmdSec[@ID='ie-dmd']/mets:mdWrap/mets:xmlData/dc:record/dc:language='heb'">
					"right-to-left"
				</xsl:when>
				<xsl:when test="/mets:mets/mets:dmdSec[@ID='ie-dmd']/mets:mdWrap/mets:xmlData/dc:record/dc:language='ara'">
					"right-to-left"
				</xsl:when>
				<xsl:when test="/mets:mets/mets:dmdSec[@ID='ie-dmd']/mets:mdWrap/mets:xmlData/dc:record/dc:language='chi'">
					"top-to-bottom"
				</xsl:when>
				<xsl:otherwise>
					"left-to-right"
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:variable name="VIEWING_HINT_VALUE">
			<xsl:choose>
				<xsl:when
					test="/mets:mets/mets:amdSec[@ID='ie-amd']/mets:techMD[@ID='ie-amd-tech']/mets:mdWrap/mets:xmlData/dnx:dnx/dnx:section[@id='generalIECharacteristics']/dnx:record/dnx:key[@id='IEEntityType']='Book'">
					"paged",
				</xsl:when>
				<xsl:otherwise>
					"individuals",
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:value-of select="$ID" /><xsl:value-of select="$DELIVERY_BASE_URL" /><xsl:value-of select="$URI_PREFIX" /><xsl:value-of select="$PID" />/sequence/<xsl:value-of select="$REP_ID" />",
		<xsl:value-of select="$TYPE" />sc:Sequence",
		<xsl:value-of select="$LABEL" />
		<xsl:choose>
			<xsl:when test="$HAS_PHYSICAL_SM = true()">
				<xsl:value-of select="/mets:mets/mets:structMap[@TYPE='PHYSICAL']/mets:div/mets:div/@LABEL" />",
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of
					select="/mets:mets/mets:amdSec[@ID=concat($REP_ID,'-amd')]/mets:techMD[@ID=concat($REP_ID,'-amd-tech')]/mets:mdWrap/mets:xmlData/dnx:dnx/dnx:section[@id='generalRepCharacteristics']/dnx:record/dnx:key[@id='label']" />",
			</xsl:otherwise>
		</xsl:choose>
		<xsl:value-of select="$VIEWING_DIRECTION" /><xsl:value-of select="$VIEWING_DIRECTION_VALUE" />,
		<xsl:value-of select="$VIEWING_HINT" /><xsl:value-of select="$VIEWING_HINT_VALUE" />
		"canvases": [
		<xsl:choose>
			<xsl:when test="$HAS_PHYSICAL_SM = true()">
				<xsl:for-each select="//mets:structMap[@TYPE='PHYSICAL']/mets:div/mets:div/mets:div[@TYPE='FILE']">
					{
					<xsl:variable name="FILEID" select="./mets:fptr/@FILEID" />
					<xsl:variable name="FILE_LABEL_LABEL"
						select="/mets:mets/mets:amdSec[@ID=concat($FILEID,'-amd')]/mets:techMD[@ID=concat($FILEID,'-amd-tech')]/mets:mdWrap/mets:xmlData/dnx:dnx/dnx:section[@id='generalFileCharacteristics']/dnx:record/dnx:key[@id='label']/text()" />
					<xsl:variable name="FILE_LABEL_NAME"
						select="(/mets:mets/mets:amdSec[@ID=concat($FILEID,'-amd')]/mets:techMD[@ID=concat($FILEID,'-amd-tech')]/mets:mdWrap/mets:xmlData/dnx:dnx/dnx:section[@id='generalFileCharacteristics']/dnx:record/dnx:key[@id='fileOriginalName'])[1]/text()" />
		            <xsl:variable name="FILE_SM_LABEL" select="//mets:div/@LABEL[./mets:fptr/@FILEID=$FILEID]" />
					<xsl:call-template name="CANVAS">
						<xsl:with-param name="FILE_ID" select="$FILEID" />
						<xsl:with-param name="FILE_LABEL">
							<xsl:choose>
								<xsl:when test="$FILE_SM_LABEL">
									<xsl:value-of select="$FILE_SM_LABEL" />
								</xsl:when>
								<xsl:when test="$FILE_LABEL_LABEL">
									<xsl:value-of select="$FILE_LABEL_LABEL" />
								</xsl:when>
								<xsl:when test="$FILE_LABEL_NAME">
									<xsl:value-of select="$FILE_LABEL_NAME" />
								</xsl:when>
								<xsl:otherwise></xsl:otherwise>
							</xsl:choose>
						</xsl:with-param>
					</xsl:call-template>
				</xsl:for-each>
			</xsl:when>
			<xsl:otherwise>
				<xsl:for-each select="//mets:fileGrp/mets:file">
					{
					<xsl:variable name="FILEID" select="//mets:fptr/@FILEID" />
					<xsl:variable name="FILE_LABEL_LABEL"
						select="/mets:mets/mets:amdSec[@ID=concat($FILEID,'-amd')]/mets:techMD[@ID=concat($FILEID,'-amd-tech')]/mets:mdWrap/mets:xmlData/dnx:dnx/dnx:section[@id='generalFileCharacteristics']/dnx:record/dnx:key[@id='label']/text()" />
					<xsl:variable name="FILE_LABEL_NAME"
						select="(/mets:mets/mets:amdSec[@ID=concat($FILEID,'-amd')]/mets:techMD[@ID=concat($FILEID,'-amd-tech')]/mets:mdWrap/mets:xmlData/dnx:dnx/dnx:section[@id='generalFileCharacteristics']/dnx:record/dnx:key[@id='fileOriginalName'])[1]/text()" />
					<xsl:call-template name="CANVAS">
						<xsl:with-param name="FILE_ID" select="$FILEID" />
						<xsl:with-param name="FILE_LABEL">
							<xsl:choose>
								<xsl:when test="$FILE_LABEL_LABEL">
									<xsl:value-of select="$FILE_LABEL_LABEL" />
								</xsl:when>
								<xsl:when test="$FILE_LABEL_NAME">
									<xsl:value-of select="$FILE_LABEL_NAME" />
								</xsl:when>
								<xsl:otherwise></xsl:otherwise>
							</xsl:choose>
						</xsl:with-param>
					</xsl:call-template>
				</xsl:for-each>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template name="CANVAS">
		<xsl:param name="FILE_ID" />
		<xsl:param name="FILE_LABEL" />
		<xsl:variable name="HEIGHT_VALUE"
			select="/mets:mets/mets:amdSec[@ID=concat($FILE_ID,'-amd')]/mets:techMD[@ID=concat($FILE_ID,'-amd-tech')]/mets:mdWrap/mets:xmlData/dnx:dnx/dnx:section/dnx:record[dnx:key[@id='significantPropertiesType']='nisoImage.imageLength']/dnx:key[@id='significantPropertiesValue']" />
		<xsl:variable name="WIDTH_VALUE"
			select="/mets:mets/mets:amdSec[@ID=concat($FILE_ID,'-amd')]/mets:techMD[@ID=concat($FILE_ID,'-amd-tech')]/mets:mdWrap/mets:xmlData/dnx:dnx/dnx:section/dnx:record[dnx:key[@id='significantPropertiesType']='nisoImage.imageWidth']/dnx:key[@id='significantPropertiesValue']" />
		<xsl:variable name="FILE_EXTANSION"
			select="/mets:mets/mets:amdSec[@ID=concat($FILE_ID,'-amd')]/mets:techMD[@ID=concat($FILE_ID,'-amd-tech')]/mets:mdWrap/mets:xmlData/dnx:dnx/dnx:section[@id='generalFileCharacteristics']/dnx:record/dnx:key[@id='fileExtension']" />
		<xsl:variable name="FILE_FORMAT_VALUE"
			select="/mets:mets/mets:amdSec[@ID=concat($FILE_ID,'-amd')]/mets:techMD[@ID=concat($FILE_ID,'-amd-tech')]/mets:mdWrap/mets:xmlData/dnx:dnx/dnx:section[@id='generalFileCharacteristics']/dnx:record/dnx:key[@id='fileMIMEType']" />
		<xsl:value-of select="$CONTEXT" />,
		<xsl:value-of select="$ID" /><xsl:value-of select="$DELIVERY_BASE_URL" /><xsl:value-of select="$URI_PREFIX" /><xsl:value-of select="$PID" />/canvas/<xsl:value-of select="$FILE_ID" />",
		<xsl:value-of select="$TYPE" />sc:Canvas",
		<xsl:value-of select="$LABEL" /><xsl:value-of select="$FILE_LABEL" />",
		<xsl:value-of select="$THUMBNAIL" /><xsl:value-of select="$DELIVERY_BASE_URL" />DeliveryManagerServlet?dps_pid=<xsl:value-of select="$FILE_ID" />&amp;dps_func=thumbnail",
		<xsl:value-of select="$HEIGHT" />
<xsl:choose>
  <xsl:when test="$HEIGHT_VALUE">
	<xsl:value-of select="$HEIGHT_VALUE" />,
	  </xsl:when>
	  <xsl:otherwise>
	   <xsl:value-of select="$HEIGHT_PLACE_HOLDER" /><xsl:value-of select="$IMAGE_SERVER_BASE_URL" /><xsl:value-of select="$IMAGE_SERVER_PREFIX" /><xsl:value-of select="$DVS" />:<xsl:value-of select="$FILE_ID" />.<xsl:value-of select="$FILE_EXTANSION" /><xsl:value-of select="$INFO_JSON" />",
	  </xsl:otherwise>
</xsl:choose>

<xsl:value-of select="$WIDTH" />
<xsl:choose>
  <xsl:when test="$WIDTH_VALUE">
	<xsl:value-of select="$WIDTH_VALUE" />,
	  </xsl:when>
	  <xsl:otherwise>
	   <xsl:value-of select="$WIDTH_PLACE_HOLDER" /><xsl:value-of select="$IMAGE_SERVER_BASE_URL" /><xsl:value-of select="$IMAGE_SERVER_PREFIX" /><xsl:value-of select="$DVS" />:<xsl:value-of select="$FILE_ID" />.<xsl:value-of select="$FILE_EXTANSION" /><xsl:value-of select="$INFO_JSON" />",
	  </xsl:otherwise>
</xsl:choose>
		"images": [{
		<xsl:value-of select="$CONTEXT" />,
		<xsl:value-of select="$ID" /><xsl:value-of select="$DELIVERY_BASE_URL" /><xsl:value-of select="$URI_PREFIX" /><xsl:value-of select="$PID" />/annotation/<xsl:value-of select="$FILE_ID" />",
		<xsl:value-of select="$TYPE" />oa:Annotation",
		<xsl:value-of select="$MOTIVATION" />
		"resource": {
		<xsl:value-of select="$ID" /><xsl:value-of select="$DELIVERY_BASE_URL" /><xsl:value-of select="$URI_PREFIX" /><xsl:value-of select="$PID" />/res/<xsl:value-of select="$FILE_ID" />.<xsl:value-of select="$FILE_EXTANSION" />",
		<xsl:value-of select="$TYPE" />dctypes:Image",
		<xsl:value-of select="$FILE_FORMAT" /><xsl:value-of select="$FILE_FORMAT_VALUE" />",
		"service": {
		<xsl:value-of select="$SERVICE_CONTEXT" />,
		<xsl:value-of select="$ID" /><xsl:value-of select="$IMAGE_SERVER_BASE_URL" /><xsl:value-of select="$IMAGE_SERVER_PREFIX" /><xsl:value-of select="$DVS" />:<xsl:value-of select="$FILE_ID" />.<xsl:value-of select="$FILE_EXTANSION" />",
		<xsl:value-of select="$PROFILE" />,
		<xsl:value-of select="$HEIGHT" />
<xsl:choose>
  <xsl:when test="$HEIGHT_VALUE">
	<xsl:value-of select="$HEIGHT_VALUE" />,
	  </xsl:when>
	  <xsl:otherwise>
	   <xsl:value-of select="$HEIGHT_PLACE_HOLDER" /><xsl:value-of select="$IMAGE_SERVER_BASE_URL" /><xsl:value-of select="$IMAGE_SERVER_PREFIX" /><xsl:value-of select="$DVS" />:<xsl:value-of select="$FILE_ID" />.<xsl:value-of select="$FILE_EXTANSION" /><xsl:value-of select="$INFO_JSON" />",
	  </xsl:otherwise>
</xsl:choose>

<xsl:value-of select="$WIDTH" />
<xsl:choose>
  <xsl:when test="$WIDTH_VALUE">
	<xsl:value-of select="$WIDTH_VALUE" />
	  </xsl:when>
	  <xsl:otherwise>
	   <xsl:value-of select="$WIDTH_PLACE_HOLDER" /><xsl:value-of select="$IMAGE_SERVER_BASE_URL" /><xsl:value-of select="$IMAGE_SERVER_PREFIX" /><xsl:value-of select="$DVS" />:<xsl:value-of select="$FILE_ID" />.<xsl:value-of select="$FILE_EXTANSION" /><xsl:value-of select="$INFO_JSON" />"
	  </xsl:otherwise>
</xsl:choose>
		       },
 		<xsl:value-of select="$ON" /><xsl:value-of select="$DELIVERY_BASE_URL" /><xsl:value-of select="$URI_PREFIX" /><xsl:value-of select="$PID" />/canvas/<xsl:value-of select="$FILE_ID" />"
		     }
		  ]
		 }
		<xsl:if test="position()!=last()">,
		</xsl:if>
	</xsl:template>
	<xsl:template name="RANGES">
		   ]
		  }
		],
		"structures": [
		<xsl:for-each select="/mets:mets/mets:structMap[@TYPE='LOGICAL']/mets:div/mets:div">
			<xsl:choose>
			<xsl:when test="not(./@TYPE='FILE')">
			<xsl:call-template name="RANGE">
				<xsl:with-param name="RANGE_ID" select="/mets:mets/mets:fileSec/mets:fileGrp/@ID" />
			</xsl:call-template>
			</xsl:when>
			<xsl:otherwise>
			 {<xsl:value-of select="$ID" /><xsl:value-of select="$DELIVERY_BASE_URL" /><xsl:value-of select="$URI_PREFIX" /><xsl:value-of select="$PID" />/canvas/<xsl:value-of select="./mets:fptr/@FILEID" />",
				<xsl:value-of select="$TYPE" />sc:Canvas",
			  <xsl:value-of select="$LABEL" /><xsl:value-of select="@LABEL" />"
			</xsl:otherwise>
			</xsl:choose>}
					<xsl:if test="position()!=last()">,	</xsl:if>
		</xsl:for-each>
		]
	</xsl:template>

	<xsl:template name="RANGE">
		<xsl:param name="RANGE_ID" />
		{<xsl:value-of select="$ID" /><xsl:value-of select="$DELIVERY_BASE_URL" /><xsl:value-of select="$URI_PREFIX" /><xsl:value-of select="$PID" />/range/<xsl:value-of select="$RANGE_ID" />",
		<xsl:value-of select="$TYPE" />sc:Range",
		<xsl:value-of select="$LABEL" /><xsl:value-of select="./@LABEL" />",
		<xsl:choose>
			<xsl:when test="./mets:div/@TYPE='FILE'">
				"canvases": [
				<xsl:for-each select="./mets:div">
					"<xsl:value-of select="$DELIVERY_BASE_URL" /><xsl:value-of select="$URI_PREFIX" /><xsl:value-of select="$PID" />/canvas/<xsl:value-of select="./mets:fptr/@FILEID" />"
					<xsl:if test="position()!=last()">, </xsl:if>
				</xsl:for-each>
				],
				"members": [
				<xsl:for-each select="./mets:div">
					{
					<xsl:value-of select="$ID" /><xsl:value-of select="$DELIVERY_BASE_URL" /><xsl:value-of select="$URI_PREFIX" /><xsl:value-of select="$PID" />/canvas/<xsl:value-of select="./mets:fptr/@FILEID" />",
					<xsl:value-of select="$TYPE" />sc:Canvas",
					<xsl:value-of select="$LABEL" /><xsl:value-of select="@LABEL" />"}
					<xsl:if test="position()!=last()">,	</xsl:if>
				</xsl:for-each>
				]
			</xsl:when>
			<xsl:otherwise>
				"ranges": [
				<xsl:for-each select="./mets:div">
			    	"<xsl:value-of select="$DELIVERY_BASE_URL" /><xsl:value-of select="$URI_PREFIX" /><xsl:value-of select="$PID" />/range/<xsl:value-of select="concat($RANGE_ID,'-',position())" />"
					<xsl:if test="position()!=last()">,	</xsl:if>
				</xsl:for-each>
				],
				"members": [
				<xsl:for-each select="./mets:div">
					{
					<xsl:value-of select="$ID" /><xsl:value-of select="$DELIVERY_BASE_URL" /><xsl:value-of select="$URI_PREFIX" /><xsl:value-of select="$PID" />/range/<xsl:value-of select="concat($RANGE_ID,'-',position())" />",
					<xsl:value-of select="$TYPE" />sc:Range",
					<xsl:value-of select="$LABEL" /><xsl:value-of select="@LABEL" />"}
					<xsl:if test="position()!=last()">,	</xsl:if>
				</xsl:for-each>
				]
				<xsl:for-each select="./mets:div">
					  } ,<xsl:call-template name="RANGE">
						<xsl:with-param name="RANGE_ID" select="concat($RANGE_ID,'-',position())" />
					</xsl:call-template>
				</xsl:for-each>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

</xsl:stylesheet>