1

I am trying to patch several XML files using XML-patch, more specifically using this library https://github.com/dnault/xml-patch

I can properly patch any xml file that does not contain any namespace declarations. I am patching a logback.xml file and all works properly.

The problem is when I try to patch a web.xml file that looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="3.0" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" metadata-complete="true">

    <display-name>My App</display-name>
...
</web-app>

Using a xml-patch file that looks like this

<diff file="WEB-INF/web.xml">
    <add sel="web-app" >
        <simple>true</simple>
    </add>
</diff>

When I run the patcher, I get this exception:

Caused by: com.github.dnault.xmlpatch.PatchException: no matches for selector "web-app"
    at com.github.dnault.xmlpatch.Patcher.selectNodes(Patcher.java:441)
    at com.github.dnault.xmlpatch.Patcher.add(Patcher.java:252)
    at com.github.dnault.xmlpatch.Patcher.patch(Patcher.java:85)
    at com.github.dnault.xmlpatch.Patcher.patch(Patcher.java:65)
    at com.github.dnault.xmlpatch.CommandLineDriver.main(CommandLineDriver.java:62)

I am fairly confident this has to be a problem with the diff file and namespaces, which I probably have not configured properly.

I have tried adding namespace declarations to the diff file but the error is the same.

Any ideas?

3
  • 1
    Since you're dealing with namespaces, whats the output if you add a wildcard character in your select expression. Like this : add sel="*/web-app" ? It seems that's the proper way to do it. See examples here : xmlpatch.sourceforge.net/srns.txt Commented Jul 8, 2020 at 1:22
  • Hi. I had already tried something like that with no success. Your example results in a this error: no matches for selector "*/web-app" Commented Jul 9, 2020 at 6:51
  • I guess that's because I was talking about the wrong XML patch library. The one available on sourceforge (libxmlpatch), not on Github. Glad you found something that works.:) Commented Jul 10, 2020 at 0:47

2 Answers 2

1

It looks like this is a confirmed issue of the tool. Howevever, the original poster also mentions a workaround:

So in effect I haver to declare the defaultnamespace twice in order to allow selection and addition of new nodes without having to prefix the added nodes one by one with "cfg:" prefix. See the following example:

Applied to your example, the patch.xml should look something like this.

<diff xmlns:app="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="3.0" metadata-complete="true">
    <add sel="app:web-app">
        <simple>true</simple>
    </add>
</diff>

That way the patching succeeds.

Sign up to request clarification or add additional context in comments.

1 Comment

This is quite close, but the added element adds an unwanted "xmlns" attribute to the added <simple /> node: <simple xmlns="">true</simple>
1

There is another way to do it:

<diff file="WEB-INF/web.xml">
    <add sel="/*['web-app'=local-name()]">
        <simple>true</simple>
    </add>
</diff>

This also results in an unwanted but harmless "xmlns" attribute being added:

<simple xmlns="">true</simple>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.