Skip to main content
frameexpert
Community Expert
Community Expert
April 13, 2015
Question

Text nodes in a structured FrameMaker file

  • April 13, 2015
  • 1 reply
  • 781 views

Hello Scripters,

I have a recursive function that touches every element in a structured FrameMaker file. I am trying to find out how to get text nodes as I process the elements.

#target framemaker

var doc = app.ActiveDoc;

var element = doc.MainFlowInDoc.HighestLevelElement;

processElements (element);

function processElements (element) {

   

    if (element.ElementDef.ObjectValid()) {

        $.writeln (element.ElementDef.Name);

    }

    else {

        // I thought I could get text nodes here, but it never gets here.

    }

    var element2 = element.FirstChildElement;

    while(element2.ObjectValid()) {

        processElements (element2);

        element2 = element2.NextSiblingElement;

    }

}

I expected that I would get the text nodes on line 14, but it the else statement never gets there. In FrameScript, if an element has no ElementDef, then it is a text node. I am not sure about the FDK. Any ideas how I can pick up the text nodes in a function like this? Thanks.

-Rick

This topic has been closed for replies.

1 reply

Inspiring
April 13, 2015

Hi Rick,

Here is how I test for a text node. I don't remember how I came to this methodology, but it seems to be reliable.

Russ

function elem_IsTextNode(elem)

{

    if(elem.ElementDef.Name == null || elem.ElementDef.Name == "") return true;

    else return false;

}

frameexpert
Community Expert
Community Expert
April 13, 2015

Hi Russ, Thanks for the reply. I am not sure why, but my function is still not hitting any text nodes. -Rick

#target framemaker

var doc = app.ActiveDoc;

var element = doc.MainFlowInDoc.HighestLevelElement;

processElements (element);

function processElements (element) {

    

    if (element.ElementDef.Name != null && element.ElementDef.Name != "") {

        $.writeln (element.ElementDef.Name);

    }

    else {

        // I thought I could get text nodes here, but it never gets here.

        $.writeln ("************");

    }

    var element2 = element.FirstChildElement;

    while(element2.ObjectValid()) {

        processElements (element2);

        element2 = element2.NextSiblingElement;

    }

}

www.frameexpert.com
Legend
April 13, 2015

Hi Rick,

Well, I didn't look much past the test for ElementDef. But with a closer analysis, I see what may be a problem, depending on the document setup... You are starting at the root element, then stepping down through all siblings on the root element branch. It would be very unusual for there to be any text nodes on a root element branch, although I guess not impossible. If you are trying to walk the entire tree out to the ends of branches, maybe try:

element2 = element2.NextElementDFS;

...instead. That will do a full tree walk and I think it will stop at text nodes.

Russ