By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,025 Members | 2,118 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,025 IT Pros & Developers. It's quick & easy.

Hwo to do Hello World From XSLT to JavaScript?

P: n/a
RC
Hello World,
I am try do call a JavaScript function from XSLT, but I got
function not avaible error. See "????" below.
Would someone out there tell me how?
Thank Q!

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="html" />
<xsl:template match="/">

<html><head><title>Hello World</title>
<script type="text/javascript" language="JavaScript1.2">

function hello(w) {
return ("Hello " + w + "!");
}
//
</script>
</head><body>

<xsl:variable name="w" select='World' />

<!-- How do I call JavaScript function here? -->
<xsl:value-of select="????.hello($w)" />

</body></html>

</xsl:template>
</xsl:stylesheet>
Jul 23 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a


RC wrote:

I am try do call a JavaScript function from XSLT, but I got
function not avaible error.
I think you confuse two techniques, one is to use script functions in an
XSLT stylesheet, the other is to use script functions in an HTML document.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="html" />
<xsl:template match="/">

<html><head><title>Hello World</title>
<script type="text/javascript" language="JavaScript1.2">

function hello(w) {
return ("Hello " + w + "!");
}
//
</script>
So here you have a literal result element <script> in your stylesheet
meaning that <script> element and the function it defines are part of
the HTML document which is the result of the XSLT transformation.
</head><body>

<xsl:variable name="w" select='World' />

<!-- How do I call JavaScript function here? -->
<xsl:value-of select="????.hello($w)" />


Here you have an XPath expression that tries to call a function defined
in script, for that to work the XSLT processor needs to support
extension functions written in script and you need to use the proper way
for the XSLT procesor of your choice to include the function.

Which XSLT processor are you using?

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #2

P: n/a
RC
Martin Honnen wrote:
Which XSLT processor are you using?


Netscape 7.2.x and/or IE 6.x
Or

java org.apache.xalan.xslt.Process
Jul 23 '05 #3

P: n/a

"RC" <ra**********@nospam.noaa.gov> wrote in message
news:d2**********@news.nems.noaa.gov...
Hello World,
I am try do call a JavaScript function from XSLT, but I got
function not avaible error. See "????" below.
Would someone out there tell me how?
Thank Q!

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="html" />
<xsl:template match="/">

<html><head><title>Hello World</title>
<script type="text/javascript" language="JavaScript1.2">

function hello(w) {
return ("Hello " + w + "!");
}
//
</script>
</head><body>

<xsl:variable name="w" select='World' />

<!-- How do I call JavaScript function here? -->
<xsl:value-of select="????.hello($w)" />

</body></html>

</xsl:template>
</xsl:stylesheet>


If you want the *browser* to execute this javascript, based on a
variable-value, do this:

<xsl:variable name="w" select='World' />
....
....
<script language="javascript">
var someVar = hello("<xsl:value-of select="$w"/>");
...
...
</script>
....

If you want to execute javascript *from within the XSLT* transformation
itself, you have to use XSLT-extensions for JavaScript.
Go to this site for more info:
http://xml.apache.org/xalan-j/extensions.html
Good luck.
Jul 23 '05 #4

P: n/a


RC wrote:
Martin Honnen wrote:
Which XSLT processor are you using?

Netscape 7.2.x and/or IE 6.x


IE uses MSXML 3 by default, MSXML 3 has support for script in XSLT, see
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/xmlsdk/html/xmrefmsxslscriptElement.asp>
Netscape 7 respectively Mozilla has its own built-in XSLT processor but
that doesn't support script in XSLT.
java org.apache.xalan.xslt.Process


I am not sure about Xalan but if it has support for JavaScript then most
certainly not in a portable way to MSXML.

As a summary, try to avoid script in your XSLT stylesheet solutions, it
is not portable at all, even if the vendor is the same, for instance
while both MSXML and MS .NET support scripting in XSLT the APIs exposed
to script differ.

Try to ask yourself why you think you need script in an XSLT stylesheet
and then try to find ways to solve the problem with standard XSLT and
XPath 1.0.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #5

P: n/a
RC
Martin Honnen wrote:

I am not sure about Xalan but if it has support for JavaScript then most
certainly not in a portable way to MSXML.
You may want to see this link below. It introduce about
Bean Scripting Framework (BSF), which support
JavaScript, JPython, Jacl, ActiveScript(JScript,VBScript),
PerlScript, etc.

http://xml.apache.org/xalan-j/extensions.html

Also see

http://xml.apache.org/xalan-j/extensionslib.html
Try to ask yourself why you think you need script in an XSLT stylesheet
and then try to find ways to solve the problem with standard XSLT and
XPath 1.0.


Hahaha....!
I am try to do something like

2005-02-28 23:45:00 + 30 = 2005-03-01 00:15:00

I need to take care the leap year for February 28 days or 29 days,
the months for 30 days or 31 days.

I can do this in XSLT, but will very hassle and long.
Will be nice for me call Perl, or Java or JavaScript?

like
<xsl:value-of select="myjavascript:addTime('2005-02-28 23:45:00', 30)" />
Jul 23 '05 #6

P: n/a
JRS: In article <42**************@nospam.noaa.gov>, dated Wed, 30 Mar
2005 07:31:12, seen in news:comp.lang.javascript, RC
<ra**********@nospam.noaa.gov> posted :
Hahaha....!
I am try to do something like

2005-02-28 23:45:00 + 30 = 2005-03-01 00:15:00

I need to take care the leap year for February 28 days or 29 days,
the months for 30 days or 31 days.

I can do this in XSLT, but will very hassle and long.
Will be nice for me call Perl, or Java or JavaScript?

like
<xsl:value-of select="myjavascript:addTime('2005-02-28 23:45:00', 30)" />

You need to decide whether you want to do it in UTC or in local time.
Assuming from your -0500 that you are in East CONUS, it's likely that
you won't have to wait more than about 36 hours from now to see the
difference!

S = "2005-02-28 23:45:00" // or that plus " GMT"
S = S.replace(/-/g, "/") // AFAIK, all setups take yyyy/mm/dd
D = new Date(S)
D.setMinutes(D.getMinutes()+30) // or ...etUTCMin...

That will be correct for as long, ahead or aback, as the calendar rules
of the interpreting system are valid.

You'll also want, it seems, code to output D in ISO 8601, UTC or local;
for that, etc., see my Web site via sig below.

You should, of course, have known that from reading the newsgroup FAQ.
Now, after reading my relevant pages, can you explain US non-Fed
standards for week numbering?!? And US IRS practice?

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.