I need to get a handle on the element given the path to the text node or Xpath.
I use the following method to return the element.
result.singleNodeValue;
returns null if there are multiple childNodes present, can I merge them into one?
function xpathElement(expr) {
var resolver = function (prefix) {
if ("xhtml" == prefix) {
return "http://www.w3.org/1999/xhtml";
}
}
var result = document.evaluate(expr, document, resolver, 9, null)
console.log("function xpathElement: possible multiple nodes:");
console.log(result);
result = result.singleNodeValue;
console.log("function xpathElement: singleNodeValue:");
console.log(result);
return result;
}
Works:
When I pass in the following it returns a text node fine
/xhtml:html[0001]/xhtml:body[0001]/xhtml:div[0002]/xhtml:div[0001]/text()[0001]
Does not work:
When I pass in the following it returns null
/xhtml:html[0001]/xhtml:body[0001]/xhtml:div[0002]/text()[0003]
Here is the html I am using
<p class="calibre1">
<a id="itr"></a>
</p>
<div class="fmhT">
<b class="calibre3">INTRODUCTION</b>
</div>
<div class="fmtx">
Between 1843 and 1848, Dickens wrote five novellas or long short stories that he
published at Christmastime (<i class="calibre6">A Christmas Carol</i>, The Chimes, The
Cricket on the Hearth, The Battle of Life, and The Haunted Man and the Ghost’s Bargain).
The stories are not merely set at Christmas or the New Year’s holiday but contain themes
the author felt were particularly appropriate to the season. While Christmas celebrations
predate Dickens and there existed before him a tradition of telling ghost-tales at
Christmas and the turn of the year, Dickens breathed a new and unique vigor into these
celebrations and traditions that carry forward to this day. He wrote other ghost stories,
almost all of which are spoofs or farces, but in his “Christmas books” allowed
supernatural elements a power to awaken characters and readers from their social
misanthropy.
</div>
It seems this element <i class="calibre6">A Christmas Carol</i>
is splitting the text node into 3 parts
Child nodes for <div class="fmtx">
node 1
"Between 1843 and 1848, Dickens wrote five novellas or long short stories that he published at Christmastime ("
node 2
"A Christmas Carol"
node 3
", The Chimes, The Cricket on the Hearth, The Battle of Life, and The Haunted Man and the Ghost’s Bargain). The stories are not merely set at Christmas or the New Year’s holiday but contain themes the author felt were particularly appropriate to the season. While Christmas celebrations predate Dickens and there existed before him a tradition of telling ghost-tales at Christmas and the turn of the year, Dickens breathed a new and unique vigor into these celebrations and traditions that carry forward to this day. He wrote other ghost stories, almost all of which are spoofs or farces, but in his “Christmas books†allowed supernatural elements a power to awaken characters and readers from their social misanthropy.
"
How do i combine these text nodes as one?
I have tried
$('.fmtx').normalize();
with no luck, still shows 3 childNodes.
Any help would be much appreciated, thanks!
Update
I have added a jsfiddle, maby this will make more sense, http://jsfiddle.net/95Bc7/
keep and eye on console log at the bottom for +++++++++++++++++ ancestor xpath element: null
1) Make a selection before the italic text
+++++++++++++++++ ancestor xpath element: [object Text]
2) Make a selection on the italic text
+++++++++++++++++ ancestor xpath element: [object Text]
3) Make a selection after the italic text
+++++++++++++++++ ancestor xpath element: null