0

I needed to use XSLT to generate CSV text output from XML

my XML

<Row> 
<cell>Currecy</cell>
<cell>RUR</cell>
<cell>USD</cell>
<cell>EURO</cell>
</Row> 
<Row> 
<cell>Param</cell>
<cell>17.2</cell>
<cell>12.12</cell>
<cell>100.2345</cell>
</Row> 
<Row> 
<cell>Param1</cell>
<cell>100</cell>
<cell>200</cell>
<cell>3556</cell>
</Row> 

output format CSV

Cur Param,  Param1
RUR, 17.2,  100     
USD, 12.12,  200
EURO, 100.2345, 3556

I do not know how to make a "transposition"

michael.hor257k
  • 113,275
  • 6
  • 33
  • 51

1 Answers1

3

If you had a valid XML input such as:

<Rows>
    <Row> 
        <cell>Currency</cell>
        <cell>RUR</cell>
        <cell>USD</cell>
        <cell>EURO</cell>
    </Row> 
    <Row> 
        <cell>Param</cell>
        <cell>17.2</cell>
        <cell>12.12</cell>
        <cell>100.2345</cell>
    </Row> 
    <Row> 
        <cell>Param1</cell>
        <cell>100</cell>
        <cell>200</cell>
        <cell>3556</cell>
    </Row> 
</Rows>

then you could use the following stylesheet:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>

<xsl:template match="/">
        <xsl:for-each select="Rows/Row[1]/cell">
            <xsl:variable name="i" select="position()" />
                <xsl:for-each select="/Rows/Row">
                    <xsl:value-of select="cell[$i]"/>
                    <xsl:if test="position()!=last()">
                        <xsl:text>,</xsl:text>
                    </xsl:if>
                </xsl:for-each>
            <xsl:if test="position()!=last()">
                <xsl:text>&#10;</xsl:text>
            </xsl:if>
        </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

to obtain:

Currency,Param,Param1
RUR,17.2,100
USD,12.12,200
EURO,100.2345,3556
michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
  • 1
    It only has to be well-formed, not necessarily valid. – Ian Roberts May 30 '14 at 11:28
  • 1
    @IanRoberts You are right, but I prefer using "valid" (as a non-technical term) since "not well-formed" does not carry the same sense of gravity as "invalid" and may not seem critical enough to be taken seriously by a non-expert poster. – michael.hor257k May 30 '14 at 11:45
  • 1
    IMO part of the job of Stack Overflow is to help non-experts _become_ experts, and we undermine that if we misuse terminology that does have a precisely defined meaning with respect to the technology being discussed (for the non-experts reading this, an XML document can only be "valid" if the document references a DTD and complies with the constraints that it specifies, or "schema valid" if it complies with an XML Schema. The word "valid" has no meaning in the absence of a DTD or schema). If you don't want to use the accurate term "well-formed" then how about "syntactically correct"? – Ian Roberts May 30 '14 at 12:22
  • @IanRoberts You make a strong point, but... "valid" is still a valid dictionary word and neither "ill-formed", "malformed" nor "syntactically correct" carry the same punch as "invalid". If you have any alternatives that contain "bomb", "death" or "doom", I might be persuaded. ;-) – michael.hor257k May 30 '14 at 12:54
  • "If you had input that was actually XML" (since if it isn't well-formed then it isn't XML at all, however much it might look like it) – Ian Roberts May 30 '14 at 12:59
  • @IanRoberts I'll think about it (no promises). I assure you I did think about this before and chose to use the (technically incorrect) terms "valid" and "invalid" deliberately for the reasons stated above. – michael.hor257k May 30 '14 at 13:04
  • BTW, W3C themselves have a *Validation Service* that will check, among other things, the well-formedness of an XML document. – michael.hor257k May 30 '14 at 13:17