Using XSLT to convert CSV into XML
Added by Anonymous about 16 years ago
Legacy ID: #5254777 Legacy Poster: Ray Benjamin (rben)
I've been trying to create an XSLT sheet that will read in a CSV file, and convert it into XML. I'd like it to take the column names, make them lower case, replace the spaces with dashes, and use them as element names. For example: The file is called "magicians.csv" Date, First Name, Last Name, Phone Number 9/9/2008,"Harry","Houdini","001-555-9193" 9/19/2008,"Larry","Houdini","101-555-9003" should become: <magicians> <magician> <date>2008-09-09</date> <first-name>Harry</first-name> <last-name>Houdini</last-name> <phone-number>001-555-9193</phone-number> </magician> <magician> <date>2008-09-19</date> <first-name>Larry</first-name> <last-name>Houdini</last-name> <phone-number>101-555-9003</phone-number> <magician> </magicians> Googling, I found that Dr. Kay had given someone some XSLT that he thought would come close. I've tried to make it work, but can't. Mine looks like: <xsl:output method="xml" version="1.1"/> <xsl:template match="/"> <xsl:param name="inputfile"/> <xsl:variable name="lines" select='tokenize(unparsed-text($inputfile,"utf-8"), "\n")' /> <xsl:variable name="field-names" as="xs:string*" select="tokenize($lines[1],',')"/> <element name="time-card-data"> <xsl:for-each select="subsequence($lines,2)"> <timecard> <xsl:variable name="cells" select="tokenize(., ',')"/> <xsl:for-each select="$cells"> <xsl:variable name="p" as="xs:integer" select="position()"/> <xsl:variable name="fname" select="$field-names[$p]"/> <xsl:element name="{$field-names[$p]}"><xsl:value-of select="."/></xsl:element> </xsl:for-each> </timecard> </xsl:for-each> </element> </xsl:template> I get the eror that says Error on line 1 column 1 of inputfile.csv: SXXP0003 Error reported by XML parser: Content is not allowed in prolog. Transformation failed: Run-time errors were reported. I'd be happy for any suggestions. Thanks
Replies (1)
RE: Using XSLT to convert CSV into XML - Added by Anonymous about 16 years ago
Legacy ID: #5256119 Legacy Poster: Michael Kay (mhkay)
Looks as if you're supplying the CSV file as the primary input. You can't do that - the primary input must be XML. Best to run without a primary input, and read the CSV file using the doc() function. Define a named template and use that as the entry point, rather than a match="/" template. (use -it:main on the command line to start at <xsl:template name="main"/>). Please note, this is a general XSLT coding question, nothing specific to Saxon. Such questions are best asked on the xsl-list at mulberrytech.com
Please register to reply