从XSL文件调用Javascript

时间:2009-04-27 10:32:01

标签: javascript

我有一个XML文件........

<ROOT>
      <MYNODES>
           <MYNODE>A</MYNODE>
           <MYNODE>B</MYNODE>
           <MYNODE>C</MYNODE>
           <MYNODE>D</MYNODE>
      </MYNODES> 
      <DOCS>
           <DOC>1</DOC>
           <DOC>2</DOC>
           <DOC>3</DOC>
      </DOC> 
      <PICS>
           <PIC>a.jpeg</PIC>
           <PIC>b.jpeg</PIC>
           <PIC>c.jpeg</PIC>
           <PIC>d.jpeg</PIC>
           <PIC>e.jpeg</PIC>
          </PICS>
  </ROOT>

并遵循XSLT文件.....

<?xml version="1.0"?>

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

<xsl:output method="xml" version="1.0" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"/>

<xsl:template match="/ROOT">
    <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <title>JINI  <xsl:value-of select="/ROOT/MYNODES/MYNODE/." /> </title>
        <link rel="stylesheet" type="text/css" href="jini.css" />
        <script language="JavaScript" src="commonHead.js" type="text/javascript" />
        </head>
    <body>

<table style="font-size:11px" border="5" bgcolor="#FFFFFF" width="99%" align="center" cellpadding="5">
<tr>
<td>
<xsl:attribute  name="bgcolor"> #FFAAAA </xsl:attribute> <xsl:attribute  name="onMouseover" >displayColor(event, 'blue') </xsl:attribute> <xsl:attribute  name="onMouseout"> backToOriginal(event, 'yellow') </xsl:attribute> AA</td>
<td>BB </td>
</tr>
</table>
</body>
</html>
</xsl:stylesheet>

但我的displayColor在浏览器中不起作用... 任何线索....如何在XSL文件中调用java脚本文件的外部函数...

我的Javascript文件是

   /***********************************************
   * Highlight Table Cells Script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
   * Visit http://www.dynamicDrive.com for hundreds of DHTML scripts
   * This notice must stay intact for legal use
   ***********************************************/

   // Specify the default location for flying widget
   var xpos = -10;
   var ypos = 20;

   //Specify highlight behavior. "TD" to highlight table cells, "TR" to highlight the entire row:
   var highlightbehavior="TD"

   var ns6=document.getElementById&&!document.all
   var ie=document.all

   function displayColor(event, color){
     /* Popup is integrated within changeto */
     changeto(event, color,0,"");
   }

   function displayColorAndInfos(event, color, line){
     /* Popup is integrated within changeto */
     changeto(event, color,1,line);
   }

   function backToOriginal(event, color) {
     /* Killlink is integrated within changeback */
     changeback(event, color);
   }

   function changeto(e,highlightcolor,flag,line){
   source=ie? event.srcElement : e.target
   if (source.tagName=="TABLE")
   return
   while(source.tagName!=highlightbehavior && source.tagName!="HTML")
   source=ns6? source.parentNode : source.parentElement
   if (source.style.backgroundColor!=highlightcolor&&source.id!="ignore"){
      source.style.backgroundColor=highlightcolor
      if (flag==1) {
    poplink(line,-500,5);
    }
      }
   }

   function contains_ns6(master, slave) { //check if slave is contained by master
   while (slave.parentNode)
   if ((slave = slave.parentNode) == master)
   return true;
   return false;
   }

   function changeback(e,originalcolor){
   if (ie&&(event.fromElement.contains(event.toElement)||source.contains(event.toElement)||source.id=="ignore")||source.tagName=="TABLE")
   return
   else if (ns6&&(contains_ns6(source, e.relatedTarget)||source.id=="ignore"))
   return
   if (ie&&event.toElement!=source||ns6&&e.relatedTarget!=source) {
      source.style.backgroundColor=originalcolor
      killlink();
      }
   }

3 个答案:

答案 0 :(得分:3)

您尚未关闭<script>代码 - 您需要:

<script src="commonHead.js" type="text/javascript"></script>

答案 1 :(得分:1)

您发布的XML和XSLT存在一些不一致之处。

尝试以下操作(请注意插入脚本标记内的空白以避免节点被XSLT引擎折叠):

XSLT:

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

  <xsl:output method="html" version="1.0" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"/>

  <xsl:template match="/ROOT">

    <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
      <head>
        <title>
          JINI <xsl:value-of select="/ROOT/MYNODES/MYNODE/." />
        </title>
        <link rel="stylesheet" type="text/css" href="jini.css" />
        <script src="commonHead.js" type="text/javascript" >
            <xsl:text> </xsl:text>
        </script>
      </head>
      <body>
        <table style="font-size:11px" border="5" bgcolor="#FFFFFF" width="99%" align="center" cellpadding="5">
          <tr>
            <td bgcolor="#FFAAAA" onMouseover="displayColor(event, 'blue');" onMouseout="backToOriginal(event, 'yellow');">
              AA
            </td>
            <td>BB </td>
          </tr>
        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

XML输入文档:

<?xml version="1.0" encoding="utf-8"?>
<ROOT>
  <MYNODES>
    <MYNODE>A</MYNODE>
    <MYNODE>B</MYNODE>
    <MYNODE>C</MYNODE>
    <MYNODE>D</MYNODE>
  </MYNODES>
  <DOCS>
    <DOC>1</DOC>
    <DOC>2</DOC>
    <DOC>3</DOC>
  </DOCS>
  <PICS>
    <PIC>a.jpeg</PIC>
    <PIC>b.jpeg</PIC>
    <PIC>c.jpeg</PIC>
    <PIC>d.jpeg</PIC>
    <PIC>e.jpeg</PIC>
  </PICS>
</ROOT>

由于您未提供JavaScript代码,因此无法查看脚本中是否存在问题。否则一切都应该可以正常使用上面的输入/ XSLT。

答案 2 :(得分:0)

您还没有关闭XML文件中的<ROOT>标记:

<ROOT>
  <MYNODES>
       <MYNODE>A</MYNODE>
       <MYNODE>B</MYNODE>
       <MYNODE>C</MYNODE>
       <MYNODE>D</MYNODE>
  </MYNODES> 
  <DOCS>
       <DOC>1</DOC>
       <DOC>2</DOC>
       <DOC>3</DOC>
  </DOC> 
  <PICS>
       <PIC>a.jpeg</PIC>
       <PIC>b.jpeg</PIC>
       <PIC>c.jpeg</PIC>
       <PIC>d.jpeg</PIC>
       <PIC>e.jpeg</PIC>
  </PICS>
</ROOT>