Converting Pipe-Delimited txt to XML

Questions about XML that are not covered by the other forums should go here.
diblassio4
Posts: 2
Joined: Wed Mar 29, 2006 10:11 pm

Converting Pipe-Delimited txt to XML

Post by diblassio4 »

Hi George, (and everyone else!)

I checked out http://www.oxygenxml.com/forum/ftopic977.html and
I was trying to use the nice example you posted, but using <xsl:analyze-string select="$value" regex="\|"> for pipe-delimited instead.
The problem comes when there is an empty field like such:
field|secondfield|||fifthField (fields 3 and 4 are empty)
field||thirdfield||fifthField (fields 2 and 4 are empty)

then the positions get janked out of consistency in the results...

Any ideas?

Thanks in advance!
Radu
Posts: 9018
Joined: Fri Jul 09, 2004 5:18 pm

Post by Radu »

Hi,

Sorry for the delay.
If you check the annotation of the "xsl:analyse" tag available in the Model View it says something like:

Code: Select all

...Each substring will contain at least one character.
so this means you will not be notified in the "xsl:non-matching-substring" of empty strings.

I found an interesting XSLT 2.0 method called "tokenize" that seems to do exactly the same thing http://www.w3.org/TR/xpath-functions/#func-tokenize but also return the 0-length matches between the separators.

Sample usage:

Code: Select all

        <xsl:variable name="var"
select="tokenize('field|secondfield|||fifthField', '\|')"/>
<xsl:for-each select="$var">
<entry>
<xsl:value-of select="."/>
</entry>
</xsl:for-each>
The code above will also output empty "entry" tags when there is no content between separators.

Hope this helps,
Regards, Radu.
ravi
Posts: 4
Joined: Thu Sep 20, 2007 1:35 pm

Post by ravi »

Hi Radhu,

am new to this site and as well as am new to the XSLT, i need a help regarding converting CSV file to XML file

here am getting the CSV file from the MQ Queue, the message looks like this

<payload>abc,111,india</payload>


i would like to conver this MQ message into the XML message using the XSLT

expected out should like this :

<?xml version="1.0" encoding="UTF-8"?>
<empdetails><empname>abc</empname><empid>111</empid><empcountry>india<empcountry></empdetails>

it would be a gr8 if you could provide me the XSLT for this,

thanks in advance
jkmyoung
Posts: 89
Joined: Mon Mar 06, 2006 10:13 pm

Post by jkmyoung »

<xsl:variable name="var" select="tokenize('payload', ',|')"/>
jkmyoung
Posts: 89
Joined: Mon Mar 06, 2006 10:13 pm

Post by jkmyoung »

Cursed no edit forum.

<xsl:variable name="var" select="tokenize('payload', ',')"/>
ravi
Posts: 4
Joined: Thu Sep 20, 2007 1:35 pm

Post by ravi »

jkmyoung wrote:Cursed no edit forum.

<xsl:variable name="var" select="tokenize('payload', ',')"/>
am totally new to XSL, if you could provide me the XSL it's great, and also please remeber i need the each coloum in seperate tag

like if the input record is like this : ajay,111,india,1000

then am expecting the ajay in <empname>ajay</empname> tag, <empid>111</empid><empcountry>india</empcountry><empsal>1000</empsal>

am expecting the output in the a bove format

thanks in advance
Post Reply