xsl-list
[Top] [All Lists]

Re: [xsl] XSLT for CSV to XML Conversion

2019-02-16 08:04:05
Hi Rahul,
    I'm willing to try helping solve this problem. Or perhaps, someone else
may solve it if you can clarify few details as per my questions below.
Please see my questions and comments inline below.

On Fri, Feb 15, 2019 at 10:45 PM Rahul Singh 
rahulsinghindia15(_at_)gmail(_dot_)com <
xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com> wrote:

*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|aaaaaaaaaaa|aaaaaaaaaaa|aaaaaaaaaaa</root>


This input doesn't have any , character and therefore it isn't CSV I
believe. It looks to me an XML document, with the top most tag being <root>
having a body of text content below it. Can we assume, your input XML as
following (introducing also a , character)

<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|aaaaaaaaaaa|aaaaaaaaaaa|aaaaaaaaaaa</root>

I've mentioned , characters delimiting rows. Fields of row have | as a
delimiting character.

Is my suggested input definitions ok according to you? Or, you've something
else in mind?


*XSL Code:*

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="
http://www.w3.org/1999/XSL/Transform"; xmlns:xs="
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>


I see something wrong here. The following doesn't appear to be correct

<elem>
  <xsl:value-of select="substring-before($StringToTransform,'|')"/>
</descr1>

<elem>...</descr1> is not well-formed.


<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>


The same well-formedness error here as well.


</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>

*Current Output:*

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <row>
      <elem>ID</descr1>
      <elem>Value</descr1>
      <elem>Number</descr1>
      <elem>descr1</descr1>
      <elem>descr2</descr1>
      <elem>descr3</descr1>
      <elem>descr4</descr1>
   </row>


I wonder, how can an XSLT processor generate above XML output. It isn't
well-formed (different <elem>..</descr1> are not well-formed).

I'm curious, which XSLT processor are you using?




-- 
Regards,
Mukul Gandhi
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--
<Prev in Thread] Current Thread [Next in Thread>