首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > XML SOAP >

javascript 向 xsl 传递参数,为什么传递不了,给位来帮帮小弟解决思路

2012-02-22 
javascript 向 xsl 传递参数,为什么传递不了,给位来帮帮小弟我现在需要向XSL传递一个param,让后获取此para

javascript 向 xsl 传递参数,为什么传递不了,给位来帮帮小弟

我现在需要向XSL传递一个param,让后获取此param位置的数据

param 名为 pageNum

以下为代码:

javascript:

function loadDocument(fileName) 
{
  var xmlDoc = window.ActiveXObject ? new ActiveXObject("MSXML2.DOMDocument.3.0") :
  document.implementation.createDocument("","",null);
  xmlDoc.async = false;
  xmlDoc.load(fileName);
  return xmlDoc;
}


function getTransformedHTML(xmlDoc, xslDoc) {
  var html = "";
  if (window.XSLTProcessor) 
  {
  var num = 1 ;
  var xmlDom=new ActiveXObject('Msxml2.DOMDocument.3.0');
  var docStyle = new ActiveXObject('MSXML2.FreeThreadedDOMDocument');
  //var xsltProc = new XSLTProcessor();
  //xsltProc.importStylesheet(xslDoc);
  //xsltProc.addParameter("pageNum", num);

  var docTemplate = new ActiveXObject("Msxml2.XSLTemplate");
  docTemplate.stylesheet = docStyle;
  var processor = docTemplate.createProcessor();
  processor.input = xmlDom;

---------------------------------- 此处为问题关键处-----------------------


  processor.addParameter("pageNum",num);

-------------------------------------------
  processor.transform();
  html = processor.output;
   


  //var fragment = xsltProc.transfrmToDocument(xmlDoc);
  //html = new XMLSerializer().serializeToString(fragment);
  } 
  else if (window.ActiveXObject) 
  {
  html = xmlDoc.transformNode(xslDoc);
  }
  return html;
}

function GenerateCart()
{
  var xmlDoc = loadDocument("cart.xml");
  var xslDoc = loadDocument("cart.xsl"); 
  var content = getTransformedHTML(xmlDoc, xslDoc);  
  document.getElementById("cart").innerHTML = getTransformedHTML(xmlDoc, xslDoc);
  alert(content);
}

function AddRemoveItem() {}



xsl:

<?xml version="1.0" encoding="utf-8"?>

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

  <xsl:output 
  method="html" 
  indent="yes" 
  version="4.0"
  doctype-public="-//W3C//DTD HTML 4.01//EN"
  doctype-system="http://www.w3.org/TR/html4/strict.dtd"/>
  <xsl:param name="pageNum">undefined</xsl:param>
  <xsl:template match="/">
  <table id="shoppingcart">
  <xsl:call-template name="DisplayCart"></xsl:call-template>
  </table> 
  </xsl:template>


  <xsl:template name="DisplayCart">
   
  <xsl:param name="pageNum"/> 
   
  <tr class="head">
  <td colspan="4" align="center">Shopping Basket <img src="sbasket.gif"></img>
  </td>
  </tr>
  <xsl:if test="string($pageNum) = '' ">
  <tr>
  <td colspan="4" align="center">error


  </td>
  </tr>
  </xsl:if>
  <xsl:if test="number(//book/Quantity)>0">
  <tr>
  <td class="border">Item</td>
   
  <td class="border">Qty</td>
  <td class="border">Price</td>
  <td></td>
  </tr>
  </xsl:if>
  <xsl:for-each select="//book[position()=$pageNum]">
  <tr>
  <td class="border2" width="75px">
  <xsl:value-of select="Title"/>
  </td>
   
  <td class="border2" align="center">
  <xsl:value-of select="Quantity"/>
  </td>
  <td class="border2">
  $<xsl:value-of select="Price * Quantity"/>
  </td>
  <td class="border2">
  <a href="javascript:AddRemoveItem('Remove');">
  <img src='button.jpg'/>
  </a>
  </td>
  </tr>
  </xsl:for-each>
  <tr >
  <td colspan='4' class="border2"> </td>
 
  </tr>
  <xsl:choose>
  <xsl:when test="sum(//book/Quantity)&gt;0">
  <tr>
  <td colspan="2" class="border2">Total:</td>
   
  <td class="border">
  $
  <xsl:value-of select="(//Total)"/>
  </td>
  <td class="border2"> </td>
  </tr>
  </xsl:when>
  <xsl:otherwise>
  <tr>
  <td colspan = "4" class="border2">Your Basket Is Empty</td>
  </tr>
  </xsl:otherwise>
  </xsl:choose>
  <tr >
  <td colspan="4" class="border2"> </td>
   
  </tr>
  </xsl:template>
</xsl:stylesheet>



问题:

我在XSL里加了个判断,如果参数pageNum为空,则显示error

现在每次都显示error,则证明pageNum在 processor.addParameter("pageNum",num)的时候没有传递成功

请问这是为什么?网上很多例子都这样写的啊

请教如何解决~!!!!!!


[解决办法]
这样是可以的

XML code
<script type="text/javascript">function loadDocument(fileName){    var xmlDoc = window.ActiveXObject ? new ActiveXObject("MSXML2.DOMDocument.3.0") :                                        document.implementation.createDocument("","",null);    xmlDoc.async = false;    xmlDoc.load(fileName);    return xmlDoc;}function getTransformedHTML(xmlDoc, xslDoc) {    var html = "";    if (window.ActiveXObject)    {          var num = 1 ;          var docTemplate = new ActiveXObject("Msxml2.XSLTemplate");      var xslDoc = new ActiveXObject("Msxml2.FreeThreadedDOMDocument.3.0");           xslDoc.async = false;          xslDoc.load("cart.xsl");          docTemplate.stylesheet = xslDoc;          var processor = docTemplate.createProcessor();          processor.input = xmlDoc;          processor.addParameter("pageNum",num);          processor.transform();          html = processor.output;    }       return html;}function GenerateCart(){  var xmlDoc = loadDocument("cart.xml");  var xslDoc = loadDocument("cart.xsl");  var content = getTransformedHTML(xmlDoc, xslDoc);   alert(content);} GenerateCart()</script> 

热点排行