javascript - 通过 HTML 标签进行 XPath 搜索

标签 javascript dom xpath html-parsing

以下 HTML 显示第三次搜索(搜索“专业实践指南”)不起作用,因为文本“实践指南”位于 <strong></strong> 之间。标签...是否可以实现 XPath 搜索以绕过文本之间的 HTML 标签?

<html>
<head>
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title>Xpath Test</title>

<script type="text/javascript">
var search = function (button) {
    var searchTxt = button.value;
    var xpath = '//*[contains(translate(text(),\'ABCDEFGHIJKLMNOPQRSTUVWXYZ\', \'abcdefghijklmnopqrstuvwxyz\'), \'' + searchTxt.toLowerCase() + '\')]';
    var nodes = null;

    if (document.evaluate) { 
        nodes = document.evaluate(
          xpath,
          document,
          null,
          XPathResult.ORDERED_NODE_ITERATOR_TYPE,
          null
        );

        var node = nodes.iterateNext ();
        if (node) {
            while (node) {
                var nodeXpath = createXPathFromElement(node);
                alert("Found!!!\n xpath: " + nodeXpath);
                node = nodes.iterateNext ();
            }
        } else {
            alert("No results found.");
        }
    } else {  
        alert("Your browser does not support the evaluate method!");
    }
  }

  function createXPathFromElement(elm) {
    var allNodes = document.getElementsByTagName('*'); 
    for (segs = []; elm && elm.nodeType == 1; elm = elm.parentNode) 
    {  
        for (i = 1, sib = elm.previousSibling; sib; sib = sib.previousSibling) { 
            if (sib.localName == elm.localName)  i++; 
        };
        segs.unshift(elm.localName.toLowerCase() + '[' + i + ']'); 
    }; 
    return segs.length ? '//' + segs.join('//') : null; 
  }
</script>
</head>

<body>
<table>
    <tr>
        <td>
            <p>&#160;</p><h3><strong><span class="color-1">PRINCIPLES OF PATIENT CARE</span></strong></h3><p class="noindent"><strong><span class="color-1">Evidence-Based Medicine</span></strong> Evidence-based medicine refers to the concept that clinical decisions are formally supported by data, preferably data that are derived from prospectively designed, randomized, controlled clinical trials. This is in sharp contrast to anecdotal experience, which may often be biased. Unless they are attuned to the importance of using larger, more objective studies for making decisions, even the most experienced physicians can be influenced by recent encounters with selected patients. Evidence-based medicine has become an increasingly important part of the routine practice of medicine and has led to the publication of a number of practice guidelines.</p>
            <p>&#160;</p><p class="noindent"><strong><span class="color-1">Practice Guidelines</span></strong> Professional organizations and government agencies are developing formal clinical-practice guidelines to aid physicians and other caregivers in making diagnostic and therapeutic decisions that are evidence-based, cost-effective, and most appropriate to a particular patient and clinical situation. As the evidence base of medicine increases, guidelines can provide a useful framework for managing patients with particular diagnoses or symptoms. They can protect patients—particularly those with inadequate health care benefits—from receiving substandard care. Guidelines can also protect conscientious caregivers from inappropriate charges of malpractice and society from the excessive costs associated with the overuse of medical resources. There are, however, caveats associated with clinical practice guidelines since they tend to oversimplify the complexities of medicine. Furthermore, groups with differing perspectives may develop divergent recommendations regarding issues as basic as the need for periodic sigmoidoscopy in middle-aged persons. Finally, guidelines do not—and cannot be expected to—account for the uniqueness of each individual and his or her illness. The physician’s challenge is to integrate into clinical practice the useful recommendations offered by experts without accepting them blindly or being inappropriately constrained by them.</p>               
        </td>
    </tr>
    <tr>
        <td>
            Search for <input type="button" onclick="search(this)" value="Practice Guidelines" /> Success.
        </td>
    </tr>
    <tr>
        <td>
            Search for <input type="button" onclick="search(this)" value="Professional" /> Success.
        </td>
    </tr>
    <tr>
        <td>
            Search for <input type="button" onclick="search(this)" value="Practice Guidelines Professional" /> No result since text 'Practice Guidelines' is inside &lt;strong&gt;&lt;strong/&gt;, and  'Professional' is out side.     
        </td>
    </tr>
</table>
</body>
</html>

最佳答案

是的。您可以使用 normalize-space() 为了这。 (为了可读性换行)

var xpath = "
  //*[
    contains(
      translate(
        normalize-space(.), 
        'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 
        'abcdefghijklmnopqrstuvwxyz'
      ), 
      normalize-space('" + searchTxt.toLowerCase() + "')
    )
  ]
";

请注意,这将找到包含相应文本的节点的整个层次结构,直到并包括 <html>元素。我猜您会对“最深嵌套”节点感兴趣。

另请注意,您可能希望从 searchTxt 中删除单引号。因为它们会破坏 XPath 表达式。

关于javascript - 通过 HTML 标签进行 XPath 搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6812465/

相关文章:

javascript - 无法像 JS 中的对象一样从字典中获取值

java - 嵌套字符串中的 HTML 字符串

javascript - 如何在浏览器上运行text-to-svg?

javascript - 在 HTML 中获取输入并使用 javascript,对其进行处理并将新变量发送回 html

javascript - 我们如何使用 DOM 访问单选按钮的值?

html - 使用 xpath 获取下一个元素

用户输入电子邮件时用于电子邮件验证的 JavaScript 代码不起作用

php - 使用 DOM 抓取网站的标题

java - 用 xpath 替换对象列表中的值?

xml - XPath 选择元素但在结果中排​​除子元素