Reputation: 447
I have several xsl files that need to include common xsl file. So instead of including the javascript files in each one of them I would like to create a Common.xsl that will include the java script files and all xsl files will include it.
The question is how to do that?
I tried some suggested methods using including javascript file but with no success, in the browser console I get errors:
Uncaught ReferenceError: jQuery is not defined
Uncaught ReferenceError: $ is not defined
It looks like the jquery was not included.
I tried to do something like this:
<"script type="text/javascript" src="common.js" />
or
(function() {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = "../jquery/jquery-1.9.1.min.js";
document.getElementsByTagName('head')[0].appendChild(script)
})();
Edit:
I answered my question see answer below.
Upvotes: 0
Views: 326
Reputation: 447
I solved my issue by the following code using xsl instead of javascript files:
I have created a Common.xsl file that includes all my common javascript files.
Common.xsl
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template name="Common">
<link rel="stylesheet" href="SiteRef_getCSS?CSSName=Site/site.css" type="text/css"/>
<script type="text/javascript" src="SiteRef_getJS?JSName=jquery/jquery-1.11.1.min.js"/>
<script type="text/javascript" src="SiteRef_getJS?JSName=Site/common.js"/>
<script type="text/javascript" src="SiteRef_getJS?JSName=Site/MessagesConstants.js"/>
<script type="text/javascript" src="SiteRef_getJS?JSName=Site/win_functions.js"/>
<script type="text/javascript" src="SiteRef_getJS?JSName=Site/resizeFunctions.js"/>
</xsl:template>
On every xsl file that need those includes I added the following lines:
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:include href="common/Common.xsl"/> <!-- Here I include the Common.xsl -->
<xsl:template match="/Reflection">
<xsl:text disable-output-escaping='yes'><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "SiteRef_getCSS?CSSName=Site/xhtml1-transitional.dtd"></xsl:text>
<html>
<head>
<title><xsl:value-of select="Localization/ViewAbout"/></title>
<xsl:call-template name="Common"/> <!-- Here I apply the tamplate of the common includes -->
<body>
.
.
.
</body>
</xsl:template>
</xsl:transform>
In order resolve the include URI (see xsl:include) in my java application server I had to implement the URIResolver resolve() function:
public class XslURIResolver implements URIResolver
{
private static final String XSL_PATH = System.getProperty("user.dir") + File.separatorChar + INetlayerConstants.c_UI_PATH + INetlayerConstants.c_XSL_PATH;
@Override
public Source resolve(String href, String base) throws TransformerException
{
DOMSource xslDomSource;
try
{
DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
dFactory.setNamespaceAware(true);
DocumentBuilder dBuilder = dFactory.newDocumentBuilder();
InputStream in = new FileInputStream(XSL_PATH + href);
InputSource xslInputSource = new InputSource(in);
Document xslDoc = dBuilder.parse(xslInputSource);
xslDomSource = new DOMSource(xslDoc);
xslDomSource.setSystemId(XSL_PATH + href);
return xslDomSource;
}
catch (ParserConfigurationException e)
{
e.printStackTrace();
}
catch (SAXException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return null;
}
}
And then before transformation I set the URIResolver:
TransformerFactory tFactory = TransformerFactory.newInstance();
tFactory.setURIResolver(new XslURIResolver());
That's about it, hope it helps someone...
Upvotes: 0
Reputation: 20384
<xsl:element name="script">
<xsl:attribute name="src">common.js</xsl:attribute>
/* common code */
</xsl:element>
That should do the trick
Upvotes: 0