簡體   English   中英

document.evaluate不返回正確的TextNodes XPath

[英]document.evaluate does not returns proper TextNodes XPath

我在WebView中為Android創建“Highlighter”。 我通過函數獲取HTML中所選范圍的XPath表達式,如下所示

/ HTML [1] / BODY [1] / DIV [1] / DIV [3] / DIV [1] / DIV [1] /文本()[5]

現在我在javascript中通過這個函數評估上面的XPath表達式

var resNode = document.evaluate('/HTML[1]/BODY[1]/DIV[1]/DIV[3]/DIV[1]/DIV[1]/text()[5]',document,null,XPathResult.FIRST_ORDERED_NODE_TYPE ,null);
var startNode = resNode.singleNodeValue;

但我得到的startNode為'null'。

但是,這是有趣的一點:

如果我使用相同的函數評估'/ HTML [1] / BODY [1] / DIV [1] / DIV [3] / DIV [1] / DIV [1]' XPath表達式,它會給出正確的節點,即'DIV'。

兩個XPath之間的區別在於前面的包含textNode,后來只有div。

但同樣的事情在桌面瀏覽器上運行良好。

編輯樣本HTML

<html>
<head>
<script></script>
</head>
<body>
<div id="mainpage" class="highlighter-context">
<div>       Some text here also....... </div>
<div>      Some text here also.........</div>
<div>
  <h1 class="heading"></h1>
  <div class="left_side">
    <ol></ol>
    <h1></h1>
    <div class="text_bio">
    In human beings, height, colour of eyes, complexion, chin, etc. are 
    some recognisable features. A feature that can be recognised is known as 
    character or trait. Human beings reproduce through sexual reproduction. In this                
    process, two individuals one male and another female are involved. Male produces   
    male gamete or sperm and female produces female gamete or ovum. These gametes fuse 
    to form zygote which develops into a new young one which resembles to their parent. 
     During the process of sexual reproduction 
    </div>
  </div>
  <div class="righ_side">
  Some text here also.........
  </div>
  <div class="clr">
         Some text here also.......
  </div>
</div>
</div>
</body>
</html>

得到XPath:

var selection = window.getSelection(); 
var range = selection.getRangeAt(0); 
var xpJson = '{startXPath :"'+makeXPath(range.startContainer)+      
             '",startOffset:"'+range.startOffset+
             '",endXPath:"'+makeXPath(range.endContainer)+ 
             '",endOffset:"'+range.endOffset+'"}';

制作XPath的功能:

function makeXPath(node, currentPath) {
          currentPath = currentPath || ''; 
          switch (node.nodeType) { 
          case 3:
          case 4:return makeXPath(node.parentNode, 'text()[' + (document.evaluate('preceding-sibling::text()', node, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null).snapshotLength + 1) + ']');
          case 1:return makeXPath(node.parentNode, node.nodeName + '[' + (document.evaluate('preceding-sibling::' + node.nodeName, node, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null).snapshotLength + 1) + ']' + (currentPath ? '/' + currentPath : ''));
          case 9:return '/' + currentPath;default:return '';
    }
}

我不是在使用XML,而是在webview中使用HTML。

我嘗試使用Rangy序列化和反序列化,但Rangy“Serialize”正常工作但不是“反序列化”。

任何想法的家伙,哪里出錯了?

UPDATE

終於得到了問題的根本原因(尚未解決:()

` android webview中到底發生了什么? - >>不知何故,android webview正在改變加載的HTML頁面的DOM結構。 盡管DIV不包含任何TEXTNODES,但在從DIV中選擇文本時,我正在為該DIV中的每一行獲取TEXTNODE。 例如,對於桌面瀏覽器中的相同HTML頁面和相同的文本選擇,從webview獲取的XPath與在桌面瀏覽器中給出的完全不同


XPath from Desktop Browser:
startXPath /HTML[1]/BODY[1]/DIV[1]/DIV[3]/DIV[1]/DIV[1]/text()[1]
startOffset: 184 
endXPath: /HTML[1]/BODY[1]/DIV[1]/DIV[3]/DIV[1]/DIV[1]/text()[1]
endOffset: 342

Xpath from webview:
startXPath :/HTML[1]/BODY[1]/DIV[1]/DIV[3]/DIV[1]/DIV[1]/text()[3]
startOffset:0 
endXPath:/HTML[1]/BODY[1]/DIV[1]/DIV[3]/DIV[1]/DIV[1]/text()[4]
endOffset:151

在您的示例中,路徑/HTML[1]/BODY[1]/DIV[1]/DIV[3]/DIV[1]/DIV[1]/text()[5]選擇第五個文本子節點div元素

<div class="text_bio">
In human beings, height, colour of eyes, complexion, chin, etc. are 
some recognisable features. A feature that can be recognised is known as 
character or trait. Human beings reproduce through sexual reproduction. In this                
process, two individuals one male and another female are involved. Male produces   
male gamete or sperm and female produces female gamete or ovum. These gametes fuse 
to form zygote which develops into a new young one which resembles to their parent. 
 During the process of sexual reproduction 
</div>

div有一個文本子節點,所以我不明白為什么text()[5]應該選擇任何東西。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM