How to include all file contents in <body> using XSLT?

I want to have a separate file that contains some javascript functions. When XSLT processes my application, I want it to output all the contents of this file in HTML. I do not want to reference the library, but instead I have all the functions inside my html.

I know <xsl:include> , but I cannot include anything inside or <body> tags.

Is it possible?

+4
source share
2 answers

It is good if your file (e.g. scripts.xml ) should be included, e.g. XML. has content like

 <script type="text/javascript"> function foo() { ... } function bar() { ... } ... </script> 

then in XSLT you can just use

 <xsl:template match="/"> <html> <body> <xsl:copy-of select="document('scripts.xml')/script"/> </body> </html> </xsl:template> 

If this does not help, you need to explain in more detail which file you have, which version of XSLT you are using (XSLT 2.0 can also read in non-XML text files, such as Javascript code).

[edit] Here is an example of XSLT 2.0 using unparsed-text (requires an XSLT 2.0 processor such as Saxon or AltovaXML):

 <xsl:template match="/"> <html> <body> <script type="text/javascript"> <xsl:value-of select="unparsed-text('file.js')"/> </script> </body> </html> </xsl:template> 
+2
source

Use the unparsed-text() function to read the contents of a text file.

 <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:template match="/"> <html> <body> <xsl:sequence select="unparsed-text('YourFile.js')"/> </body> </html> </xsl:template> </xsl:stylesheet> 
+1
source

All Articles