replace the text of an element with tag <bold>

New Here ,
Nov 06, 2015

Copy link to clipboard

Copied

Dear all,

I want to change the complete text of an element with a certain tag, say "<bold>".

I can get the tag name via elem.ElementDef.Name. But how do I get the text()-node of the Element.

Is there something like the xslt text()-node functionality? Some other ideas?

Thanks in advance

WA

TOPICS
Scripting

Views

534

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

1 Correct Answer

Mentor , Nov 06, 2015
Russ Ward Mentor , Nov 06, 2015
function replaceElementText(doc, elem, newText) {     //Define a new element range and set it up     //to represent the contents of the element.     var er = new ElementRange();     er.beg.parent = er.end.parent = elem;     er.beg.offset = 0;                 if(ov(elem.FirstChildElement))     {         er.beg.child = elem.FirstChildElement;         er.end.offset = 0;     }     else er.end.offset = Constants.FV_OBJ_END_OFFSET;            //Set the element range in the document and     //execu...

Likes

Translate

Translate
Jump to answer Jump to answer
Mentor ,
Nov 06, 2015

Copy link to clipboard

Copied

Hi WA,

Good question. There is no concept of a single text node within a FrameMaker element like there is for other DOM tools. But even if there was, there still would be no direct method to set the text. In FrameMaker, you have to deal with element ranges and text selection tools to complete this kind of operation.

Here is a function that replaces the text of the provided element. Hope this helps.

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Mentor ,
Nov 06, 2015

Copy link to clipboard

Copied

function replaceElementText(doc, elem, newText)

{

    //Define a new element range and set it up

    //to represent the contents of the element.

    var er = new ElementRange();

    er.beg.parent = er.end.parent = elem;

    er.beg.offset = 0;

           

    if(ov(elem.FirstChildElement))

    {

        er.beg.child = elem.FirstChildElement;

        er.end.offset = 0;

    }

    else er.end.offset = Constants.FV_OBJ_END_OFFSET;   

   

    //Set the element range in the document and

    //execute Clear() to delete the current element contents.

    doc.ElementSelection = er;   

    doc.Clear(0);

   

    //The insertion point will now be at the beginning of

    //the empty element. Execute AddText() to insert the

    //new text.

    doc.AddText(doc.TextSelection.beg, newText);  

}

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Mentor ,
Nov 06, 2015

Copy link to clipboard

Copied

WA,

I just noticed one thing in my code that won't work for you, that ov() call. That is a personal shortcut to determine the validity of an object. Here is the code:

function ov(obj)

{

    if(obj == null) return false;

    else return obj.ObjectValid();

}

Russ

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more