4

I have the following task and for some reason is not matching my file:

<xmltask source="nbproject/project.xml" dest="nbproject/project.xml">
        <replace path="/project/configuration/data/class-path-extension/runtime-relative-path/text()" 
        withText="ext/extensions/${extension-lib.dist.jar}.jar"/>
        <replace path="/project/configuration/data/class-path-extension/binary-origin/text()" 
        withText="${original.project.dir}/dist/${extension-lib.dist.jar}.jar"/>
</xmltask>

Here's the xml file I'm searching:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project xmlns="http://www.netbeans.org/ns/project/1">
<type>org.netbeans.modules.apisupport.project</type>
<configuration>
    <data xmlns="http://www.netbeans.org/ns/nb-module-project/3">
        .
        .
        .
        <class-path-extension>
            <runtime-relative-path>ext/extensions/Zone_x.jar</runtime-relative-path>
            <binary-origin>../../Simple Marauroa Java/Zone Extension/dist/Zone_y.jar</binary-origin>
        </class-path-extension>
    </data>
</configuration>

I removed stuff not important for this question. Using the Xpath plugin for NetBeans on the same file shows matches for ext/extensions/Zone_x.jar and ../../Simple Marauroa Java/Zone Extension/dist/Zone_y.jar respectively, but the task doesn't see them.

Any ideas?

Mark O'Connor
  • 76,015
  • 10
  • 139
  • 185
javydreamercsw
  • 5,363
  • 13
  • 61
  • 106
  • Would an XSLT solution interest you? The benefit would be one less dependency and a clean solution. – FailedDev Oct 07 '11 at 00:09
  • If it can be done from ant I have no issues with it. provide the answer and I'll take a look. – javydreamercsw Oct 10 '11 at 15:01
  • Any updates from your approach with XSLT? – javydreamercsw Oct 24 '11 at 21:54
  • Hi @javydreamercsw... I ran into a similar problem. The exact scenario is this: xmlns="..." attribute of the project element (in the source XML). If you remove the attribute (or set it to "") XMLTask should work correctly. This is the problem, but I'm still looking for a solution. Please let me know if you've found something! – themoondothshine Dec 02 '11 at 07:05

2 Answers2

8

The problem is that the input XML uses namespaces. The solution is to use *[local-name()='project'] instead of project, etc., which means you need to write

<xmltask source="nbproject/project.xml" dest="nbproject/project.xml">
    <replace path="/*[local-name()='project']/*[local-name()='configuration']/*[local-name()='data']/*[local-name()='class-path-extension']/*[local-name()='runtime-relative-path']/text()" 
        withText="ext/extensions/${extension-lib.dist.jar}.jar"/>
    <replace path="/*[local-name()='project']/*[local-name()='configuration']/*[local-name()='data']/*[local-name()='class-path-extension']/*[local-name()='binary-origin']/text()" 
        withText="${original.project.dir}/dist/${extension-lib.dist.jar}.jar"/>
</xmltask>
jpsecher
  • 4,461
  • 2
  • 33
  • 42
Kreozot
  • 1,577
  • 1
  • 16
  • 26
  • Can you give me the full answer with your suggestion? I'm not clear on what you mean. – javydreamercsw Jun 11 '12 at 13:29
  • When xml tag have an xmlns attribute, xmltask is not work correctly with plain XPath expressions. To describe xmltask what you want to find, you must clarify that you means just local name of the tags. In your case expression must looks something like that: `/*[local-name()='project']/*[local-name()='configuration']/*[local-name()='data']/*[local-name()='class-path-extension']/*[local-name()='runtime-relative-path']/text()` Sorry about my horrible english =] – Kreozot Jun 15 '12 at 10:57
  • Wonderful explanation! Saved my day. Thanks very much – user1807337 Jul 09 '13 at 21:46
2

simply use ":" for local name space . Ex.

replace path="/:project/:configuration/:data/:class-path-extension/:runtime-relative-path/text()"

Reference document = https://today.java.net/article/2006/10/31/xml-manipulation-using-xmltask

Read section - Paths and Namespaces