开发者

Opera JS: XPath to find links that contain a path

I'm using Opera 11.50 and I have a JavaScript-function that works as a callback of an Ajax-request:

function nextPageCallback(responseText, responseXML) {
    if (responseXML && responseXML.documentElement) doc = responseXML.documentElement;
    else {
        doc = document.createElement("html");
        doc.innerHTML = responseText.replace(/[\n\r]/g, "").replace(/^.*\<html.*?\>|\<\/html.*$/i, "");
    }
    table = doc.getElementsByTagName("table")[1];

    if (table) {
        var trs;

        trs = document.evaluate("descendant::tr[count(td) > 1]", table, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);

        for (var pos = 0; pos < trs.snapshotLength; pos++) {
            var tr = trs.snapshotItem(pos);
            var link;

            link = tr.innerHTML.match(/href=["'](\/path\/[^"']+?)["']/i)[1];
            linkX = document.evaluate("descendant::a开发者_开发技巧[contains(@href, '/path/')]", tr, null, XPathResult.STRING_TYPE, null);
        }
    }
}

The main idea is to parse the table for certain links and then do something with the A- and TR-elements (further code is pointless for this issue and thus omitted).

The assignment to trs works, there are elements in the snapshot. The TRs will be moved around later so I need a snapshot.

The problem is with the last two lines. link is assigned by using plain old (error prone) RegEx and up to now works since there has been only one link that contains "/path/". Now linkX should end up with the same value. If you wonder why there's STRING_TYPE, well, nothing else works, not even ANY_TYPE (they all cause an XPathException: TYPE_ERR).

linkX is set to what seems to be the link text or innerText of the A-element. However, I want the href-attribute.

I already tried various things like using a namespace-resolver

function nsr() {
    return "http://www.w3.org/1999/xhtml";
}
:
            linkX = document.evaluate("descendant::a[contains(@href, '/path/')]", tr, nsr, XPathResult.STRING_TYPE, null);

but it isn't even called.

I also added more stuff at the end of the XPath like /parent::node(), but nothing works.

Does it have something to do that I'm using a DOM-structure that was returned by document.evaluate()? Is my XPath wrong? How do I fix this?


EDIT: As requested, here's a sample response that appears in responseText.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  </head>
  <body>
    <table>
      <thead>
        <tr>
          <th>Type</th>
          <th>Id</th>
          <th>Name</th>
          <th>Serialnumber</th>
        </tr>
      </thead>
      <tr>
        <td>Media</td>
        <td><a href="/path/2355">2355</a></td>
        <td>Sampler</td>
        <td>A65270-D32</td>
      </tr>
      <!-- more TRs of the structure above -->
      <tr>
        <td colspan="4" style="text-align:center;">
          <a href="/list/1">Previous</a>&nbsp;
          <a href="/list/3">Next</a>&nbsp;
        </td>
      </tr>
    </table>
  </body>
</html>

Regarding the code above it executes the else:

else {
    doc = document.createElement("html");
    doc.innerHTML = responseText.replace(/[\n\r]/g, "").replace(/^.*\<html.*?\>|\<\/html.*$/i, "");
}

What the code should do is find all TRs and in the TR the link containing "/path/". For further processing I need the TR-element and the href-attribute of A.


What the code should do is find all TRs and in the TR the link containing "/path/". For further processing I need the TR-element and the href-attribute of A.

This XPath expression selects the wanted nodes:

 //x:tr[//x:a[contains(@href, '/path/')]]
|
 //x:tr//x:a/@href[contains(.,'/path/')] 

Here the prefix x must be associated to the (default) namespace "http://www.w3.org/1999/xhtml".

Please, do note that XML and XPath are case-sensitive -- in the provided XML document there are no element with upper-case names.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜