General XMetaL Discussion
XMetaL Community Forum › General XMetaL Discussion › Why is XMetaL unable to locate a text node?
-
spyro March 11, 2011 at 5:12 pm
Why is XMetaL unable to locate a text node?
March 11, 2011 at 5:12 pmParticipants 2Replies 3Last Activity 12 years agoHello,
I have to select a specific textnode in XMetaL 5.5 with a very simple xpath statement like this:
(//text())[123]
So i tried
activedocument.getNodesByXPath(“//text()[123]”).item(0).parentNode.selection.SelectNodeContents
for
“Select the 123th textnode of this document.”
This wasn't working. For testing purposes I tried
msgbox activedocument.getNodesByXPath(“//text()”).length
The return value for this statement is 0, but this isn't correct. It has to be 4815 in my case because this specific document has 4815 text nodes. It seems like XMetaL isn't able to adress text nodes at all?!
spyro
Derek Read March 16, 2011 at 10:40 pm
Reply to: Why is XMetaL unable to locate a text node?
March 16, 2011 at 10:40 pmThe XPath “tests” for comment(), processing-instruction(), and node() will work but not text(), which is not supported in versions up to and including 6.0:
//XMetaL Script Language JScript:
var doc = ActiveDocument;
var ndsText = doc.getNodesByXPath("//text()");
var ndsPI = doc.getNodesByXPath("//processing-instruction()");
var ndsComment = doc.getNodesByXPath("//comment()");
var ndsNode = doc.getNodesByXPath("//node()");
var msg = "";
msg += "Text:" + ndsText.length;
msg += "nPI:" + ndsPI.length;
msg += "nComment:" + ndsComment.length;
msg += "nNode:" + ndsNode.length;
Application.Alert(msg);What specifically are you trying to do?
It sounds like you need to find text node # 123 (I'm not sure where that number comes from) and move the selection to that position in the document.I think this problem may need to be tackled using an entirely different strategy than the one you are trying, but knowing exactly what you are attempting to do, and perhaps how you are creating the value 123, may help.
Derek Read March 16, 2011 at 11:06 pm
Reply to: Why is XMetaL unable to locate a text node?
March 16, 2011 at 11:06 pmIf the answer to my question is “yes” (ie: “you need to find text node # 123 and move the selection to that position in the document”) then you can try something like this:
//XMetaL Script Language JScript:
var textNodeCount = 0;
var rng = ActiveDocument.Range;function readtree(node,cnt) {
//Do this for text nodes only
if (node.nodeType==3) {
textNodeCount++;
if(textNodeCount == cnt) {
rng.SelectNodeContents(node);
rng.Select();
}
}// Process this node's children
if (node.hasChildNodes()) {
readtree(node.firstChild,cnt);
}// Continue with this node's siblings
if (node.nextSibling!=null) {
readtree(node.nextSibling,cnt)
}
}
readtree(ActiveDocument.DocumentElement,123); -
AuthorPosts
- You must be logged in to reply to this topic.