[xsl] Probelm with xsl:value-of in CSV to XML transform

Subject: [xsl] Probelm with xsl:value-of in CSV to XML transform
From: "Marney Cotterill" <marney@xxxxxxxxxxxxxxxxxxxx>
Date: Fri, 04 Apr 2008 11:28:21 +1000
Hi,

I am so close to having a perfect working stylesheet, this is driving me crazy!

I am converting a CSV into XML, and wanted to transform the following two 
tags:
               <classdate>9/03/2008</classdate>
               <sortdate>2008-03-09</sortdate>

Into:
               <classdate>
                  <items>
                     <item>
                        <label>9/03/2008</label>
                        <value>2008-03-09</value
                     </item>
                  </items>
               </classdate>

Where the <sortdate> value becomes the <value> of the <classdate>

I am almost there, but can't get the value of the <sortdate> to appear in the 
<value> tag.

Here is the relevant section of the XSLT:
<xsl:when test="name()='classdate'">
                <xsl:element name="classdate">
                  <items>
                    <item>
                      <label>
                        <xsl:value-of select="."/>
                      </label>
                      <value>
                          <xsl:value-of select="sortdate"/>
                      </value>
                    </item>
                  </items>
                </xsl:element>
              </xsl:when>

I have tried a few different methods I have found, being:
<xsl:value-of select="sortdate"/>, this resulted in <value/>
<xsl:value-of select="name()='sortdate'"/>, this resulted in <value>false</value>
<xsl:value-of select="/root/row/sortdate"/>, this placed all instances of the 
sortdate from all records into the field, and was as close as i got.

This is the last tiny peice of this puzzle! ANy help would be super!

Thanks,
Marney


Full XSLT stylesheet below:

<?xml version="1.0"?>
<!--
	A CSV to XML transform
	Version 2
	Andrew Welch
	http://andrewjwelch.com
	
	Modify or supply the $pathToCSV parameter and run the transform
	using "main" as the initial template.
	
	For bug reports or modification requests contact me at 
andrew.j.welch@xxxxxxxxx
-->
  		
<xsl:stylesheet version="2.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
	xmlns:xs="http://www.w3.org/2001/XMLSchema";
  xmlns:msxsl="urn:schemas-microsoft-com:xslt"
	xmlns:fn="fn"
	exclude-result-prefixes="xs fn">

<xsl:output indent="yes" encoding="US-ASCII"/>

<xsl:param name="pathToCSV" select="'file:///c:/fish.csv'"/>

<xsl:function name="fn:getTokens" as="xs:string+">
	<xsl:param name="str" as="xs:string"/>
		<xsl:analyze-string select="concat($str, ',')" 
regex='(("[^"]*")+|[^,]*),'>
			<xsl:matching-substring>
				<xsl:sequence 
select='replace(regex-group(1), "^""|""$|("")""", "$1")'/>
			</xsl:matching-substring>
		</xsl:analyze-string>
</xsl:function>

<xsl:template match="/" name="main">
  <xsl:variable name="csvconverted">
  <xsl:choose>
    <xsl:when test="unparsed-text-available($pathToCSV)">
      <xsl:variable name="csv" select="unparsed-text($pathToCSV)"/>
      <xsl:variable name="lines" select="tokenize($csv, '&#xD;&#xA;')" 
as="xs:string+"/>
      <xsl:variable name="elemNames" select="fn:getTokens($lines[1])" 
as="xs:string+"/>
        <root>
          <xsl:for-each select="$lines[position() > 1]">
            <row>
              <xsl:variable name="lineItems" select="fn:getTokens(.)" 
as="xs:string+"/>

              <xsl:for-each select="$elemNames">
                <xsl:variable name="pos" select="position()"/>
                <xsl:element name="{.}">
                  <xsl:value-of select="$lineItems[$pos]"/>
                </xsl:element>
              </xsl:for-each>
            </row>
          </xsl:for-each>
        </root>
      </xsl:when>
    <xsl:otherwise>
      <xsl:text>Cannot locate : </xsl:text>
      <xsl:value-of select="$pathToCSV"/>
    </xsl:otherwise>
  </xsl:choose>
  </xsl:variable>
  <xsl:apply-templates select="$csvconverted/root"/>
</xsl:template>


  <xsl:template match="/root">
    <xmodexport version="4.0" generationdate="2008-03-27 22:52:04.984" 
portalid="0">
      <records>
        <xsl:apply-templates select="row">
        </xsl:apply-templates>
      </records>
    </xmodexport>
  </xsl:template>
  <xsl:template match="row">
    <record id="-1" formid="22" portalid="0" adduser="1" updateuser="1" 
approved="true" dateadded="2008-04-18 00:00:00.000" datemodified="2006-04-
18 00:00:00.000" displaydate="2006-04-17 00:00:00.000" expirydate="9999-12-
31 23:59:59.000">
      <InstanceData>
        <xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text>
        <instance>
          <xsl:for-each select="node()">
            <xsl:choose>
              <xsl:when test="name()='daytime'">
                <xsl:element name="daytime">
                  <xsl:choose>
                    <xsl:when test="text()='yes'">
                      <items>
                        <item>
                          <label>yes</label>
                          <value>**DAYTIME CLASS**</value>
                        </item>
                      </items>
                    </xsl:when>
                    <xsl:when test="text()='no'">
                      <items>
                        <item>
                          <label>no</label>
                          <value></value>
                        </item>
                      </items>
                    </xsl:when>
                    <xsl:otherwise>
                      <items/>
                    </xsl:otherwise>
                  </xsl:choose>
                </xsl:element>
              </xsl:when>
              <xsl:when test="name()='classdate'">
                <xsl:element name="classdate">
                  <items>
                    <item>
                      <label>
                        <xsl:value-of select="."/>
                      </label>
                      <value>
                          <xsl:value-of select="sortdate"/>
                      </value>
                    </item>
                  </items>
                </xsl:element>
              </xsl:when>
              <xsl:when test="name()='sortdate'"/>
              <xsl:otherwise>
                  <xsl:copy-of select="."></xsl:copy-of>
                </xsl:otherwise>
              </xsl:choose>
          </xsl:for-each>
          <classfull>
            <items>
              <item>
                <label>No</label>
                <value></value>
              </item>
            </items>
          </classfull>
        </instance>
        <xsl:text disable-output-escaping="yes">]]&gt;</xsl:text>
      </InstanceData>
    </record>
  </xsl:template>

  </xsl:stylesheet>
-- 

Current Thread