Re: [xsl] XSL for CSV to XML

Subject: Re: [xsl] XSL for CSV to XML
From: "G. Ken Holman g.ken.holman@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 14 Feb 2019 13:07:14 -0000
You may find the code I used in these stylesheets to be helpful:

https://cranesoftwrights.github.io/resources/#csv

. . . . . Ken

At 2019-02-14 05:53 +0000, Rahul Singh rahulsinghindia15@xxxxxxxxx wrote:
Hi,

I wrote XSL for CSV to XML conversion but i am
not getting proper output for #Id 3. and Also I
need output as per given expected output. Need
yours help please for given Input CSV, XSL Code,
Current Output, Expected Output:

Input CSV:

<root>ID|Value|Number|descr1|descr2|descr3|descr4
1|AAAAA|1|aaaaaaaaaaa|aaaaaaaaaaa|aaaaaaaaaaa|aaaaaaaaaaa
2|AAAAA|2|aaaaaaaaaaa|aaaaaaaaaa|aaaaaaaa|aaaaaaaaaaaa
3|AAAAA|3|aaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa|aaaaaaaaa
aa|aaaaaaaaaaa|aaaaaaaaaaa</root>

XSL Code:


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="<http://www.w3.org/1999/XSL/Transform>http://www.w3.org/1999/XSL/
Transform"
xmlns:xs="<http://www.w3.org/2001/XMLSchema>http://www.w3.org/2001/XMLSchema";
exclude-result-prefixes="xs">
<xsl:output method="xml" indent="yes" encoding="UTF-8"/>
<xsl:template match="/">
<root>
<xsl:call-template name="texttorows">
<xsl:with-param name="StringToTransform" select="root"/>
</xsl:call-template>
</root>
</xsl:template>
<xsl:template name="texttorows">
<xsl:param name="StringToTransform" select="''"/>
<xsl:choose>
<xsl:when test="contains($StringToTransform,'&#xA;')">
<row>
<xsl:call-template name="csvtoxml">
<xsl:with-param name="StringToTransform"
select="substring-before($StringToTransform,'&#xA;')"/>
</xsl:call-template>
</row>
<xsl:call-template name="texttorows">
<xsl:with-param name="StringToTransform">
<xsl:value-of select="substring-after($StringToTransform,'&#xA;')"/>
</xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<row>
<xsl:call-template name="csvtoxml">
<xsl:with-param name="StringToTransform" select="$StringToTransform"/>
</xsl:call-template>
</row>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="csvtoxml">
<xsl:param name="StringToTransform" select="''"/>
<xsl:choose>
<xsl:when test="contains($StringToTransform,'|')">
<elem>
<xsl:value-of select="substring-before($StringToTransform,'|')"/>
</descr1>
<xsl:call-template name="csvtoxml">
<xsl:with-param name="StringToTransform">
<xsl:value-of select="substring-after($StringToTransform,'|')"/>
</xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<elem>
<xsl:value-of select="$StringToTransform"/>
</descr1>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>

CurrentB Output:

<?xml version="1.0" encoding="UTF-8"?>
<root>
B  B <row>
B  B  B  <elem>ID</descr1>
B  B  B  <elem>Value</descr1>
B  B  B  <elem>Number</descr1>
B  B  B  <elem>descr1</descr1>
B  B  B  <elem>descr2</descr1>
B  B  B  <elem>descr3</descr1>
B  B  B  <elem>descr4</descr1>
B  B </row>
B  B <row>
B  B  B  <elem>1</descr1>
B  B  B  <elem>AAAAA</descr1>
B  B  B  <elem>1</descr1>
B  B  B  <elem>aaaaaaaaaaa</descr1>
B  B  B  <elem>aaaaaaaaaaa</descr1>
B  B  B  <elem>aaaaaaaaaaa</descr1>
B  B  B  <elem>aaaaaaaaaaa</descr1>
B  B </row>
B  B <row>
B  B  B  <elem>2</descr1>
B  B  B  <elem>AAAAA</descr1>
B  B  B  <elem>2</descr1>
B  B  B  <elem>aaaaaaaaaaa</descr1>
B  B  B  <elem>aaaaaaaaaa</descr1>
B  B  B  <elem>aaaaaaaa</descr1>
B  B  B  <elem>aaaaaaaaaaaa</descr1>
B  B </row>
B  B <row>
B  B  B  <elem>3</descr1>
B  B  B  <elem>AAAAA</descr1>
B  B  B  <elem>3</descr1>
B  B  B  <elem>aaaaaaaaaaa</descr1>
B  B </row>
B  B <row>
B  B  B
<elem>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</de
scr1>
B  B  B  <elem>aaaaaaaaaaa</descr1>
B  B  B  <elem>aaaaaaaaaaa</descr1>
B  B  B  <elem>aaaaaaaaaaa</descr1>
B  B </row>
</root>

Expected Output:

<?xml version="1.0" encoding="UTF-8"?>
<root>
B  B <row>
B  B  B  <ID>1</ID>
B  B  B  <Value>AAAAA</Value>
B  B  B  <Number>1</Number>
B  B  B  <descr1>aaaaaaaaaaa</descr1>
B  B  B  <descr2>aaaaaaaaaaa</descr3>
B  B  B  <descr3>aaaaaaaaaaa</descr3>
B  B  B  <descr4>aaaaaaaaaaa</descr4>
B  B </row>
B  B <row>
B  B  B  <ID>2</ID>
B  B  B  <Value>AAAAA</Value>
B  B  B  <Number>2</Number>
B  B  B  <descr1>aaaaaaaaaaa</descr1>
B  B  B  <descr2>aaaaaaaaaa</descr2>
B  B  B  <descr3>aaaaaaaa</descr3>
B  B  B  <descr4>aaaaaaaaaaaa</descr4>
B  B </row>
B  B <row>
B  B  B  <Id>3</Id>
B  B  B  <Value>AAAAA</Value>
B  B  B  <Number>3</Number>
B  B  B
<descr1>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaa</descr1>
B  B  B  <descr2>aaaaaaaaaaa</descr2>
B  B  B  <descr3>aaaaaaaaaaa</descr3>
B  B  B  <descr4>aaaaaaaaaaa</descr4>
B  B </row>
</root>


Thanks, Rahul. <http://www.mulberrytech.com/xsl/xsl-list>XSL-List info and archive <http://lists.mulberrytech.com/unsub/xsl-list/96802>EasyUnsubscribe (<>by email)


--
Contact info, blog, articles, etc. http://www.CraneSoftwrights.com/s/ |
Check our site for free XML, XSLT, XSL-FO and UBL developer resources |
Streaming hands-on XSLT/XPath 2 training class @ US$45 (5 hours free) |

Current Thread