This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

RE: Converting XML source to CSV output


To output the header information once for each detail line, you need to
output it while processing the detail element, not while processing the
header.

You could achieve this by inverting the usual structure. In your root
template do

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

and in your detail template do

<xsl:template match="DETAIL">
  <xsl:apply-templates select="ancestor::HEADER"/>
</xsl:template>

Then the HEADER template will be expanded once for each DETAIL element.

Your unwanted white space probably arises because of the
<xsl:apply-templates/> in the DETAIL template. For some reason you didn't
include DETAIL in the list of elements in <xsl:strip-space>, so its children
include whitespace nodes which this apply-templates instruction is
processing.

Mike Kay

> -----Original Message-----
> From: owner-xsl-list@lists.mulberrytech.com
> [mailto:owner-xsl-list@lists.mulberrytech.com]On Behalf Of
> Chris Pearson
> Sent: 19 September 2001 19:41
> To: XSL-List@lists.mulberrytech.com
> Subject: [xsl] Converting XML source to CSV output
>
>
> Hi all.  A novice question that I can't seem to locate a solution for
> ...  I have an XML source that I need to convert to a text delimited
> file.
>
> The xml source is:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <DATA>
>    <HEADER>
>       <RECORD_A value="Header1 Record A" />
>       <RECORD_B value="Header1 Record B" />
>       <RECORD_n value="Header1 Record n" />
>    </HEADER>
>    <BODY>
>       <BODY_RECORD_A value="Body1 Record A" />
>       <BODY_RECORD_B value="Body1 Record B" />
>       <BODY_RECORD_n value="Body1 Record n" />
>       <DETAIL>
>          <DET_RECORD_A value="Detail1 Record A" />
>          <DET_RECORD_B value="Detail1 Record B" />
>          <DET_RECORD_n value="Detail1 Record n" />
>       </DETAIL>
>       <DETAIL>
>          <DET_RECORD_A value="Detail2 Record A" />
>          <DET_RECORD_B value="Detail2 Record B" />
>          <DET_RECORD_n value="Detail2 Record n" />
>       </DETAIL>
>    </BODY>
>    <BODY>
>       <BODY_RECORD_A value="Body2 Record A" />
>       <BODY_RECORD_B value="Body2 Record B" />
>       <BODY_RECORD_n value="Body2 Record n" />
>       <DETAIL>
>          <DET_RECORD_A value="Detail3 Record A" />
>          <DET_RECORD_B value="Detail3 Record B" />
>          <DET_RECORD_n value="Detail3 Record n" />
>       </DETAIL>
>       <DETAIL>
>          <DET_RECORD_A value="Detail4 Record A" />
>          <DET_RECORD_B value="Detail4 Record B" />
>          <DET_RECORD_n value="Detail4 Record n" />
>       </DETAIL>
>    </BODY>
> </DATA>
>
> +++++++++++++++++++++++++++++++++++++++++++
>
> My stylesheet is:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet version="1.0"
>    xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
>
> <xsl:output method="text" media-type="text/plain" />
> <xsl:strip-space elements="DATA HEADER BODY" />
>
> <xsl:template match="DATA">
>    <xsl:apply-templates />
> </xsl:template>
>
> <xsl:template match="HEADER">
>    <xsl:variable name="rec1" select="RECORD_A/@value" />
>    <xsl:variable name="rec2" select="RECORD_B/@value" />
>    <xsl:variable name="recn" select="RECORD_n/@value" />
>    <xsl:variable name="Head_Line"
>       select="concat ($rec1, ',', $rec2, ',', $recn, ',')" />
>    <xsl:value-of select="$Head_Line" />
>    <xsl:apply-templates />
> </xsl:template>
>
> <xsl:template match="BODY">
>    <xsl:variable name="bodya" select="BODY_RECORD_A/@value" />
>    <xsl:variable name="bodyb" select="BODY_RECORD_B/@value" />
>    <xsl:variable name="bodyn" select="BODY_RECORD_n/@value" />
>    <xsl:variable name="Body_Line"
>       select="concat ($bodya, ',', $bodyb, ',', $bodyn, ',')" />
>    <xsl:value-of select="$Body_Line" />
>    <xsl:apply-templates />
> </xsl:template>
>
> <xsl:template match="DETAIL">
>    <xsl:variable name="deta" select="DET_RECORD_A/@value" />
>    <xsl:variable name="detb" select="DET_RECORD_B/@value" />
>    <xsl:variable name="detn" select="DET_RECORD_n/@value" />
>    <xsl:variable name="Detail_Line"
>       select="concat ($deta, ',', $detb, ',', $detn)" />
>    <xsl:value-of select="$Detail_Line" />
>    <xsl:apply-templates />
> </xsl:template>
>
> </xsl:stylesheet>
>
> ++++++++++++++++++++++++++++++++++++++
>
> And, my output is:
>
> Header1 Record A,Header1 Record B,Header1 Record n,Body1
> Record A,Body1
> Record B,Body1 Record n,Detail1 Record A,Detail1 Record
> B,Detail1 Record
> n
>
>
>
>       Detail2 Record A,Detail2 Record B,Detail2 Record n
>
>
>
>       Body2 Record A,Body2 Record B,Body2 Record n,Detail3 Record
> A,Detail3 Record B,Detail3 Record n
>
>
>
>       Detail4 Record A,Detail4 Record B,Detail4 Record n
>
> ++++++++++++++++++++++++++++++++++++++
>
> What I want is:
>
> Header1 Record A,Header1 Record B,Header1 Record n,Body1
> Record A,Body1
> Record B,Body1 Record n,Detail1 Record A,Detail1 Record
> B,Detail1 Record
> n
> Header1 Record A,Header1 Record B,Header1 Record n,Body1
> Record A,Body1
> Record B,Body1 Record n,Detail2 Record A,Detail2 Record
> B,Detail2 Record
> n
> Header1 Record A,Header1 Record B,Header1 Record n,Body2
> Record A,Body2
> Record B,Body2 Record n,Detail3 Record A,Detail3 Record
> B,Detail3 Record
> n
> Header1 Record A,Header1 Record B,Header1 Record n,Body2
> Record A,Body2
> Record B,Body2 Record n,Detail4 Record A,Detail4 Record
> B,Detail4 Record
> n
>
> I'm having 2 issues:
>
> 1) How to repeat the header (and Body data) for detail records
> 2) How to format it without extra white space
>
> Is my approach flawed?  Any and all suggestions, comments,
> criticisms or
> random postulations welcomed.  Thanks in advance!
>
> Chris
>
>
>
>
>
>
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
>


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]