suroundInFragment method
Oxygen general issues.
-
- Posts: 13
- Joined: Tue Mar 16, 2021 10:01 pm
suroundInFragment method
Hi,
I have the following TEI XML structure (simplified)
I want to surround the mixed content (Roman numerals mixed with some other symbols and elements) with a <num> element:
This needs to be done programmatically from an authorOperation, as the value of the numeral and other stuff is computed and inserted.
I try to insert <num> by using
Which surrounds the <label> element with an <num> element. To do it the other way around, I tried to compute the (changing) length of the <label> start tag and add the value to the labelNode.getStartOffset(). Then the fixed length of the closing tag </label> = 8 is subtracted from the EndOffset.
This is the line I use for the insertion:
This is how I compute the starting tag length somewhere earlier in the code (in a for loop looping through a nodelist found by xpath):
If I use this operation in Oxygen, it only works if I leave the elements offset untouched, producing <num><label></label></num>. The moment I add and subtract the computed offset, the action does something (because oxygen freezes for a second), but produces no outcome or error message whatsoever.
Questions:
- Is there a more elegant way to do this using oxygenxml API?
- Why does my code have no effect?
Any help would be greatly appreciated (as always!)
Best,
Michael
I have the following TEI XML structure (simplified)
Code: Select all
<list>
<item><label someAtr="somevalue" someOtherAtr="othervalue" >some <g>m</g>ixed content</label></item>
<item><label someAtr="somevalue" someOtherAtr="othervalue">some <g>m<unclear>ixed content</unclear></label></item>
...
</list>
Code: Select all
<list>
<item><label someAtr="somevalue" someOtherAtr="othervalue" ><num>some <g>m</g>ixed content</num></label></item>
<item><label someAtr="somevalue" someOtherAtr="othervalue"><num>some <g>m<unclear>ixed content</unclear></num></label></item>
...
</list>
I try to insert <num> by using
Code: Select all
authorAccess.getDocumentController().surroundInFragment("<num></num>", labelNode.getStartOffset(), labelNode.getEndOffset());
This is the line I use for the insertion:
Code: Select all
authorAccess.getDocumentController().surroundInFragment("<num></num>", labelNode.getStartOffset() + startTagLength, labelNode.getEndOffset() - 8);
Code: Select all
AuthorElement labelElement = (AuthorElement) labelNode;
// Get length of the element's name, add 2 for angle brackets.
int startTagLength = labelElement.getName().length() + 2; // +2 for '<' and '>'
// Iterate over all attributes and add their lengths
int attrCount = labelElement.getAttributesCount();
for (int i = 0; i < attrCount; i++) {
// Get attribute name, add 1 for equals sign, get value, add 2 for quotes, add 1 for leading space.
String attrName = labelElement.getAttributeAtIndex(i);
String attrValue = labelElement.getAttribute(attrName).getValue();
startTagLength += attrName.length() + attrValue.length() + 1 + 2 + 1;
}
Questions:
- Is there a more elegant way to do this using oxygenxml API?
- Why does my code have no effect?
Any help would be greatly appreciated (as always!)
Best,
Michael
-
- Posts: 1016
- Joined: Wed Nov 16, 2005 11:11 am
Re: suroundInFragment method
Post by alex_jitianu »
Hello,
There are a couple aspects to consider:
1. In the model, each start tag and end tag has just one special marker character.
2. An AuthorNode start offset points to the start marker and the end offset points to the end marker, as depicted in the link above
3. surroundInFragment() endOffset is inclusive, so that offset also gets surrounded.
To surround the contents of the label element, this should do:
Please note that the above code will be in trouble for an empty label element (labelNode.getStartOffset() + 1 = labelNode.getEndOffset()) in which case you should just insert at labelNode.getStartOffset() + 1
Best regards,
Alex
There are a couple aspects to consider:
1. In the model, each start tag and end tag has just one special marker character.
2. An AuthorNode start offset points to the start marker and the end offset points to the end marker, as depicted in the link above
3. surroundInFragment() endOffset is inclusive, so that offset also gets surrounded.
To surround the contents of the label element, this should do:
Code: Select all
authorAccess.getDocumentController().surroundInFragment("<num></num>", labelNode.getStartOffset() + 1, labelNode.getEndOffset() - 1);
Best regards,
Alex
-
- Posts: 13
- Joined: Tue Mar 16, 2021 10:01 pm
Re: suroundInFragment method
Dear Alex,
thanks, that works! I misunderstood the image, but now it makes perfect sense. The empty element is no problem because in this case, nothing will be inserted.
Thanks for the help and explanation!
thanks, that works! I misunderstood the image, but now it makes perfect sense. The empty element is no problem because in this case, nothing will be inserted.
Thanks for the help and explanation!
Last edited by np18 on Wed May 17, 2023 10:14 am, edited 1 time in total.
Jump to
- Oxygen XML Editor/Author/Developer
- ↳ Feature Request
- ↳ Common Problems
- ↳ DITA (Editing and Publishing DITA Content)
- ↳ SDK-API, Frameworks - Document Types
- ↳ DocBook
- ↳ TEI
- ↳ XHTML
- ↳ Other Issues
- Oxygen XML Web Author
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Content Fusion
- ↳ Feature Request
- ↳ Common Problems
- Oxygen JSON Editor
- ↳ Feature Request
- ↳ Common Problems
- Oxygen PDF Chemistry
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Feedback
- ↳ Feature Request
- ↳ Common Problems
- Oxygen XML WebHelp
- ↳ Feature Request
- ↳ Common Problems
- XML
- ↳ General XML Questions
- ↳ XSLT and FOP
- ↳ XML Schemas
- ↳ XQuery
- NVDL
- ↳ General NVDL Issues
- ↳ oNVDL Related Issues
- XML Services Market
- ↳ Offer a Service