简体   繁体   中英

how to download xslt file to .xls using javascript

i am working on export data to .xls and after googling i found that this can be done using xml to xslt and then to excel. But i stuck with how to download my xslt file to .xls file in javascript.

anyone with the solution how to do this.

thanks in advance

I don't think you will need to have JavaScript deal with the XSLT transform at all. XSLT capable browsers should apply the transformation for you.

See W3schools page on xsl transformation for a more detailed example.

The general idea is this: Given a raw XML document, one writes an XSL Style Sheet to define a transformation template. This defines for the various elements in your XML file, what they should be transformed into. The example from the W3schools article transforms XML to XHTML.

For every node or type of node ( For-each is a keyword to note here) in the XML, a (XHTML) code scrap is defined, which will replace such nodes in the translated output.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
   <html>
       <body>
           <h2>My CD Collection</h2>
           <table border="1">
               <tr bgcolor="#9acd32">
               <th>Title</th>
               <th>Artist</th>
               </tr>
               <xsl:for-each select="catalog/cd">
                   <tr>
                   <td><xsl:value-of select="title"/></td>
                   <td><xsl:value-of select="artist"/></td>
                   </tr>
               </xsl:for-each>
           </table>
       </body>
    </html>
</xsl:template>
</xsl:stylesheet>

What happens here, is that the root is replaced with <html> and <body> tags. Other nodes are replaced with table entries, etc.

Your transform depends on the layout of your XML file, and in your case should adhere to proper structuring of XLS files.

Finally, include in your XML document a reference to the XSL file.

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>
<catalog>
...
</catalog>

If you have an XSLT compliant browser it will nicely transform your XML into the desired format.

Some discussion elsewhere on stackoverflow.com, which includes examples on how to transform an XML file to proper XLS format.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM