RE: [xsl] transforming a XML to CSV

Subject: RE: [xsl] transforming a XML to CSV
From: "Xu, Xiaocun" <XXu@xxxxxxxxxxxx>
Date: Wed, 14 Mar 2001 14:37:30 -0500
Ooops, I didn't meant to say "would the values in the resultant CSV be
screwed", I meant to say "skew".  Sorry :*(

Xiaocun Xu
Emptoris, Inc.
xxu@xxxxxxxxxxxx


-----Original Message-----
From: Xu, Xiaocun [mailto:XXu@xxxxxxxxxxxx]
Sent: Wednesday, March 14, 2001 2:28 PM
To: 'xsl-list@xxxxxxxxxxxxxxxxxxxxxx'
Subject: RE: [xsl] transforming a XML to CSV


Hi, Jenni:

> <!-- by default give the value followed by a comma -->
> <xsl:template match="DATA/*">
>   <xsl:value-of select="." />,<xsl:text />
> </xsl:template>

Just a quick question regarding the code above:
1. Would the above code work if some of the children element of DATA is
implied (not required)?  If some of the children element is missing in a
particular DATA instance, would the values in the resultant CSV be screwed?

2. How would the above code change if the values are stored as attributes
rather than elements?  e.g. <DATA DATA_ITEM="SPIN_PRICE" SOURCE="" SINK=""
RESOURCE="ZP2" OPR_DATE="20000512" INTERVAL_NUM="24" NULL_FLAG="F"
VALUE="0.25"/>.  Just curious :)

Thanks,

Xiaocun Xu
Emptoris, Inc.
xxu@xxxxxxxxxxxx


-----Original Message-----
From: Jeni Tennison [mailto:mail@xxxxxxxxxxxxxxxx]
Sent: Wednesday, March 14, 2001 1:40 PM
To: Awasthi, Anand
Cc: 'xsl-list@xxxxxxxxxxxxxxxxxxxxxx'
Subject: Re: [xsl] transforming a XML to CSV


Hi Anand,

> the kind of output which i am trying to achieve is something like :
> ( i want HEADER ELEMENT to repeat  with all DATA_ITEM ELELMENTS )
>
> Venkatesh,1,US$,SPIN_PRICE,,,ZP26,20000512,24,F,0.25
> Venkatesh,1,US$,SPIN_PRICE,,,ZP26,20000512,25,T
> Parigi,2,MW,SPIN_PRICE,,,ZP26,20000512,24,0.25
> Parigi,2,MW,SPIN_PRICE,,,ZP26,20000512,25,T

The easiest way to do this is to apply templates to only the DATA
elements within a template that

<xsl:template match="REPORT_ITEM">
   <xsl:apply-templates select="DATA" />
</xsl:template>

Within the template for the DATA element, apply templates to the
relevant HEADER element (which you can get to by going up to the DATA
element's parent and then down again to the child HEADER element).
Then give the data that you want for the particular DATA element:

<xsl:template match="DATA">
   <xsl:apply-templates select="../HEADER" />
   <xsl:apply-templates />
</xsl:template>

<!-- by default give the value followed by a comma -->
<xsl:template match="DATA/*">
   <xsl:value-of select="." />,<xsl:text />
</xsl:template>

<!-- don't output anything for the NULL_FLAG element -->
<xsl:template match="DATA/NULL_FLAG" priority="1" />

<!-- output the VALUE element if NULL_FLAG isn't 'T' -->
<xsl:template match="DATA/VALUE" priority="1">
   <xsl:choose>
      <xsl:when test="../NULL_FLAG = 'T'">T</xsl:when>
      <xsl:otherwise><xsl:value-of select="." /></xsl:otherwise>
   </xsl:choose>
</xsl:template>

Within the template for the HEADER element, output whatever you want
to add from each HEADER. You can do this by applying templates to its
content, as I've done for the content of the DATA element above, or
(as you only want to know a bit of the contained data) it's probably
simpler to just list them. You can get the number for the HEADER
element using xsl:number:

<xsl:template match="HEADER">
   <xsl:value-of select="REPORT" />,<xsl:text />
   <xsl:number level="any" />,<xsl:text />
   <xsl:value-of select="UOM" />,<xsl:text />
</xsl:template>

I hope that helps,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread