1

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.

0

2 Answers 2

2

Try this XSLT to get your desired output:

  <xsl:template match="portfolio">
    <xsl:text>"Name";"Price"</xsl:text>
    <xsl:text>&#10;</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>&#10;</xsl:text>
    </xsl:for-each>
  </xsl:template>
2
  • Thanxs Navin.Its work .I add <xsl:for-each select="//stock[@exchange='nasdaq']"> to filter the output.I also add <br/>&#10; to make a new line. =) Jun 27, 2013 at 4:13
  • For those who may search it: &#10; is \n stackoverflow.com/questions/3025265/…
    – pdem
    Sep 25, 2019 at 13:48
0

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>&#10;</xsl:text>
<xsl:text>;</xsl:text>
<xsl:text>&#10;</xsl:text>
<xsl:text>"Price"</xsl:text><br/>&#10;

<xsl:for-each select="//stock[@exchange='nasdaq']">

<xsl:text>"</xsl:text>
<xsl:value-of select="name"/>
<xsl:text>"</xsl:text><xsl:text>&#10;</xsl:text>

<xsl:text>;</xsl:text>
<xsl:text>&#10;</xsl:text>

<xsl:text>"</xsl:text>
<xsl:value-of select="price"/>
<xsl:text>"</xsl:text><br/>&#10;

</xsl:for-each>
</xsl:template>

Its work fine to filter out the info and making new line for csv output! =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.