Jump to content

XML content in to CSV file


mysite
 Share

Recommended Posts

Hi all,I want to collect some data(not all) from an xml file and write them into a csv file. I used a xsl file formatting for that. Here is my xml file content.

<?xml version="1.0" encoding="ISO-8859-1"?><MAIN><CATALOG>	<PLANT Tag="no">	<LIGHT>Time may differ</LIGHT>		<PRICE>$2.44</PRICE>	</PLANT>	<PLANT Tag="no">	<LIGHT>Fixed time</LIGHT>		<PRICE>$9.37</PRICE>	</PLANT></CATALOG><CHAT>	<SOME>Some Text</SOME>	<MORE>More Text</MORE>	</CHAT></MAIN>

I want to get data on CATALOG element. So here is my style sheet.

<?xml version="1.0"?><xsl:stylesheet version = "1.0"	xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">  <xsl:output method="text"/>  <!--<xsl:strip-space elements = "CATALOG"/>-->  <xsl:template match="/MAIN/CATALOG/PLANT">	<!--<xsl:text/><xsl:value-of select="normalize-space(PLANT)"/>"<xsl:text/>-->	<xsl:value-of select="normalize-space(LIGHT)"/>";"<xsl:text/>	<xsl:value-of select="normalize-space(PRICE)"/>"<xsl:text/>	<xsl:text disable-output-escaping = "yes" ></xsl:text>  </xsl:template></xsl:stylesheet>

But what happened here is, data on CHAT element also added to the file. I only want data on <LIGHT> and <PRICE> elements. But my style sheet gives data on <SOME> and <MORE> elements. How can I correct this.Please help me too fix this. I'm stuck with this for two days.Thanks.

Link to comment
Share on other sites

You might want to force your PLANT template to be the only one active. Right now, you're applying XSLT's default temlates, which basically copy every non-matched text node, starting from the root.To force your template as a starting one, try to add this in your stylesheet:

<xsl:template match="/"><xsl:apply-templates select="MAIN/CATALOG/PLANT" /></xsl:template>

Link to comment
Share on other sites

Thanks for the replay sir. I'm waiting for a clue for two days here. :)You mean that replace the existing with your code?

<xsl:template match="/MAIN/CATALOG/PLANT">

But there is an issue too, say I've added additional element inside the PLANT. When I execute the new xslt sheet all information are printed into the CSV file, I mean elements which I don't refer here also added to the CSV file. Can you tell me why is that?At the time I'm using separate xml file for this, which is equal to the format I want. But I want to do all of them using a single xml file.And also there is another question. How can I define the font type in the above style sheet. I want to print all text in Arial format into the CSV file? Can you tell me how can I do it?Thanks a lot

Link to comment
Share on other sites

CSV files are (as far as I know) plain text, and have nothing to do with appearance. Having said that, it's impossible to set a font in any way within the CSV file. You can only set a font upon trying to display data in whatever environment. For example, if you're displaying the CSV text inside an HTML page, use CSS to define the font on the HTML page, and thus the CSV text.As for the template, as I said, add it into your stylesheet (I specifically made sure I used the word "add" to avoid confusion). In other words, your complete stylesheet would look:

<?xml version="1.0"?><xsl:stylesheet version = "1.0"	xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">  <xsl:output method="text"/>  <!--<xsl:strip-space elements = "CATALOG"/>-->  <xsl:template match="/MAIN/CATALOG/PLANT">	<!--<xsl:text/><xsl:value-of select="normalize-space(PLANT)"/>"<xsl:text/>-->	<xsl:value-of select="normalize-space(LIGHT)"/>";"<xsl:text/>	<xsl:value-of select="normalize-space(PRICE)"/>" <xsl:text/>	<xsl:text disable-output-escaping = "yes" ></xsl:text>  </xsl:template><xsl:template match="/"><xsl:apply-templates select="MAIN/CATALOG/PLANT" /></xsl:template></xsl:stylesheet>

Link to comment
Share on other sites

Thanks for the replay sir. :) I have another small question sir. If I want to do the same thing for a note pad, I mean writing data to a note pad, how it's possible.Thanks again for your comments.

Link to comment
Share on other sites

Thanks for the replay sir. :) I have another small question sir. If I want to do the same thing for a note pad, I mean writing data to a note pad, how it's possible.Thanks again for your comments.
What do you mean by that? Save it to a .txt file? XSLT can't do that alone. You need to instruct the XSLT processor to save the XSLT output to a file. How you do that depends on the XSLT processor you use and the API by which you call it.For example, in PHP with libxslt, you use XSLTProcessor::transformToURI().If you're running XSLT directly in the browser, you can't save the output to a file. It's a basic security measure that JavaScript and other client side languages can't save files on the client computer.
Link to comment
Share on other sites

What do you mean by that? Save it to a .txt file? XSLT can't do that alone. You need to instruct the XSLT processor to save the XSLT output to a file. How you do that depends on the XSLT processor you use and the API by which you call it.
Ok, I'll explain more about this. I have a xml file with some data. I want to create some file formats using XSLT with C#. It's possible in C#. But the thing is I don't know about xslt. This is the first time I work on it.First target is converting xml into CSV, which is discuss so far here in this thread. It's fine.Like that I've try to work on for Notepad. I mean a Text file. I'm working on it now. :) But still no luck.Anyway, I want to learn few basis about XSLT. But I don't like to buy a new book and study. So, if you have any link to learn XSLT from the beginning please send to me my friend. I've search on the Google and found some links. But all are start at once on larger scales in XSLT. Thanks.
Link to comment
Share on other sites

In addition to the W3Schools tutorial, look at the last question from the XSLT FAQ for information on XSLT.I still don't understand what you mean by "try to work on for Notepad. I mean a Text file".What XSLT does is to take an XML as input and produce something as output (whether it's another XML document, or as seen here - a plain text document). That's all XSLT does. Everything else depends on the environment (C# in your case). If you want to save the output from XSLT to a file, you need to do it in C#. If you want to do something further with the output, you need to do it in C#.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...