Page 1 of 1

Selected text from Author to xmlfragment

Posted: Wed Sep 23, 2015 1:55 pm
by ronaldo30
Hello,

I have a problem with the selected text after the serialization.

I use for that:

Code: Select all

if(authorAccess.getEditorAccess().hasSelection()) {
AuthorDocumentController documentController = authorAccess.getDocumentController();
AuthorDocumentFragment selectionAsAFragment = documentController.createDocumentFragment(
authorAccess.getEditorAccess().getSelectionStart(), authorAccess.getEditorAccess().getSelectionEnd());
String selectionAsXML = documentController.serializeFragmentToXML(selectionAsAFragment);

//Deletes the selection
authorAccess.getEditorAccess().deleteSelection();

//Process the selectionAsXML fragment, modify it.
//................

//Insert the XML fragment back at caret position.
documentController.insertXMLFragment(selectionAsXML, authorAccess.getEditorAccess().getCaretOffset());
}
I have the selection from the original xml like:

Code: Select all

<container>
<format>text</format>
</container>
After the serializeFragmentToXML the text will be:

String selectionAsXML -> "<container>[space]<format>text</format>[space]</container>"

I have [space] in the text. That isn't correct for me because I want make some changes on text.

How can I get the serialized text like the original text selected without spaces, like ?

String selectionAsXML -> "<container><format>text</format></container>"

Thanks!
Calin

Re: Selected text from Author to xmlfragment

Posted: Wed Sep 23, 2015 4:38 pm
by Radu
Dear Calin,

So the original XML content looks like this, right?

Code: Select all

<container>
<format>text</format>
</container>
That line break that you have before the <format> element is still considered to be a whitespace and when the XML is loaded in the Author editing mode it gets converted to a single space.
Does the DTD/XML Schema associated to the XML specify that the <container> element has element-only content (without the possibility to have text between the <format> elements for example)? If it would, then Oxygen would remove that extra space when presenting the document in the Author editing mode.

Regards,
Radu

Re: Selected text from Author to xmlfragment

Posted: Wed Sep 23, 2015 11:33 pm
by ronaldo30
Thank you Radu for very rapid answer.
I knew your solution.

Unfortunately <container> may contain PCDATA. That xml fragment is like:
1.

Code: Select all


<div>text1 <b>text2 text3</b> text4</div>
2.

Code: Select all


<div>
text1 <b>text2 text3</b> text4
</div>
So, after documentController.serializeFragmentToXML the text will be:
1. -> <div>text1 <b>text2 text3</b> text4</div>
2. -> <div>[space]text1 <b>text2 text3</b> text4[space]</div>

The resulted texts are different from the same original selection.

A workaround can be to get the fragment from original xml but I can't get the selected nodes into original xml.
Maybe with: java.lang.String getXPathExpression(int offset) get the xpath from author mode and will apply that to the original xml. I don't know.

What do you think is possible a workaround for that ?

Thanks!
Calin

Re: Selected text from Author to xmlfragment

Posted: Thu Sep 24, 2015 10:31 am
by Radu
Hi Calin,

Just to see that I understand your example:

So in the first case there is no space or line break between the <div> and the text1. And in the second case you have a line break between them.
When the edited content the Author mode is obtained by normalizing all spaces so that line break will be converted to a space.
There is no possibility for you to obtain the XML content exactly as it was from the Author API other than setting the <div> element as space preserve in the CSS, something like:

Code: Select all

div  {
white-space:pre;
}
I'm afraid I still do not understand your original use case, why you need the serialization of the AuthorDocumentFragment to be precisely what the XML contained and not an equivalent of that.

Regards,
Radu