简体   繁体   English

javascript函数中的xsl:call-template?

[英]xsl:call-template within a javascript function?

I'm really new at coding, sorry if any of this sounds silly or stupid. 我真的是编码新手,很抱歉,这听起来很愚蠢或愚蠢。 We have a new project to come up with a new webpage. 我们有一个新项目要提供一个新的网页。 I have a multiple condition if statement and would like to call a xsl template if condition is met. 我有多个条件if语句,如果条件满足,我想调用xsl模板。 Here's how I have it now and it doesn't work at all. 这是我现在的方式,它根本不起作用。

<script>
function getSelectedValue()
{
if("document.getElementById('type').value==1 and document.getElementById('cablegroup5').value==9"+
"document.getElementById('cablegroup3').value==22 and document.getElementById('cablelength').value==11")
{
<xsl:call-template name="PN">
<xsl:with-param name="Cable">ABC111-06</xsl:with-param>
</xsl:call-template>
}
}
</script>

I know the first part works, I've tested it with an alert message and that works just fine. 我知道第一部分有效,我已经用警告消息对其进行了测试,并且效果很好。 These are all activated by a button(onclick) next to multiple drop down menus. 这些都通过多个下拉菜单旁边的按钮(onclick)激活。 Is there a way to get this to work? 有没有办法让它工作? Any help would be really appreciated. 任何帮助将非常感激。 Thanks. 谢谢。

You're confused about the processing model. 您对处理模型感到困惑。 Is the script element generated by XSLT? 脚本元素是由XSLT生成的吗? If so, the call-template will probably be called at the time the script is generated. 如果是这样,则可能在生成脚本时调用调用模板。 It won't be called at the time the script is executed. 执行脚本时不会调用它。 Javascript code isn't going to magically execute XSLT instructions. Javascript代码不会神奇地执行XSLT指令。

There are a couple of issue within the script that would prevent the if statement executing correctly. 脚本中有几个问题会阻止if语句正确执行。

  1. The boolean and operator in JavaScript is && not and . JavaScript中的布尔值和运算符是&&而不是and Note the if you use & this would be a bitwise and. 请注意,如果使用&则将按位与。
  2. The tests should not be a string. 测试不应为字符串。 Due to JavaScript type coersion it a string will be converted to a boolean. 由于JavaScript类型具有强制性,因此会将字符串转换为布尔值。 A null or empty string '' will evaluate false , all other strings will evaluate true . null或空字符串''将评估为false ,所有其他字符串将评估为true Currently you have 目前你有

     if("test1 and test2") 

    This should be 这应该是

     if(test1 && test2) 

So far your updated script would be 到目前为止,您更新的脚本将是

<script>
  function getSelectedValue()
  {
  if(document.getElementById('type').value==1 && 
    document.getElementById('cablegroup5').value==9 &&
    document.getElementById('cablegroup3').value==22 &&  
    document.getElementById('cablelength').value == 11)
    {
      // Process Xml
    }
  }
</script>

You need to use the browser xml parser to handle your xml. 您需要使用浏览器xml解析器来处理xml。 I will assume that you have an xml string, if you have a document object then you will have to change the following slightly, this is from w3schools . 我将假设您有一个xml字符串,如果您有一个文档对象,那么您将不得不稍微更改以下内容,这是来自w3schools的

  var xmlString = "<Products>" +
                    "<Product partnumber='foo'>This is product 1</Product>" +
                    "<Product partnumber='bar'>This is product 2</Product>" +
                  "</Products>";
  // Load into an XML document
  var myDoc;
  if (window.DOMParser)
  {
    var parser=new DOMParser();
    myDoc=parser.parseFromString(xmlString,"text/xml");
  }
  else // Internet Explorer
  {
    myDoc=new ActiveXObject("Microsoft.XMLDOM");
    myDoc.async="false";
    myDoc.loadXML(xmlString);
  }

  // Get all product nodes
  var products = myDoc.getElementsByTagName('Product');
  var i, targetProduct, partNumber;

  for(i = 0; i < products.length; i += 1){
    // Get the partnumber attribute
    partnumber = products[i].attributes.getNamedItem('partnumber');

    // Ensure that the partnumber exists and its value is what is wanted
    if(partnumber && partnumber.value == 'foo'){
      targetProduct = products[i];
      // Exit for
      break;
    }
  }

  // If the product has been found alert its value.
  if(targetProduct != null){
    alert(targetProduct.textContent || targetProduct.text);
  }

If you were selected a node by id then you could use xmlDoc.getElementById instead of iterating through all nodes of a type and checking attributes. 如果通过ID选择了一个节点,则可以使用xmlDoc.getElementById而不是遍历一个类型的所有节点并检查属性。

To select the text value of an xml node most browsers use the property textContent although Internet Explorer uses text . 尽管Internet Explorer使用text但是大多数浏览器都使用textContent属性来选择xml节点的text值。 The line 线

  targetProduct.textContent || targetProduct.text

returns textContent if it is present and not null, or the value of text . 如果存在且不为null,则返回textContenttext的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM