Subject: Re: [xsl] transforming a XML to CSV From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx> Date: Thu, 15 Mar 2001 04:41:16 +0000 |
Hi Xiaocun, >> <!-- 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? If the element was missing then templates wouldn't be applied to it and you wouldn't get the (desired) comma. But if the element was always there, just with its *value* sometimes missing (as it was in Anand's sample) then you'd get an empty string value followed by a comma, which was what Anand was after. So the above template would match the SOURCE element in: <DATA> ... <SOURCE /> ... </DATA> and produce just a comma. > 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 :) Well, it would need to match attributes instead of elements, so it would need to be: <!-- by default give the value followed by a comma --> <xsl:template match="DATA/@*"> <xsl:value-of select="." />,<xsl:text /> </xsl:template> But more importantly, you'd need to *apply* templates to the attributes in the order that you wanted them output. Unlike elements, attributes haven't got an inherent order, so you'd need to do: <xsl:template match="DATA"> ... <xsl:apply-templates select="@DATA_ITEM" /> <xsl:apply-templates select="@SOURCE" /> <xsl:apply-templates select="@SINK" /> <xsl:apply-templates select="@RESOURCE" /> <xsl:apply-templates select="@OPR_DATE" /> <xsl:apply-templates select="@INTERVAL_NUM" /> <xsl:apply-templates select="@VALUE" /> </xsl:template> Note, though, that the advantage of picking the attributes out one by one like this is that I don't automatically apply templates to the NULL_FLAG attribute, so I don't need an empty template to filter out that value. I hope that helps, Jeni --- Jeni Tennison http://www.jenitennison.com/ XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
RE: [xsl] transforming a XML to CS, Xu, Xiaocun | Thread | RE: [xsl] transforming a XML to CS, Xu, Xiaocun |
RE: [xsl] Testing for Parent Nodes , Ciaran Byrne | Date | Re: [xsl] retrive data from #1 xml , Jeni Tennison |
Month |