Mark Steggles
Mark Steggles

Reputation: 5553

how to include script tag in an xsl file?

I'm working on an old site that uses java and xsl. How can I inculde a script file in an xsl file? Top of the file:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:include href="shipmentPackageInfo.xsl"/>

<script src="/fs/scripts/shipment/shipment.js"></script>

breaks the app

-- UPDATE --

There is another file called pageHeader.xsl which has all the script tags inside of

<xsl:output method="html"/>
  <xsl:template match="PageHeaderData">

Upvotes: 11

Views: 38950

Answers (7)

DVN-Anakin
DVN-Anakin

Reputation: 1772

The solution that works for:

    <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output encoding='utf-8' />
    <xsl:output method="xml" omit-xml-declaration="yes" standalone="no" indent="yes" media-type="text/xhtml" />

is:

<script type="text/javascript">
    <xsl:text disable-output-escaping="yes">

         <![CDATA[

               /* Your Javascript code comes here  */

         ]]>
    </xsl:text>
    <xsl:comment>_</xsl:comment>
 </script>

Upvotes: 2

annonymously
annonymously

Reputation: 4708

There seems to be a solution here:

http://www.webdeveloper.com/forum/archive/index.php/t-20815.html

Put javascript code between <xsl:text> tag something like below

<script type="text/javascript">

<xsl:text>

javascript here

</xsl:text>

</script>

Upvotes: 9

thinkbase
thinkbase

Reputation: 141

Oh, I do like this:

... ...
        </body>
<script language="javascript"><![CDATA[
var a_patch = function(){
    var links = document.getElementsByTagName("a");
    for (var i=0; i<links.length; i++){
        var link = links[i];
        var text = link.innerHTML;
        ... ...
    }
};
a_patch();
]]></script>
</html>

Upvotes: 8

Rumplin
Rumplin

Reputation: 2768

 <script type="text/javascript" src="/Scripts/script.js">
     <xsl:comment>script</xsl:comment>
 </script>

Upvotes: 0

Kevin Hakanson
Kevin Hakanson

Reputation: 42200

Take a look at my answer to XSLT wont allow me to use self-closing img and br tags

Self closing script tags (<script src="code.js"/>) can cause the JavaScript files not to load, so inside of your XSLT, you may need to have some text inside the script tag to keep it from self closing and get it to work.

<script src="code.js>//</script>

Upvotes: 10

First Zero
First Zero

Reputation: 22356

EDITED

Here is a solution that works

Assuming this is your xsl and you are including xsl as you mentioned. In this case I called it include.xsl. I just call a template called headers that pipes out the javascript reference.

Main XSLT File:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 version="1.0">
    <xsl:include href="include.xsl"/>

    <xsl:template match="/">
        <xsl:call-template name="headers"/>
        <bar>
           <xsl:value-of select="root"/>
        </bar>
    </xsl:template>
</xsl:stylesheet>

include.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
   <xsl:template name="headers">
        <script src="/fs/scripts/shipment/shipment.js"></script>
   </xsl:template>
</xsl:stylesheet>

Output:

<?xml version="1.0" encoding="utf-8"?>
<script src="/fs/scripts/shipment/shipment.js"/>
<bar>foo</bar>

Upvotes: 1

Treemonkey
Treemonkey

Reputation: 2163

you must insert the script tag within a template block or it will break...

ie

<xsl:template match="/">
    <script src="/fs/scripts/shipment/shipment.js"></script>
</xsl:template>

Upvotes: 2

Related Questions