Note that there are some explanatory texts on larger screens.

plurals
  1. POdocument.evaluate does not returns proper TextNodes XPath
    primarykey
    data
    text
    <p>I am creating "Highlighter" for Android in WebView. I am getting XPath expression for the selected Range in HTML through a function as follows</p> <blockquote> <p><strong>/HTML[1]/BODY[1]/DIV[1]/DIV[3]/DIV[1]/DIV[1]/text()[5]</strong></p> </blockquote> <p>Now i am evaluating the above XPath expression through this function in javascript</p> <pre><code>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; </code></pre> <p>but I am getting the startNode 'null'.</p> <p>But, here is the interesting point:</p> <p>if I evaluate this <strong>'/HTML[1]/BODY[1]/DIV[1]/DIV[3]/DIV[1]/DIV[1]'</strong> XPath expression using the same function, it gives the proper node i.e. a 'div'.</p> <p>The difference between the two XPaths is the previous ones contains a textNode and later only div.</p> <p>But the same thing is working fine on Desktop browsers.</p> <p><strong>Edited</strong> Sample HTML</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;script&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="mainpage" class="highlighter-context"&gt; &lt;div&gt; Some text here also....... &lt;/div&gt; &lt;div&gt; Some text here also.........&lt;/div&gt; &lt;div&gt; &lt;h1 class="heading"&gt;&lt;/h1&gt; &lt;div class="left_side"&gt; &lt;ol&gt;&lt;/ol&gt; &lt;h1&gt;&lt;/h1&gt; &lt;div class="text_bio"&gt; 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 &lt;/div&gt; &lt;/div&gt; &lt;div class="righ_side"&gt; Some text here also......... &lt;/div&gt; &lt;div class="clr"&gt; Some text here also....... &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p><strong>getting XPath:</strong></p> <pre><code>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+'"}'; </code></pre> <p><strong>function to make XPath:</strong></p> <pre><code>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 ''; } } </code></pre> <p>I am not working with XML but with HTML in webview.</p> <p>I tried using Rangy serialize and deserialize but the Rangy "Serialize" works properly but not the "deserialize".</p> <p>Any ideas guys, whats going wrong?</p> <p><strong>UPDATE</strong></p> <p>Finally got the root cause of the problem (not solution yet :( )</p> <p>`<strong>what exactly is happening in android webview. -->> Somehow, the android webview is changing the DOM structure of the loaded HTML page. Even though the DIV doesn't contains any TEXTNODES, while selecting the text from DIV, i am getting TEXTNODE for every single line in that DIV. for example, for the same HTML page in Desktop browser and for the same text selection, the XPath getting from webview is entirely different from that of given in Desktop Browser</strong>'</p> <hr> <pre><code>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 </code></pre> <hr> <pre><code>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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload