Page 1 of 1

Manipulating XML in text mode

Posted: Fri Nov 06, 2020 12:04 am
by jakubsimek
Hi,

I am trying to figure out how I could write a plugin action for the text mode which would manipulate some elements or attributes. As far as I can see, the sample plugins in the SDK only demonstrate string manipulation for the text mode (e.g. capitalizing).

The "TextDocumentController" which can be used on "WSXMLTextEditorPage" only has a method for inserting an XML fragment.

Suppose I would like to insert an attribute on a certain element which is already present in the document. Could i use the "evaluateXPath" method on "WSXMLTextEditorPage" to return the element I want to manipulate? The method returns an array of java.lang.Object. Does this mean that in case the items returned by "evaluateXPath" are XML element nodes, I could then manipulate them "in place" as instances of something like "org.w3c.dom.Element"?

Would something like this be the way to go or are there other ways in the API to manipulate XML nodes in text mode?

I am a bloody beginner in the Oxygen SDK, so sorry if I am asking something which is perhaps obvious.

Best wishes,
Jakub

Re: Manipulating XML in text mode

Posted: Fri Nov 06, 2020 8:47 am
by Radu
Hi Jakub,

This is a good question, our API on the Text editing mode side is more primitive than the API for the Author visual editing mode. Partly this is because in the Text mode you might not have wellformed XML, so we cannot create a nodes structure over it.
Basically the Text page is a Swing JTextArea, and you can gain access to its internal document ro.sync.exml.workspace.api.editor.page.text.WSTextEditorPage.getDocument() if you want to perform insertions and deletions other than what our API allows.
This API gives you ranges of elements ro.sync.exml.workspace.api.editor.page.text.xml.WSXMLTextEditorPage.findElementsByXPath(String). Each range has start line/column and end line/column information. After this you can use our API ro.sync.exml.workspace.api.editor.page.text.WSTextEditorPage.getOffsetOfLineStart(int) to convert line/column information to an offset in the document and thus obtain the offset range beween which an element exists, in this way you can retrieve the entire element content in a string, maybe manipulate it and then insert it back in the document.

Regards,
Radu

Re: Manipulating XML in text mode

Posted: Fri Nov 06, 2020 9:36 am
by jakubsimek
Hi Radu,

Thanks a lot for this explanation. May I ask back about the evaluateXPath() method on WSXMLTextEditorPage? What kind of objects are being returned by this method if I e.g. address element nodes by the XPath expression?

Best,
Jakub

Re: Manipulating XML in text mode

Posted: Fri Nov 06, 2020 9:50 am
by Radu
Hi Jakub,

The "evaluateXPath" should return an array of DOM "org.w3c.dom.Node". You can use it more to interrogate if certain XML elements are found and what they contain. Making changes to the DOM nodes will not update the XML content.

Regards,
Radu

Re: Manipulating XML in text mode

Posted: Fri Nov 06, 2020 11:03 am
by jakubsimek
OK, thank you very much for explaining this.
Best,
Jakub