<?xml version="1.0" encoding="iso-8859-1" standalone="yes"?>

<!DOCTYPE xsl:stylesheet [
  <!ELEMENT Header (#PCDATA)>
]>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  
  <xsl:output method="text" indent="yes" />
  <!--<xsl:param name="te"/>-->

  <xsl:template match="/">
        <Header>TE Site&#9;Point&#9;Latitude&#9;Longitude&#9;HAE&#9;CE90&#9;LE90&#9;Image IDs&#9;User Data&#9;Line/Sample<xsl:text>
</xsl:text></Header>
    <xsl:apply-templates select="//Target" />
  </xsl:template>  

  <xsl:template match="Target">

  	<xsl:for-each select="JDPI">
     	<xsl:value-of select="@JDPISite"/>
		<xsl:value-of select="'&#9;'"/>
		<xsl:value-of select="@JDPIId"/>
		<xsl:value-of select="'&#9;'"/>

     	<xsl:variable name="coord" select="Coord" />
        <xsl:choose>   <!--North or South?--> 
          <xsl:when test="contains($coord,&apos;N&apos;)">
            <xsl:variable name="lat" select="concat(substring($coord,1,6), '.', substring($coord,7,3), ' N')" />
            <xsl:value-of select="normalize-space($lat)" />
            <xsl:value-of select="'&#9;'"/>
          </xsl:when>
          <xsl:when test="contains($coord,&apos;S&apos;)">
            <xsl:variable name="lat" select="concat(substring($coord,1,6), '.', substring($coord,7,3), ' S')" />
            <xsl:value-of select="normalize-space($lat)" />
            <xsl:value-of select="'&#9;'"/>
          </xsl:when>
        </xsl:choose>
        <xsl:choose>   <!--East or West?--> 
          <xsl:when test="contains($coord,&apos;E&apos;)">
            <xsl:variable name="lon" select="concat(substring($coord,11,7), '.', substring($coord,18,3), ' E')" />
            <xsl:value-of select="normalize-space($lon)" />
            <xsl:value-of select="'&#9;'"/>
          </xsl:when>
          <xsl:when test="contains($coord,&apos;W&apos;)">
            <xsl:variable name="lon" select="concat(substring($coord,11,7), '.', substring($coord,18,3), ' W')" />
            <xsl:value-of select="normalize-space($lon)" />
            <xsl:value-of select="'&#9;'"/>
          </xsl:when>
        </xsl:choose>

		<xsl:value-of select="@ElevationHAE" />
		<xsl:value-of select="'&#9;'"/>
		<xsl:value-of select="@CoordDerivAcc" />
		<xsl:value-of select="'&#9;'"/>
		<xsl:value-of select="@ElevationHAEDerivedAccuracy" />
		<xsl:value-of select="'&#9;'"/>
    </xsl:for-each>

      <!-- Get the image IDs and add a space between each ID -->
      <xsl:for-each select="MensurationDetail/ImagePoints/Image">
        <xsl:variable name="imgid" select="concat(translate(@filename, ' ', '_'), ' ')" />  <!--Replace spaces in @id between ed. and ed. # w/ an "_", then concatenate and add a space between image IDs-->
        <xsl:value-of select="$imgid" />
		<xsl:value-of select="'&#9;'"/>
      </xsl:for-each>

<!--<xsl:text>
</xsl:text>-->

  </xsl:template>
          
</xsl:stylesheet>
