1
I want to insert an element in the XML.
This is an XML file - named web.xml

<web-app>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
</web-app>

This is the ANT task I am using to insert an element in the web-app node.

<taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask" classpath="C:/xmltask.jar"/>    
<xmltask source="C:\web.xml" dest="C:\web.xml>              
     <insert path="/web-app">
          <![CDATA[
            <hello_world id="3">hello world</hello_world>
          ]]>
     </insert>          
</xmltask>      

Ant task run inserts the hello_world node in the web-app.

The insert (actually root node selection) fails when the root has an attribute.
So the xmltask insert doesn't work when the XML is -

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee">
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
</web-app>

I tried to use insert like this, but no luck -
<insert path="/web-app/[@xmlns='http://xmlns.jcp.org/xml/ns/javaee']">

What is the way to select the root node? Why this is not working, an explanation would help.

p zankat
  • 75
  • 2
  • 14

1 Answers1

1

This is most likely a problem for XmlTask/xpath when the Xml uses a declared namespace (xmlns= is not just an attribute). See this other answer: https://stackoverflow.com/a/35778167/366749 I suggest you try:

<taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask" classpath="C:/xmltask.jar"/>    
<xmltask source="C:\web.xml" dest="C:\web.xml>              
     <insert path="*[local-name()='web-app']">
          <![CDATA[
            <hello_world id="3">hello world</hello_world>
          ]]>
     </insert>          
</xmltask>     
Patrice M.
  • 4,209
  • 2
  • 27
  • 36
  • Thanks Patrice, your suggestion worked, a minor modification - it should be - i.e. only one closing square bracket instead of two. It also introduces "xmlns=''" with inserted node. I.e. hello_world. Any suggestion not to avoid induction of attribute "xmlns". – p zankat Oct 03 '20 at 04:50
  • Thanks @p-zankat I fixed the syntax. For the `xmlns` namespace, does it actually empty the contents, or does it introduce it when the original xml didn't have it ? I suppose this syntax is to be used when you KNOW there is an xml namespace already declared. It shouldn't blank it out in this case and leave it as-is, though, and if it does, then the solution is incomplete indeed. If you KNOW that the xml doesn't have an xmlns declaration, then use your own original solution instead. Apologies, I don't have XmlTask around to test this, I stopped using it may years ago... – Patrice M. Oct 03 '20 at 16:30
  • Thanks for your response. The xml node that I want to insert is hello world. As is shown above. It doesn't have attribute 'xmlns'. It gets introduces when I use what you have suggested. In case suppose the root element don't have any attribute, the path selection like path="/web-app" works and also it don't insert the attribute in the adding node. – p zankat Oct 05 '20 at 04:03
  • My apologies for reading this too fast. So I suppose what's happening is that the enclosing XML has a namespace declared, but the XML you're adding (hello_world) doesn't. XmlTask feels it needs to reset the xmlns to nothing, in order to clarify that the enclosing xml namespace doesn't apply. The workaround, as discussed in https://stackoverflow.com/a/40305581/366749, is to specify the same xmlns attribute in your inserted xml. – Patrice M. Oct 05 '20 at 12:09