Ant XMLTASK insert node if it does not already exist

I have the task of inserting an XML node into an existing XML file only if the node no longer exists. I fit the insert perfectly, however I cannot find this missing, if not functionality

<xmltask source="shared.xml" dest="shared.xml" outputter="simple:3"> <insert path="/sharedobjects[last()]"> <![CDATA[ <connection> <name>MY CONNECTION</name> </connection> ]]> </insert> </xmltask> 

If I run this several times, of course, I will have several MY CONNECTION in the XML file. I would like to make sure that I only insert it if the desired connection was not already in the file.

Thanks in advance.

+4
source share
3 answers

I believe this method also works.

 <xmltask source="shared.xml" dest="shared.xml" outputter="simple:3"> <copy path="/sharedobjects/connection[name/text()='MY CONNECTION']/name/text()" property="XML_EXISTS_ALREADY" /> <insert path="/sharedobjects[last()]" unless="XML_EXISTS_ALREADY"> <![CDATA[ <connection> <name>MY CONNECTION</name> </connection> ]]> </insert> </xmltask> 

NOTE. The xmltask copy task allows you to store attributes or text nodes in properties. Thus, you must specify /name/text() at the end of the path argument for <copy> (even if we really care about the whole <connection> node, and not its child text).

+7
source

I managed to solve my problem. This is a more or less workaround. The solution is the delete and then insert method

 <xmltask source="shared.xml" dest="shared.xml" outputter="simple:3"> <remove path="/sharedobjects/connection[name/text()='MY CONNECTION']"/> <insert path="/sharedobjects[last()]"> <![CDATA[ <connection> <name>MY CONNECTION</name> </connection> ]]> </insert> </xmltask> 
+2
source

Using Ant conditions (not sure if everything existed when asking the question):

 <if> <not> <resourcecontains resource="shared.xml" substring="&gt;MY CONNECTION&lt;name&gt;" /> </not> <then> <xmltask ... </xmltask> </then> 
0
source

All Articles