Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript in XSL doesn't work 3

Status
Not open for further replies.

dk2006

MIS
Jan 19, 2006
53
US
Hello all,
I have XSL Stylesheet with function in JavaScript. I don't get any error but the function doesn't return anything.

Code:
<SCRIPT>
<![CDATA[
<!--

function Right(str, n) 
{
    if (n <= 0)     
        return "";
    else if (n > String(str).length)   
        return str;                    
    else {
        var iLen = String(str).length;
        return String(str).substring(iLen, iLen - n);
    }
}

// -->

]]>
</SCRIPT>

. 
.
.

<xsl:template match="File">

				
	<xsl:element name="A">
		<xsl:attribute name="CLASS">normalTextNoUnderLine</xsl:attribute>
		<xsl:attribute name="HREF">javascript: popAdobe(<xsl:value-of select="$SiteID" />, '<xsl:value-of select="Path" /><xsl:value-of select="Name" />')</xsl:attribute>
			<script>Right('<xsl:value-of select="Name"/>',8)</script>  <!-- somehow i can't get this to work -->
			<xsl:text>&#160;&#160;&#160;&#160;&#160;</xsl:text>	Date:	
			<xsl:value-of select="DateLastModified" />			
	</xsl:element><BR/>
			
</xsl:template>

please help.

dk
 
What are you trying to do? JavaScript does not work in this way. You can use JavaScript as an extension function in XSL, but not like you have used it.

Also, there is no point in using JavaScript to do something you can do in XSL, it very ugly.

To do the same, simply use:
Code:
<xsl:value-of select="substring(Name, string-length(Name) - 8)"/>

Jon

"I don't regret this, but I both rue and lament it.
 
Jon,
Exactly what I'm looking for.

Thank you,
dk
 
To:eek:p
To implement javascript extension in xsl, one has to content with sacrificing some portability. In windows os with msxml core service, you can do it like this which is drastically different from what you conceived.

[1] Add some namespace references. (The xmlns:user part is quite arbitrary.)
[tt]
<xsl:stylesheet version="1.0" xmlns:xsl=" [blue]xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:user="urn:jsuser-exp:js"[/blue]>[/tt]

[2] Then add the reference namespace to the script section. (Note: no more <!-- ... // --> thing. It would be an error.)
[tt]
<msxsl:script language="jscript" implements-prefix="user">
<![CDATA[
function Right(cnode, n) {
var str=cnode[0].text;
if (n <= 0) {
return "";
} else if (n > str.length) {
return sstr;
} else {
var iLen = str.length;
return str.substring(iLen - n,iLen);
}
}
]]>
</msxsl:script>
[/tt]
[3] Then you call that script service like this.
[tt]
<xsl:element name="A">
<!-- etc etc -->
<xsl:value-of select="[blue]user:[/blue]Right(Name,8)" />
<!-- etc etc -->
</xsl:element>
[/tt]
The big design structure is something like the above.
 
It adds flexibility if the code is in an external file:

<xsl:import href="CommonScripts.xsl" />
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top