Page 1 of 1

How to highlight a string in xml document?

Posted: Mon Jul 24, 2023 10:13 pm
by kyu
Hi,
I have an xml document displayed in the oxygen xml editor and know the referenceGuid of the string I need to highlight.

Basically the reverse of what's asked in this thread but high lighting all the words inside the tag when I have the referenceGuid:
common-problems-f34/topic24987.html

Thank you,
Karen

Re: How to highlight a string in xml document?

Posted: Tue Jul 25, 2023 9:57 am
by cristi_talau
Hello,

What I understand is that you have the value of the "referenceGuid" of an attribute and you want to select the text inside the element.
If you are writing code inside a framework or plugin you can use the JS API: Best,
Cristian

Re: How to highlight a string in xml document?

Posted: Tue Aug 01, 2023 4:47 am
by kyu
Hi Christian,
I'm using the following code and getting the following error (I am getting elements back in the getElementsByTagName call):

Code: Select all

var editingSupport = editorGlobal.getEditingSupport();
var elements = editingSupport.getDocument().getElementsByTagName("reference");
var selectionManager = editorGlobal.getSelectionManager();
var selection = selectionManager.createAroundNode(elements.item(0));
editorGlobal.getEditingSupport().getPersistentHighlightsManager().addHightlight(null);
highlight_error_snip.JPG
Could you please help me find the correct syntax?

Thank you,
Karen

Re: How to highlight a string in xml document?

Posted: Tue Aug 01, 2023 9:59 am
by mihaela
Hello,

Maybe there is no 'reference' element in the document. Use console.log to print the resulting elements, to see if this is the case.

Best Regards,
Mihaela

Re: How to highlight a string in xml document?

Posted: Tue Aug 01, 2023 5:41 pm
by kyu
I didn't include the console.log statements in the code snippet. This is the output for document and elements. It looks like it is returning the two reference elements I have.
image.png

Re: How to highlight a string in xml document?

Posted: Wed Aug 02, 2023 2:32 pm
by Bogdan Dumitru
Hello,

From the "Uncaught TypeError: Cannot read properties of undefined (reading 'getType')" it seems that you call SelectionManager.createAroundNode() with an undefined object as parameter. Please double-check that.
Also notice that the objects printed in the console from the last screenshot are sync.api.dom.Element objects, not HTMLElement objects.

If you consider that's a bug in one of the JavaScript API methods from Oxygen XML Web Author please provide the following:
  • the exact method that doesn't respect its documentation or that has a bug
  • a script that we can run to reproduce the problem
  • the expected behavior
  • the actual behavior

Re: How to highlight a string in xml document?

Posted: Fri Aug 04, 2023 12:41 am
by kyu
This is one of the reference elements in the xml.

<description><reference referenceGuid="ffa25acc-4af5-4e13-9230-8d2255932be3">Two</reference> </description>

And this is the code with the console logs.

Code: Select all

editorGlobal = e.editor;
var editingSupport = editorGlobal.getEditingSupport();
console.log("document: ", editingSupport.getDocument());
var elements = editingSupport.getDocument().getElementsByTagName("reference");
console.log("elements: ", elements);
var selectionManager = editorGlobal.getSelectionManager();
var selection = selectionManager.createAroundNode(elements.item(0));
console.log("selection: ", selection);
editorGlobal.getEditingSupport().getPersistentHighlightsManager().addHightlight(null);
Could you please confirm this is the correct syntax for adding highlights?

Re: How to highlight a string in xml document?

Posted: Fri Aug 04, 2023 9:11 am
by mihaela
Hello,

Here are the changes you must do in your code, to add highlights to the reference elements:
1. Set the selection created around the reference node: after obtaining the selection from the createAroundNode method, you must call the setSelection API.
2. There is a typo in your code (addHightlight must be changed to addHighlight).
Also, make sure you first check if the list of elements is not empty (so that the "Cannot read properties of undefined (reading 'getType')" error wont happen again).


Best Regards,
Mihaela

Re: How to highlight a string in xml document?

Posted: Sat Aug 26, 2023 1:04 am
by kyu
Thank you, that worked in highlighting.