Hi i do have following xml code:
Then i need to convert those xml to csv format with header title is show.
<?xml version='1.0'?>
<portfolio>
<stock exchange="nasdaq">
<name>zaffymat inc</name>
<symbol>ZFFX</symbol>
<price>92.250</price>
</stock>
<stock exchange="nyse">
<name>zacx corp</name>
<symbol>ZCXM</symbol>
<price>28.875</price>
</stock>
<stock exchange="nasdaq">
<name>zysmergy inc</name>
<symbol>ZYSZ</symbol>
<price>20.313</price>
</stock>
</portfolio>
How can i transform xml above to csv like below output via xsl?
"Name";"Price"
"zaffymat inc";"92.250"
"zysmergy inc";"20.313"
Thank you.
Try this XSLT to get your desired output:
<xsl:template match="portfolio">
<xsl:text>"Name";"Price"</xsl:text>
<xsl:text> </xsl:text>
<xsl:for-each select="stock">
<xsl:text>"</xsl:text>
<xsl:value-of select="name"/>
<xsl:text>";</xsl:text>
<xsl:text>"</xsl:text>
<xsl:value-of select="price"/>
<xsl:text>"</xsl:text>
<xsl:text> </xsl:text>
</xsl:for-each>
</xsl:template>
This is my last script:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="portfolio">
<xsl:text>"Name"</xsl:text>
<xsl:text> </xsl:text>
<xsl:text>;</xsl:text>
<xsl:text> </xsl:text>
<xsl:text>"Price"</xsl:text><br/>
<xsl:for-each select="//stock[@exchange='nasdaq']">
<xsl:text>"</xsl:text>
<xsl:value-of select="name"/>
<xsl:text>"</xsl:text><xsl:text> </xsl:text>
<xsl:text>;</xsl:text>
<xsl:text> </xsl:text>
<xsl:text>"</xsl:text>
<xsl:value-of select="price"/>
<xsl:text>"</xsl:text><br/>
</xsl:for-each>
</xsl:template>
Its work fine to filter out the info and making new line for csv output! =)