<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
		xmlns:lom="http://ltsc.ieee.org/xsd/LOM"
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0"
		xmlns:str="http://exslt.org/strings"
                extension-element-prefixes="str">

  <xsl:import href="classpath:/properties/xsl/ext/all-exslt/str/str.xsl" />


  <xsl:output method="xml" indent="yes" encoding="utf-8" />


  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
  </xsl:template>


  <xsl:template
      match="lom:entity">

    <xsl:variable name="vcard-string" select="."/>

    <xsl:variable name="vcard">
      <xsl:for-each select="str:tokenize($vcard-string, '&#x0a;')">
	<xsl:variable name="name-attributes" select="str:tokenize(substring-before(., ':'), ';')" as="xs:string*"/>
	<xsl:if test="$name-attributes[1] != 'FN' and $name-attributes[1] != 'N'">
	  <xsl:value-of select="."/>
	  <xsl:value-of select="'&#x0a;'"/>
	</xsl:if>
	<xsl:if test="$name-attributes[1] = 'N'">
	  <xsl:value-of select="."/>
	  <xsl:value-of select="'&#x0a;'"/>
	  <xsl:if test="contains(.,';')">
	    <xsl:value-of select="'FN:'"/><xsl:value-of select="substring-after(substring-after(.,':'),';')"/><xsl:value-of select="' '"/><xsl:value-of select="substring-before(substring-after(.,':'),';')"/><xsl:value-of select="'&#x0a;'"/>
	  </xsl:if>	
	  <xsl:if test="not(contains(.,';'))">
	    <xsl:value-of select="'FN:'"/><xsl:value-of select="substring-after(.,':')"/><xsl:value-of select="'&#x0a;'"/>
	  </xsl:if>
	</xsl:if>
      </xsl:for-each>
    </xsl:variable>
    <!-- Output existing elements -->
    <lom:entity><xsl:copy-of select="$vcard"/></lom:entity>

  </xsl:template>



</xsl:stylesheet>
