[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Modification to The CSV to XML transform XSLT v2 f

Subject: Re: Modification to The CSV to XML transform XSLT v2 from Andrew Welch
From: "Marney Cotterill" <marney@xxxxxxxxxxxxxxxxxxxx>
Date: Fri, 28 Mar 2008 12:24:05 +1000
Re:  Modification to The CSV to XML transform XSLT v2 f
Quote from Andrew

"If you do have problems, I would recommend leaving this transform
as-is, then running another transform over the result to convert the
attributes values to element names which handles any problem names."

I have found a snipped from a style sheet that I think will to do the transform 
Andrew has suggested above, but i'm not sure how to implement it - or if it is 
what you are suggesting.

The snippet:
 <xsl:template match="*" name="main">
    <xsl:element name="{local-name()}">
      <xsl:apply-templates select="@*|node()"/>

  <xsl:template match="@*">
    <xsl:attribute name="{local-name()}">
      <xsl:value-of select="."/>

  <xsl:template match="processing-instruction()|comment()">
      <xsl:apply-templates select="node()"/>

Not sure how to add this into my Stylesheet, below:

<?xml version="1.0"?>
	A CSV to XML transform
	Version 2
	Andrew Welch
	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 
<xsl:stylesheet version="2.0"
	exclude-result-prefixes="xs fn">

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

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

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

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

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

  <xsl:template match="/root">
    <xmodexport version="4.0" generationdate="2008-03-27 22:52:04.984" 
        <xsl:apply-templates select="row">
  <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">
        <xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text>
          <xsl:copy-of select="./*"></xsl:copy-of>
        <xsl:text disable-output-escaping="yes">]]&gt;</xsl:text>


I know it has to go inbetween the two templates, with another variable so I can 
call the transformed data into my second template.

Any direction would be great!

Thanks for all the pointers!

Current Thread


Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
First Name
Last Name
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.