XSLT output incompatible with standard  
Author Message
Sek360





PostPosted: XML and the .NET Framework, XSLT output incompatible with standard Top

Hi Folks,

I am trying to convert XML to CSV format output.

I was able to convert it by using the following option:

output method="text"

But, the output doesn't seem to adhere to the standard.

I was referrring to the following link to refer to the CSV format:

http://www.hide-link.com/

Please correct me if i am wrong.

TIA

Sek



.NET Development30  
 
 
Appel





PostPosted: XML and the .NET Framework, XSLT output incompatible with standard Top

If you showed us your XSLT document, what your input looks like, what you expected to happen and what happened it would be easier to tell you what's wrong with it.

Setting the output method to text will just cause your XSLT to output text, not text in the CSV format.


 
 
Sek360





PostPosted: XML and the .NET Framework, XSLT output incompatible with standard Top

Appel,

Following is the input xml:

< xml version="1.0" encoding="utf-f" >

<emailList>

<person> <name>Travis Timmons</name> <email>ac, abs, moon</email> </person>

<person> <name>Venture "Extended Edition"</name> <email></email> </person>

<person> <name>Grand Cherokee</name> <email>MUST SELL!

air, moon roof, loaded</email> </person>

</emailList>

Following is the CSV output of above XML by Excel: (This is my expected output)

Travis Timmons,"ac, abs, moon"
"Venture ""Extended Edition""",
Grand Cherokee,"MUST SELL!
air, moon roof, loaded"

Following is the output by XSLT conversion:

Travis Timmons,ac, abs, moon
Venture "Extended Edition",
Grand Cherokee,MUST SELL!
air, moon roof, loaded

Following is my XSL:

< xml version="1.0" encoding="utf-8" >

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

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

<xsl:template match="/emailList/person">

<xsl:value-of select="name" />,<xsl:value-of select="email"/>

<xsl:text>&#x0D;&#x0A;</xsl:text>

</xsl:template>

</xsl:stylesheet>

Based on the above data, i am expecting the output as generated by Excel. Basically, i was referring to the wiki link to check the adherence of my output to the standard.

I would love to know what i am missing here.

TIA

Sek


 
 
-Anton Lapounov





PostPosted: XML and the .NET Framework, XSLT output incompatible with standard Top

As Appel correctly noticed, the text output method does not assume the CSV format. Here you can find a sample how to convert XML to CSV using XSLT:

http://lists.xml.org/archives/xml-dev/200404/msg00216.html

This sample, however, does not quote strings that must be quoted according to CSV format rules. It should not be difficult to fix this issue.

Best regards,
Anton


 
 
-Anton Lapounov





PostPosted: XML and the .NET Framework, XSLT output incompatible with standard Top

You have to implement CSV quoting rules in your stylesheet. For example:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="text"/>

<!-- Taken from XSLT Cookbook, chapter 1.7 (http://www.oreilly.com/catalog/xsltckbk/) -->

<xsl:template name="search-and-replace">

<xsl:param name="input"/>

<xsl:param name="search-string"/>

<xsl:param name="replace-string"/>

<xsl:choose>

<!-- See if the input contains the search string -->

<xsl:when test="$search-string and

contains($input,$search-string)">

<!-- If so, then concatenate the substring before the search

string to the replacement string and to the result of

recursively applying this template to the remaining substring.

-->

<xsl:value-of

select="substring-before($input,$search-string)"/>

<xsl:value-of select="$replace-string"/>

<xsl:call-template name="search-and-replace">

<xsl:with-param name="input"

select="substring-after($input,$search-string)"/>

<xsl:with-param name="search-string"

select="$search-string"/>

<xsl:with-param name="replace-string"

select="$replace-string"/>

</xsl:call-template>

</xsl:when>

<xsl:otherwise>

<!-- There are no more occurences of the search string so

just return the current input string -->

<xsl:value-of select="$input"/>

</xsl:otherwise>

</xsl:choose>

</xsl:template>

<!-- Quote the string according to CSV rules (http://en.wikipedia.org/wiki/Comma-separated_values) -->

<xsl:template name="csv-quote">

<xsl:param name="value"/>

<xsl:choose>

<!-- To be on the safe side, append '&#x9; ' (tabulation and space)

to the list of special characters below // Anton Lapounov -->

<xsl:when test="translate($value, '&#xa;&#xd;&quot;,', '') = $value">

<!-- No quoting needed, just return the string -->

<xsl:value-of select="$value"/>

</xsl:when>

<xsl:otherwise>

<!-- Enclose in quotes, doubling all quotes encountered in the string -->

<xsl:text>&quot;</xsl:text>

<xsl:call-template name="search-and-replace">

<xsl:with-param name="input" select="$value"/>

<xsl:with-param name="search-string" select="'&quot;'"/>

<xsl:with-param name="replace-string" select="'&quot;&quot;'"/>

</xsl:call-template>

<xsl:text>&quot;</xsl:text>

</xsl:otherwise>

</xsl:choose>

</xsl:template>

<xsl:template match="/emailList/person">

<xsl:call-template name="csv-quote">

<xsl:with-param name="value" select="name"/>

</xsl:call-template>

<xsl:text>,</xsl:text>

<xsl:call-template name="csv-quote">

<xsl:with-param name="value" select="email"/>

</xsl:call-template>

<xsl:text>&#xa;</xsl:text>

</xsl:template>

</xsl:stylesheet>

Best regards,
Anton


 
 
Sek360





PostPosted: XML and the .NET Framework, XSLT output incompatible with standard Top

Anton,

that was quite self-explanatory

i was spending time understanding every bit of your work!

kudos for taking time to post.

-Sek