Using JavaScript with XML and XSL

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

    Using JavaScript with XML and XSL

    Is it possible to use JavaScript with XML and XSL?
    I tried the following but it does not work.. anything missing?

    test.xml file:
    --------------
    <?xml version="1.0" encoding="ISO88 59-1" ?>
    <?xml-stylesheet type="text/xsl" href="test.xsl" ?>
    <REPORT>

    <FOOTER>
    <FIELD id="errors">
    <KEY>Error Code</KEY>
    <VALUE>200014 5</VALUE>
    </FIELD>
    <FIELD id="configurati on">
    <KEY>DEBUG</KEY>
    <VALUE>TRUE</VALUE>
    </FIELD>
    </FOOTER>

    </REPORT>

    test.xsl file:
    --------------
    <?xml version='1.0'?>
    <xsl:styleshe et version='1.0'
    xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">

    <HTML>
    <BODY bgcolor="palego ldenrod">

    <SCRIPT LANGUAGE="javas cript">
    <!--
    function seeFooter_oncli ck()
    {
    footer1.style.d isplay = "none"
    }
    //-->
    </SCRIPT>

    <INPUT id="seeFooter" type="checkbox" LANGUAGE="javas cript"

    onclick="seeFoo ter_onclick()"> FOOTER</INPUT>
    <TABLE id="footer1" bgcolor="#d1d2d 2" border="1" width="100%">
    <xsl:for-each select="REPORT/FOOTER/FIELD">
    <TR>
    <TD width="40%"><xs l:value-of select="KEY"/></TD>
    <TD><xsl:valu e-of select="VALUE"/></TD>
    </TR>
    </xsl:for-each>
    </TABLE>

    </BODY>
    </HTML>

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

    The basic requirement is on the click of checkbox the <TABLE> should
    disappear. But when i click the checkbox, browser generates an error.

    I might also be posting this in the wrong newsgroup, so the pointer to
    the right also would help

    Regards
    Fareeda
  • Martin Honnen

    #2
    Re: Using JavaScript with XML and XSL



    fareeda wrote:
    [color=blue]
    > Is it possible to use JavaScript with XML and XSL?[/color]

    What you have below is simply some script in the HTML result document of
    an XSLT transformation, that shouldn't pose any problems different from
    using script directly in an HTML document.
    [color=blue]
    > I tried the following but it does not work.. anything missing?
    >
    > test.xml file:
    > --------------
    > <?xml version="1.0" encoding="ISO88 59-1" ?>
    > <?xml-stylesheet type="text/xsl" href="test.xsl" ?>
    > <REPORT>
    >
    > <FOOTER>
    > <FIELD id="errors">
    > <KEY>Error Code</KEY>
    > <VALUE>200014 5</VALUE>
    > </FIELD>
    > <FIELD id="configurati on">
    > <KEY>DEBUG</KEY>
    > <VALUE>TRUE</VALUE>
    > </FIELD>
    > </FOOTER>
    >
    > </REPORT>
    >
    > test.xsl file:
    > --------------
    > <?xml version='1.0'?>
    > <xsl:styleshe et version='1.0'
    > xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
    > <xsl:template match="/">
    >
    > <HTML>
    > <BODY bgcolor="palego ldenrod">
    >
    > <SCRIPT LANGUAGE="javas cript">
    > <!--
    > function seeFooter_oncli ck()
    > {
    > footer1.style.d isplay = "none"
    > }
    > //-->
    > </SCRIPT>
    >
    > <INPUT id="seeFooter" type="checkbox" LANGUAGE="javas cript"
    >
    > onclick="seeFoo ter_onclick()"> FOOTER</INPUT>
    > <TABLE id="footer1" bgcolor="#d1d2d 2" border="1" width="100%">
    > <xsl:for-each select="REPORT/FOOTER/FIELD">
    > <TR>
    > <TD width="40%"><xs l:value-of select="KEY"/></TD>
    > <TD><xsl:valu e-of select="VALUE"/></TD>
    > </TR>
    > </xsl:for-each>
    > </TABLE>
    >
    > </BODY>
    > </HTML>
    >
    > </xsl:template>
    > </xsl:stylesheet>
    >
    > The basic requirement is on the click of checkbox the <TABLE> should
    > disappear. But when i click the checkbox, browser generates an error.[/color]

    Which browser is that, Mozilla or Netscape?
    To make your script work with DOM compliant browsers like
    Mozilla/Netscape you should use

    function seeFooter_oncli ck () {
    if (document.getEl ementById) {
    var footer = document.getEle mentById('foote r1');
    if (footer && footer.style) {
    footer.style.di splay = 'none';
    }
    }
    }

    --

    Martin Honnen


    Comment

    • Yann-Erwan Perio

      #3
      Re: Using JavaScript with XML and XSL

      fareeda wrote:
      [color=blue]
      > <SCRIPT LANGUAGE="javas cript">
      > <!--
      > function seeFooter_oncli ck()
      > {
      > footer1.style.d isplay = "none"
      > }
      > //-->
      > </SCRIPT>[/color]

      You've got three problems here:
      - you use the "language" attribute while you should prefer the "type"
      attribute,
      - you've enclosed the script part into comments, as a result it's not
      even interpreted,
      - you're using IE-specific object accessors, while using the standard
      document.getEle mentById would make your script work on more platforms.

      Note that it's usually recommended to externalize the script part in a
      javascript include, to avoid parsing issues with the script content.
      [color=blue]
      > <INPUT id="seeFooter" type="checkbox" LANGUAGE="javas cript"[/color]

      An input element doesn't have a "language" attribute.
      [color=blue]
      > onclick="seeFoo ter_onclick()"> FOOTER</INPUT>[/color]

      An input element doesn't have any closing tag.


      <?xml version='1.0'?>
      <xsl:styleshe et
      version='1.0'
      xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
      <xsl:template match="/">

      <html>
      <head>
      <title>Hello, World!</title>
      <style type="text/css">
      body { background-color:palegolde nrod }
      </style>
      <script type="text/javascript">
      function seeFooter_oncli ck(){
      if(document.get ElementById){
      with(document.g etElementById(" footer1"))
      style.display=s tyle.display==" none"?"":"none" ;
      }
      }
      </script>
      </head>
      <body>
      <input type="checkbox" onclick="seeFoo ter_onclick()" />
      FOOTER
      <table id="footer1" border="1" width="100%">
      <xsl:for-each select="REPORT/FOOTER/FIELD">
      <tr>
      <td width="40%"><xs l:value-of select="KEY" /></td>
      <td><xsl:valu e-of select="VALUE" /></td>
      </tr>
      </xsl:for-each>
      </table>
      </body>
      </html>

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


      HTH,
      Yep.

      Comment

      Working...