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任务运行将hello_world节点插入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']">选择根节点的方法是什么?为什么这是行不通的,一个解释会有帮助。
发布于 2020-10-02 17:24:03
当Xml使用声明的命名空间时,这很可能是XmlTask/xpath的一个问题(xmlns=不仅仅是一个属性)。参见另一个答案:https://stackoverflow.com/a/35778167/366749,我建议您尝试:
<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> https://stackoverflow.com/questions/64159442
复制相似问题