1

I would like to programmatically remove a chunk of XML using an ant script. I found the wonderful xmltask task, but for the life of me I can't find the resource-ref node that I want to delete.

Here's a subsection of what my XML doc looks like. It's from a web.xml file that uses the standard DTD:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" id="WebApp_ID"
     version="3.0" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                       http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name>Foo</display-name>

  <resource-ref>
    <description>Something Clever</description>
    <res-ref-name>jdbc/foo1</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
  </resource-ref>

  <resource-ref>
    <description>Reports Database</description>
    <res-ref-name>jdbc/foo2</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
  </resource-ref>

</web-app>

I'm trying to remove the second resource-ref chunk like this:

<project name="test" basedir="." default="fixxml">
<taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask"/>
  <target name="fixxml" description="er doy">
    <xmltask source="web.xml" dest="output.xml">
      <remove path="/web-app/resource-ref/description[text()='Reports Database']" />
    </xmltask>
  </target>
</project>

However, it doesn't work. I've also tried the following remove statements:

<remove path="/web-app/resource-ref[2]" />
....
<remove path="//description[text()[normalize-space(.)='Reports Database']]"" />    

None of them have working. Does anyone see what I may be doing wrong with my queries?

Tom Purl
  • 519
  • 4
  • 20

2 Answers2

2

The main issue is likely that your web.xml declares a name-space, but your xpath ignores it. Please see: https://stackoverflow.com/a/35778167/366749

The other issue is potentially that your xpath expression designates the 'description' element as the node to delete, not its parent.

Suggested edit (untested):

<remove path="path="/*[local-name()='web-app']/*[local-name()='resource-ref'][2]"/>
Community
  • 1
  • 1
Patrice M.
  • 4,209
  • 2
  • 27
  • 36
  • Thank you for your suggestion but this didn't work for me. I also tried a few variations to no avail. – Tom Purl Mar 10 '17 at 13:44
  • Could you provide specifics about how it didn't work ? Maybe we can fix it and provide an update that can help other folks. – Patrice M. Mar 10 '17 at 17:22
  • I'm sorry but I don't still have examples of what didn't work. I guess the issue is that everything I tried didn't work, and I have no idea how close it was to working. I therefore didn't bother saving them. Please note that I did use multiple tools to help figure this out, including two xpath helpers in Eclipse. – Tom Purl Mar 10 '17 at 22:45
  • 1
    This worked for me exactly like suggested - I had multiple nodes with the same name and this deleted the one I needed (Numbering from 1) – Jaroslav May 23 '19 at 12:02
  • Thank you for the confirmation, helpful! – Patrice M. May 23 '19 at 16:32
0

Patrice's answer seems to describe the the issue best. Xpath seems to be ignoring my namespace. I therefore tried to fiddle with the "query string" using this knowledge and various xpath helpers written in Java. After quite a bit of time I finally gave up on this path.

I ended up fixing this issue by doing the following:

  1. Writing a shell script that removes the XML chunk.
  2. Calling that script from my Ant file.

Please note that for me, this isn't option A or B - it's more like option M. But I just couldn't really afford to learn all of the warts of Xpath and how it's implemented in popular Java libraries.

Tom Purl
  • 519
  • 4
  • 20