Hwo to do Hello World From XSLT to JavaScript?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • RC

    #1

    Hwo to do Hello World From XSLT to JavaScript?

    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:styleshe et xmlns:xsl="http ://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:output method="html" />
    <xsl:template match="/">

    <html><head><ti tle>Hello World</title>
    <script type="text/javascript" language="JavaS cript1.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="????.he llo($w)" />

    </body></html>

    </xsl:template>
    </xsl:stylesheet>
  • Martin Honnen

    #2
    Re: Hwo to do Hello World From XSLT to JavaScript?



    RC wrote:

    [color=blue]
    > I am try do call a JavaScript function from XSLT, but I got
    > function not avaible error.[/color]

    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.
    [color=blue]
    > <xsl:styleshe et xmlns:xsl="http ://www.w3.org/1999/XSL/Transform"
    > version="1.0">
    > <xsl:output method="html" />
    > <xsl:template match="/">
    >
    > <html><head><ti tle>Hello World</title>
    > <script type="text/javascript" language="JavaS cript1.2">
    >
    > function hello(w) {
    > return ("Hello " + w + "!");
    > }
    > //
    > </script>[/color]

    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.
    [color=blue]
    > </head><body>
    >
    > <xsl:variable name="w" select='World' />
    >
    > <!-- How do I call JavaScript function here? -->
    > <xsl:value-of select="????.he llo($w)" />[/color]

    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/

    Comment

    • RC

      #3
      Re: Hwo to do Hello World From XSLT to JavaScript?

      Martin Honnen wrote:
      [color=blue]
      > Which XSLT processor are you using?[/color]

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

      java org.apache.xala n.xslt.Process

      Comment

      • Anton Spaans

        #4
        Re: Hwo to do Hello World From XSLT to JavaScript?


        "RC" <raymond.chui@n ospam.noaa.gov> wrote in message
        news:d2c2q0$hcp $1@news.nems.no aa.gov...[color=blue]
        > 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:styleshe et xmlns:xsl="http ://www.w3.org/1999/XSL/Transform"
        > version="1.0">
        > <xsl:output method="html" />
        > <xsl:template match="/">
        >
        > <html><head><ti tle>Hello World</title>
        > <script type="text/javascript" language="JavaS cript1.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="????.he llo($w)" />
        >
        > </body></html>
        >
        > </xsl:template>
        > </xsl:stylesheet>[/color]

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

        <xsl:variable name="w" select='World' />
        ....
        ....
        <script language="javas cript">
        var someVar = hello("<xsl:val ue-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.


        Comment

        • Martin Honnen

          #5
          Re: Hwo to do Hello World From XSLT to JavaScript?



          RC wrote:
          [color=blue]
          > Martin Honnen wrote:
          >[color=green]
          >> Which XSLT processor are you using?[/color]
          >
          >
          > Netscape 7.2.x and/or IE 6.x[/color]

          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/xmrefmsxslscrip tElement.asp>
          Netscape 7 respectively Mozilla has its own built-in XSLT processor but
          that doesn't support script in XSLT.
          [color=blue]
          > java org.apache.xala n.xslt.Process[/color]

          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/

          Comment

          • RC

            #6
            Re: Hwo to do Hello World From XSLT to JavaScript?

            Martin Honnen wrote:
            [color=blue]
            >
            > I am not sure about Xalan but if it has support for JavaScript then most
            > certainly not in a portable way to MSXML.[/color]

            You may want to see this link below. It introduce about
            Bean Scripting Framework (BSF), which support
            JavaScript, JPython, Jacl, ActiveScript(JS cript,VBScript) ,
            PerlScript, etc.

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

            Also see

            http://xml.apache.org/xalan-j/extensionslib.html
            [color=blue]
            > 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.[/color]

            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="myJavaS cript:addTime(' 2005-02-28 23:45:00', 30)" />

            Comment

            • Dr John Stockton

              #7
              Re: Hwo to do Hello World From XSLT to JavaScript?

              JRS: In article <424A9C10.10501 02@nospam.noaa. gov>, dated Wed, 30 Mar
              2005 07:31:12, seen in news:comp.lang. javascript, RC
              <raymond.chui@n ospam.noaa.gov> posted :
              [color=blue]
              >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="myJavaS cript:addTime(' 2005-02-28 23:45:00', 30)" />[/color]


              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.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
              <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
              <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

              Comment

              Working...