Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Insert a CRLF when transforming XML into text

4,430 views
Skip to first unread message

Savvoulidis Iordanis

unread,
Jan 3, 2009, 7:40:00 PM1/3/09
to
I want to create a CSV (text) file out of a XML file, so it can be bulk
copied into SQLServer. How can I insert a CRLF char when I'm done converting
every record into its corresponding CSV?
The XML file I use, depicts master and detail records. In the CSV, I want to
have each of the master record and its detail records in one line, no matter
how many detail records there are for each master. Master fields are
separated by semicolon (;) and the details are separated by caret sign (^).
XML follows:

<Section> <!-- master 1-->
<BetStartDate>4/6/2009</BetStartDate>
<BetEndDate>4/8/2009</BetEndDate>
<Event> <!-- details -->
<Code>111</Code>
<Description>TEAM 1</Description>
<Apodosi>10,00</Apodosi>
</Event>
<Event>
<Code>112</Code>
<Description>TEAM 2 </Description>
<Apodosi>20,00</Apodosi>
</Event>
</Section>
<Section> <!-- master 2-->
<BetStartDate>12/12/2008</BetStartDate>
<BetEndDate>16/12/2008</BetEndDate>
<Event> <!-- details -->
<Code>211</Code>
<Description>TEAM 3</Description>
<Apodosi>5,00</Apodosi>
</Event>
<Event>
<Code>212</Code>
<Description>TEAM 4</Description>
<Apodosi>4,00</Apodosi>
</Event>
<Event>
<Code>213</Code>
<Description>TEAM 5</Description>
<Apodosi>7,00</Apodosi>
</Event>
</Section>

I want to get:
4/6/2009;4/8/2009^111^TEAM 1^10,00^112^TEAM 2^20,00
12/12/2008;16/12/2008^211^TEAM 3^5,00^212^TEAM 4^4,00^213^TEAM 5^7,00

I use the following template, which works, except it outputs only one line
that includes everything. I want to have a CRLF after 20,00 as displayed
above.

<xsl:template match="/">
<xsl:for-each select="Section">
<xsl:value-of select="BetStartDate" />;<xsl:value-of
select="BetEndDate"/><xsl:for-each select="Event">^<xsl:value-of
select="Code"/>^<xsl:value-of select="Description"/>^<xsl:value-of
select="Apodosi"/>
</xsl:for-each>
</xsl:for-each>
</xsl:template>

TIA

Neil Smith [MVP Digital Media]

unread,
Jan 3, 2009, 8:58:12 PM1/3/09
to
On Sat, 3 Jan 2009 16:40:00 -0800, Savvoulidis Iordanis
<Savvoulid...@discussions.microsoft.com> wrote:

>I want to create a CSV (text) file out of a XML file, so it can be bulk
>copied into SQLServer. How can I insert a CRLF char when I'm done converting
>every record into its corresponding CSV?

You need to add the character entity corresponding to a newline /
carriage return after each output group (inside your xsl:for-each) :

<xsl:text>&#10;&#13;</xsl:text>


Note the snippet you provided should probably have a line after
xsl:stylesheet and before xsl:template reading

<xsl:output method="text" .... />

HTH
Cheers - Neil
------------------------------------------------
Digital Media MVP : 2004-2009
http://mvp.support.microsoft.com/mvpfaqs

Savvoulidis Iordanis

unread,
Jan 5, 2009, 9:49:02 AM1/5/09
to
Thanks Neil.

Savvoulidis Iordanis

unread,
Jan 16, 2009, 9:57:02 AM1/16/09
to
Although the <xsl:text>&#13</xsl:text> goes to a new line, I noticed that
even when I just press Enter inside the XSLT file, the output text file has a
CRLF at the same position as the Enter I pressed.
And also, if I ident my XSL code using spaces on the left of my XSLT lines,
the output file is also carrying them too. I get spaces in front of every CSV
line produced. So, I'm forced to stuff all the code on the left which is ugly.
Is there anything I can do?

TIA

Neil Smith [MVP Digital Media]

unread,
Jan 16, 2009, 2:56:45 PM1/16/09
to

OK so far you haven't shown your stylesheet - the suggestion for
character entities was to clearly indicate where the intended CR/LF
should appear, rather than using the invisible <press enter> method.

Perhaps http://www.xml.com/pub/a/2001/12/05/whitespace.html and other
google searches on white space in XSL might be more useful - note how
they squash the space within the XSL stylesheet to get the desired
output.

Although it might not look readable to you, the machine will have no
problem processing that even if all the xsl: instructions are squashed
together into a single line.

0 new messages