Hello,
I need to be able to get the information from a table on my form and export that information to a csv file.
I saw that Webmaker has a transform action when constructing rules, which will allow me to take the XML block that I use to populate the table and transform it using a stylesheet that I created to transform it into a csv format. Looking at the rule, it also allows me to write that data to a file.
I have a few questions about this:
1. In the 'from location' box, what part of the xml do I need to put in?
Can I just enter the portion that needs to be transformed?
2. In the 'to document' box, the default value is 'outputFile.xml'.
a. Can I change this to a csv file format?
b. Does this file need to already exist? If so, where do I need to add this file? If not, where do I get this file in Webmaker?
3. After the document has been created, how can the user download the file?
I need to be able to get the information from a table on my form and export that information to a csv file.
I saw that Webmaker has a transform action when constructing rules, which will allow me to take the XML block that I use to populate the table and transform it using a stylesheet that I created to transform it into a csv format. Looking at the rule, it also allows me to write that data to a file.
I have a few questions about this:
1. In the 'from location' box, what part of the xml do I need to put in?
Can I just enter the portion that needs to be transformed?
2. In the 'to document' box, the default value is 'outputFile.xml'.
a. Can I change this to a csv file format?
b. Does this file need to already exist? If so, where do I need to add this file? If not, where do I get this file in Webmaker?
3. After the document has been created, how can the user download the file?
RE: Constructing Rules - Transform
However, I'm running into an issue.
In my xsl, I specify that a new line needs to be added after a row.
However, no matter what I put, the new line is not being added to the csv file that's being generated.
RE: Constructing Rules - Transform
I'm glad you are making progress.
Yes getting new lines out from the XSL can sometimes be a bit fiddly.
Firstly I would make sure that you have the output method at the top set to text.
Then there are a couple of options I have used before. Firstly you could put your line break within an <xsl:text> tag so that it knows that this is important whitespace. Eg
<xsl :s tylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="text"/> <xsl:template match="/"> <xsl:for-each select="//*"> <xsl:value-of select="firstValue"/> <xsl:text>,</xsl:text> <xsl:value-of select="secondValue"/> ... <xsl:text> </xsl:text> </xsl:for-each> </xsl:template> </xsl :s tylesheet>
Alternatively you can define a variable with the line break value, and then whenever you want to output the new line just use an xsl:value-of statement to output the variable. Eg
<xsl :s tylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="text"/> <xsl:variable name="lineBreak" select="'
'"/> <xsl:template match="/"> <xsl:for-each select="//*"> <xsl:value-of select="firstValue"/> <xsl:text>,</xsl:text> <xsl:value-of select="secondValue"/> ... <xsl:value-of select="$lineBreak"/> </xsl:for-each> </xsl:template> </xsl :s tylesheet>
Hopefully these examples help.
With regards to getting the CSV file downloaded to the user there are again a few different options. You could save the files out temporarily to a directory that is web accessible, and then give the user the URL to this file. You would then need a clean up process of some sort to remove these files at a later point.
Alternatively (and perhaps a better approach) would be to place the CSV details back into your factbase data, and then use a simple custom XGate plugin to return the CSV content to the user directly instead of the normal HTML response. You could have a look at the details under step 3 in this post (http://www.hyfinity.com/node/263 for an example of a simple plugin. In this case it is returning a json structure, but the same principles would apply for CSV.
I hope this is useful.
Please let me know if you have any further questions.
Regards,
Gerard
RE: Constructing Rules - Transform
I tried both solutions you provided (for the new line), but the first solution did not work.
The second solution did not give a new line, but it provided a space instead.
Is there something wrong with how I'm calling this method?
I have this transform method in my controller and after it runs, I just open the file in notepad.
RE: Constructing Rules - Transform
That is a shame that these options didn+?+?+?t work for you.
Can I ask how you are editing the XSL file? Are you opening up the file directly in an external editor, or are you using the browser based edit option in WebMaker? I guess it might be possible that the new lines are getting lost during the save process of the XSL, so it is probably worth checking the XSL file on the file system directly if you are not already.
Also, it might be worth opening the created CSV file in another editor instead of notepad. The
code used in the second approach actually represents a line feed, which I think might not be correctly displayed by notepad. It might be that if you open up this CSV file in another editor (or Wordpad) that the new lines are correctly displayed. If you do need it to display in notepad, then you may need to use the carriage return line feed combination, which I think is '
'
I hope this helps.
Regards,
Gerard