我的contains()有什么问题;我的XML解析器中的函数?

时间:2012-03-08 19:01:20

标签: javascript html xml xslt xpath

在作者和标题字段中,用户可以输入如下字符串:

包含广告包含杰克包含确认等。

并且我的html表将显示相应的信息(即标题(或作者,如果我们正在谈论作者字段..)的书籍包含给定的子字符串..但它不起作用..我在做什么错了?我一直在我的错误控制台中获取contains is not defined ..

(顺便说一下,我的所有条件var author=...等定义应该放在if(moz)之前?否则这只适用于Firefox吗?)

 <html>
  <script type="text/javascript">

  function filterTable(f)
  {
  var xmlDoc = loadXML("books.xml");
  var stylesheet = loadXML("books.xsl");
  var moz = (typeof document.implementation.createDocument != 'undefined');
  var ie = (typeof window.ActiveXObject != 'undefined');

  if (moz)
  {
    var nsResolver = stylesheet.createNSResolver( stylesheet.ownerDocument == null ? stylesheet.documentElement : stylesheet.ownerDocument.documentElement);

    var value = stylesheet.evaluate("//xsl:template[@match='/']//xsl:for-each", stylesheet, nsResolver, XPathResult.ANY_UNORDERED_NODE_TYPE, null);


    var author = document.getElementById("inauthor").value;
    var title = document.getElementById("intitle").value;
    var filter = "";

    if (author != "")
    {
       if(contains(author,"contains"))
       {
              filter = filter + "contains(author," + "'" + author.substr(10) + "'" + ")";
       }

       else filter= filter + "author='" + author.substr(1) + "'";
    }

    if (title != "")
    {
        if (filter != "") filter = filter + " and ";

        if(contains(title,"contains"))
        {
          filter = filter + "contains(title," + "'" + title.substr(10) + "'" + ")";
        }

        else
        {
          filter = filter+ "title='" + title.substr(1) + "'";
        }
    }

    if (filter != "") filter = "[" + filter + "]";

    value.singleNodeValue.setAttribute("select", "books/scifi"+filter);

    var proc = new XSLTProcessor();
    proc.importStylesheet(stylesheet);
    var resultFragment = proc.transformToFragment(xmlDoc, document);
    document.getElementById("target4").appendChild(resultFragment);
  }

  else if (ie)
  {

    var value = stylesheet.selectSingleNode("//xsl:template[@match='/']//xsl:for-each");
    value.setAttribute("select", "books/scifi"+filter);
    document.write(xmlDoc.transformNode(stylesheet));
  }


 }

    </script>
<center>
  <table border="1" cellpadding="8">
  <tr><td>
  </br>
    <form>
      <center>
        <b> search by </b>
        Author(s): <input type="text" name="authors" id="inauthor"/>
        Title: <input type="text" name="title" id="intitle" />
        </br></br>
        <input type="button" value="Display" onClick="filterTable(this.form)"/>
     </center>
    </form>
   </td></tr>
   </table>
</center>

</body>

</html>

2 个答案:

答案 0 :(得分:0)

How to check whether a string contains a substring in JavaScript?

  

通常我会期待一个String.contains()方法,但没有   似乎是一个。

     

indexOf返回另一个字符串中字符串的位置。如果不   发现,它将返回-1。

编辑:

if (contains(author,"contains")) {

应该是:

if (myString.indexOf(author) !== -1) {

答案 1 :(得分:0)

您的错误在于认为XPath函数可用作JavaScript函数。 XPath contains(haystack, needle)函数仅在XPath表达式中可用。因此,if (contains(author,"contains"))if(contains(title,"contains"))应为if(author.indexOf("contains") >= 0)if (title.indexOf("contains") >= 0)。对于额外的功劳,您还应该转义'中的所有*blah*.substr(10)字符,这样当作者的名字是“O'Malley,Liam”时,您的代码就不会爆炸。