Skip to main content
1-Visitor
November 23, 2015
Solved

Unable to read element by element

  • November 23, 2015
  • 1 reply
  • 5416 views

HI Team,

             I have one requirement in my current project, which I need to replace some customized words in the elements where it is present in whole document.The below peace of code facing some issues on replacing the words.

function checkWord(word,color){

local rtn = 1;

  local logmsg = oid_first();

# Set disable continue search

  set wrapscan = off;

   # Cursor goto root of document

  goto_oid(oid_caret());

  # Find word & changing text color

  while(rtn == 1) {

  rtn = find(word, 0x0008|0x0200|0x0020);

  if(rtn == 1) {

  selectText = main::selection;

  EditDelete;

  str = '<?Pub _font FontColor="'.$color.'" ?>'.selectText.'<?Pub /_font?>';

#insert(str);

  local findWithin; findWithin = V['FindWithin'];

  local replFlags = flags|0x0020

  replace(word, insert(str), replFlags, findWithin, current_doc())

  }

}

  }

example doc : -

<SimpleText>

  <para>Be aware of the fire hazard, especially when working near flammable substances or vapours</para>

  <para2>I am WelCome you vapours</para2>

</SimpleText>

After Replacing the out put the doc

<SimpleText>

  <para>Be aware of the fire hazard, especially when working near flammable substances or vapours</para>

  <para2>I am WelCome you 1</para2> // here word vapours is replaced with one.

</SimpleText>

Could you please any one do some need full help on this.

Thanks

Prashant

    Best answer by ClayHelberg

    To limit the search to full words, change the flag in the replace() function from 0x2010 to 0x2018. (You can find a full description of the flag parameter in the online documentation for the replace() function.)

    --C

    1 reply

    18-Opal
    November 23, 2015

    Hi Prashant--

    You're really overthinking this. The built-in replace() function will handle the entire operation you are trying to accomplish.Try something like this:

         replace("vapours",'<?Pub _font FontColor="Red"?>vapours<?Pub /_font?>",0x2010,"para2");

    By this, I mean replace *all* of your code above with this single line. If I read your description correctly, this should do everything you need modifying all instances of "vapours" within <para2> throughout the document.

    --Clay

    prao1-VisitorAuthor
    1-Visitor
    November 23, 2015

    Thanks for your immediate response, If you see the code which the function checkWord(word,color) contains two arguments I am passing word and colour  are dynamic values , every time the value will not same just like "vapours". As you suggested in the mail should I pass dynamic value as  "vapours" in the line replace("vapours",'<?Pub _font FontColor="Red"?>vapours<?Pub /_font?>",0x2010,"para2"); but here para2 will not be very time the same it could be any thing there then how should I handle it.

    thanks,

    prashant

    18-Opal
    November 23, 2015

    Hi Prashant--

    Yes, of course, those are parameters you can modify as you need to, or use variable values for as you normally would. For convenience, you can define a custom function to simplify the parameterization. For example, you could write a function like this:

    function highlightWord(word, color, context) {

      replace(word,'<?Pub _font FontColor="' . color . '"?>' . word . "<?Pub /_font?>",0x2010, context);

    }

    Then you just call this function and pass it the text you want to replace (the "word" parameter), the color you want to make it, and the name of the element you want to search within (the "context" parameter).

    --Clay