[xsl] Adding a translate function to the CSV to XML transform XSLT v2 from Andrew Welch

Subject: [xsl] Adding a translate function to the CSV to XML transform XSLT v2 from Andrew Welch
From: "Marney Cotterill" <marney@xxxxxxxxxxxxxxxxxxxx>
Date: Wed, 14 May 2008 14:11:18 +1000
Hi,

Using XML version 1.0, processing with Kernow.

Being quite new to XML and XSLT, I have managed with lots of help from this 
list to modify Andrew's original CSV to XML XSLT transform to my needs.

I have a few small bugs which I was hoping could be fixed with a translate 
function, but have no idea where to put this, and if it is require to reside in it's 
own template and be called seperatley.

In the original CSVfile, I have 3 characters that are breaking the transform, I 
was hoping to replace these as such:

 replace with '  (right single quotation marks with apostrophies)
i replace with e
 replace with -   (en dash with a hyphen/minus)

within the XSLT with
<xsl:value-of select='translate(.,'',''')'/>
<xsl:value-of select='translate(.,'i','e')'/>
<xsl:value-of select='translate(.,'','-')'/>

If someone could point me in the right direction of where to place these 
translate declarations I would be much obliged. I am guessing this should 
probably be a template I run on the CSV first? 

The XSLT is 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 msxsl">

  <xsl:output indent="yes" encoding="UTF-8"/>

  <xsl:param name="pathToCSV" select="'file:///c:/Documents and 
Settings/test.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="self::daytime">
                <daytime>
                  <xsl:choose>
                    <xsl:when test=".='yes'">
                      <items>
                        <item>
                          <label>yes</label>
                          <value>**DAYTIME CLASS**</value>
                        </item>
                      </items>
                    </xsl:when>
                    <xsl:when test=".='no'">
                      <items>
                        <item>
                          <label>no</label>
                          <value></value>
                        </item>
                      </items>
                    </xsl:when>
                    <xsl:otherwise>
                      <items/>
                    </xsl:otherwise>
                  </xsl:choose>
                </daytime>
              </xsl:when>
              <xsl:when test="self::classdate">
                <classdate>
                  <items>
                    <item>
                      <label>
                        <xsl:value-of select="."/>
                      </label>
                      <value><xsl:value-of select="following-sibling::sortdate[1]"/> 
00:00:00</value>
                    </item>
                  </items>
                </classdate>
              </xsl:when>
              <xsl:when test="self::sortdate"/>
              <xsl:when test="self::xmodid">
                <xmodid>
                  <xsl:choose>
                    <xsl:when test=".='Alex Kearns'">
                      <items>
                        <item>
                          <label>Alex Kearns</label>
                          <value>333</value>
                        </item>
                      </items>
                    </xsl:when>
                    <xsl:when test=".='Ashley Hughes'">
                      <items>
                        <item>
                          <label>Ashley Hughes</label>
                          <value>330</value>
                        </item>
                      </items>
                    </xsl:when>
                    <xsl:when test=".='Australian Gourmet Traveller'">
                      <items>
                        <item>
                          <label>Australian Gourmet Traveller</label>
                          <value>325</value>
                        </item>
                      </items>
                    </xsl:when>
                    <xsl:when test=".='Cheong Liew'">
                      <items>
                        <item>
                          <label>Cheong Liew</label>
                          <value>329</value>
                        </item>
                      </items>
                    </xsl:when>
                    <xsl:when test=".='Christine Manfield'">
                      <items>
                        <item>
                          <label>Christine Manfield</label>
                          <value>317</value>
                        </item>
                      </items>
                    </xsl:when>
                    <xsl:when test=".='Damien Pignolet'">
                      <items>
                        <item>
                          <label>Damien Pignolet</label>
                          <value>322</value>
                        </item>
                      </items>
                    </xsl:when>
                    <xsl:when test=".='Daniel Puskas'">
                      <items>
                        <item>
                          <label>Daniel Puskas</label>
                          <value>323</value>
                        </item>
                      </items>
                    </xsl:when>
                    <xsl:when test=".='Darrell Felstead'">
                      <items>
                        <item>
                          <label>Darrell Felstead</label>
                          <value>324</value>
                        </item>
                      </items>
                    </xsl:when>
                    <xsl:when test=".='Giovanni Pilu'">
                      <items>
                        <item>
                          <label>Giovanni Pilu</label>
                          <value>326</value>
                        </item>
                      </items>
                    </xsl:when>
                    <xsl:when test=".='Guillaume Brahimi'">
                      <items>
                        <item>
                          <label>Guillaume Brahimi</label>
                          <value>327</value>
                        </item>
                      </items>
                    </xsl:when>
                    <xsl:when test=".='Hideo Dekura'">
                      <items>
                        <item>
                          <label>Hideo Dekura</label>
                          <value>54</value>
                        </item>
                      </items>
                    </xsl:when>
                    <xsl:when test=".='Jade and Muriel Chen'">
                      <items>
                        <item>
                          <label>Jade and Muriel Chen</label>
                          <value>328</value>
                        </item>
                      </items>
                    </xsl:when>
                    <xsl:when test=".='James Kidman'">
                      <items>
                        <item>
                          <label>James Kidman</label>
                          <value>55</value>
                        </item>
                      </items>
                    </xsl:when>
                    <xsl:when test=".='John Poh'">
                      <items>
                        <item>
                          <label>John Poh</label>
                          <value>320</value>
                        </item>
                      </items>
                    </xsl:when>
                    <xsl:when test=".='Justin North'">
                      <items>
                        <item>
                          <label>Justin North</label>
                          <value>56</value>
                        </item>
                      </items>
                    </xsl:when>
                    <xsl:when test=".='Kathy Snowball'">
                      <items>
                        <item>
                          <label>Kathy Snowball</label>
                          <value>58</value>
                        </item>
                      </items>
                    </xsl:when>
                    <xsl:when test=".='Lyndey Milan'">
                      <items>
                        <item>
                          <label>Lyndey Milan</label>
                          <value>60</value>
                        </item>
                      </items>
                    </xsl:when>
                    <xsl:when test=".='Mark Best'">
                      <items>
                        <item>
                          <label>Mark Best</label>
                          <value>63</value>
                        </item>
                      </items>
                    </xsl:when>
                    <xsl:when test=".='Mark Jensen'">
                      <items>
                        <item>
                          <label>Mark Jensen</label>
                          <value>64</value>
                        </item>
                      </items>
                    </xsl:when>
                    <xsl:when test=".='Matthew Moran'">
                      <items>
                        <item>
                          <label>Matthew Moran</label>
                          <value>62</value>
                        </item>
                      </items>
                    </xsl:when>
                    <xsl:otherwise>
                      <items>
                        <item>
                          <label>No Guest Presenter</label>
                          <value></value>
                        </item>
                      </items>
                    </xsl:otherwise>
                  </xsl:choose>
                </xmodid>
              </xsl:when>
              <xsl:otherwise>
                <xsl:copy-of select="."></xsl:copy-of>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:for-each>
          <xmod_rfvclassdescription></xmod_rfvclassdescription>
          <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