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 任务运行在web-app中插入hello_world节点。

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']">

选择根节点的方法是什么?为什么这不起作用,解释会有所帮助。

4

1 回答 1

1

xmlns=当 Xml 使用声明的命名空间(不仅仅是一个属性)时,这很可能是 XmlTask​​/xpath 的问题。请参阅其他答案: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>     
于 2020-10-02T17:24:03.017 回答