Hi,
Both operations can be done using the
XML Refactoring tool that is available in the application starting with version 17. To access this tool, select the
XML Refactoring action from the
Tools menu.
For instance, to insert a new element you can use the predefined
Insert XML Fragment operation. After you select the operation, in the next step you have to specify its parameters as follows:
XML Fragment: <Qty>1</Qty>
XPath: PartType
Position: After
For the second request, to move an element, there is no predefined operation. I've added an issue to be added in a further oXygen version.
The good news is that this tool is extensible and it accepts custom operations. An XML Refactoring operation is defined as a pair of resources:
* an XQuery Update script or XSLT stylesheet that Oxygen XML Editor will run in order to refactor the XML files.
* an XML Operation Descriptor file that contains information about the operation, such as the name, description, and parameters.
move-element.xq
Code: Select all
xquery version "3.0" encoding "utf-8";
(:
XQuery document used to implement 'Move element' operation from the XML Refactor tool.
:)
declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";
declare namespace saxon = "http://saxon.sf.net/";
declare option output:method "xml";
declare option output:indent "no";
(: The XPath that localize the elements to be moved. :)
declare variable $context_element_xpath as xs:string external;
declare variable $element_to_move_xpath as xs:string external;
(: The XPath that localize the anchor elements. The element will be moved relatively to this node. :)
declare variable $element_anchor_xpath as xs:string external;
(: The insert position relative to the node determined by the 'Element XPath' expression. :)
declare variable $insert_position as xs:string external;
let $cElements := saxon:evaluate($context_element_xpath)
for $elem in $cElements
let $elemsToMove := saxon:evaluate(concat('$p1/', $element_to_move_xpath), $elem)
let $anchorElements := saxon:evaluate(concat('$p1/', $element_anchor_xpath), $elem)
return
if (count($elemsToMove) > 0 and count($elemsToMove) = count($anchorElements))
then
(
for $eMove in $elemsToMove
let $aNode := $anchorElements[position()]
return
if($eMove/ancestor::node() = $elem and $aNode/ancestor::node() = $elem) then (
switch ($insert_position)
case 'firstChild'
return insert nodes $eMove as first into $aNode
case 'lastChild'
return insert nodes $eMove as last into $aNode
case 'after'
return insert nodes $eMove after $aNode
case 'before'
return insert nodes $eMove before $aNode
default return () ,
delete node $eMove
) else ()
) else ( )
move-element.xml
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<!--
Descriptor for 'Rename attribute' operation.
-->
<refactoringOperationDescriptor
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.oxygenxml.com/ns/xmlRefactoring" id="move-element" name="Move Element">
<description>Move one or more elements</description>
<script type="XQUERY_UPDATE" href="move-element.xq"/>
<category>Elements</category>
<parameters>
<description>Specify the elements to be moved and their new location.</description>
<parameter type="TEXT" name="context_element_xpath" label="Context Element">
<description>The XPath expression that identifies one or more context elements.</description>
</parameter>
<parameter type="TEXT" name="element_to_move_xpath" label="Element to Move">
<description>The XPath expression that identifies one or more elements to move.
The Xpath expression is relative to the context node.</description>
</parameter>
<parameter type="TEXT" name="element_anchor_xpath" label="Anchor Element">
<description>The XPath expression that identifies one or more anchor elements.
The Xpath expression is relative to the context node.</description>
</parameter>
<parameter label="Position" type="TEXT" name="insert_position">
<description>The insert position relative to the anchor element.</description>
<possibleValues onlyPossibleValuesAllowed="true">
<value name="before">Before</value>
<value name="after" default="true">After</value>
<value name="firstChild">First child</value>
<value name="lastChild">Last child</value>
</possibleValues>
</parameter>
</parameters>
</refactoringOperationDescriptor>
Please save these files in a separate folder. After that, you have to specify the path to this folder in the preferences page:
XML / XML Refactoring. This lets application to know where are the additional operations.
Finally, you have to display the
XML Refactoring dialog and choose the new added operation. Specify the next parameters:
Context Element: //App
Element to Move: Note
Anchor Element: Qty
Position: Before