How to highlight a string in xml document?

Having trouble deploying Oxygen XML Web Author? Got a bug to report? Post it all here.
kyu
Posts: 5
Joined: Mon Jul 24, 2023 9:47 pm

How to highlight a string in xml document?

Post 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
cristi_talau
Posts: 496
Joined: Thu Sep 04, 2014 4:22 pm

Re: How to highlight a string in xml document?

Post 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
kyu
Posts: 5
Joined: Mon Jul 24, 2023 9:47 pm

Re: How to highlight a string in xml document?

Post 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
highlight_error_snip.JPG (51.03 KiB) Viewed 1181 times
Could you please help me find the correct syntax?

Thank you,
Karen
mihaela
Posts: 490
Joined: Wed May 20, 2009 2:40 pm

Re: How to highlight a string in xml document?

Post 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
Mihaela Calotescu
http://www.oxygenxml.com
kyu
Posts: 5
Joined: Mon Jul 24, 2023 9:47 pm

Re: How to highlight a string in xml document?

Post 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
image.png (8.79 KiB) Viewed 1160 times
Bogdan Dumitru
Site Admin
Posts: 142
Joined: Tue Mar 20, 2018 5:28 pm

Re: How to highlight a string in xml document?

Post 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
Bogdan Dumitru
http://www.oxygenxml.com
kyu
Posts: 5
Joined: Mon Jul 24, 2023 9:47 pm

Re: How to highlight a string in xml document?

Post 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?
mihaela
Posts: 490
Joined: Wed May 20, 2009 2:40 pm

Re: How to highlight a string in xml document?

Post 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
Mihaela Calotescu
http://www.oxygenxml.com
kyu
Posts: 5
Joined: Mon Jul 24, 2023 9:47 pm

Re: How to highlight a string in xml document?

Post by kyu »

Thank you, that worked in highlighting.
Post Reply