Pages: 1
Print
Author Topic: Why is XMetaL unable to locate a text node?  (Read 981 times)
spyro
Member

Posts: 35


« on: March 11, 2011, 11:12:48 AM »

Hello,

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


« Last Edit: March 28, 2011, 02:46:52 AM by spyro » Logged
Derek Read
Program Manager (XMetaL)
Administrator
Member

Posts: 1546



WWW
« Reply #1 on: March 16, 2011, 04:40:12 PM »

The 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.
Logged
Derek Read
Program Manager (XMetaL)
Administrator
Member

Posts: 1546



WWW
« Reply #2 on: March 16, 2011, 05:06:34 PM »

If 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);
Logged
spyro
Member

Posts: 35


« Reply #3 on: March 21, 2011, 10:50:43 AM »

Thank you Derek!

Thanks to your help I was able to complete this task. :)

Greetings,
spyro
Logged
Pages: 1
Print
Jump to:  

email us