How to replace value of an XML field using Ant?

2019-02-26 00:02发布

问题:

In an Ant script, I need to replace the value of javax.persistence.jdbc.url property in the following persistence.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="testPU" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>org.somecompany.domain.SomeEntity</class>
        <validation-mode>NONE</validation-mode>
        <properties>
            <property name="testprop" value="testval" />
        </properties>
    </persistence-unit>
</persistence>

I've downloaded XMLTask and have tried the following:

<xmltask source="${persistence-xml-file-path}" dest="${persistence-xml-file-path}_replaced" report="true">
    <replace path="/:persistence/:persistence-unit/:properties/:property[:name/text()='testprop']/:value/text()" withText="replaced" />
</xmltask>  

Unfortunately, this doesn't work. I don't get any errors. Contents of both source and destination xml files appear in console and they're the same. It's as if the replace instruction quoted above never runs (or never identifies the property to update).

=== Following Response from Patrice ===========================================

This seems to work without schema definition for persistence tag:

<xmltask source="${persistence-xml-file-path}" dest="${persistence-xml-file-path}_replaced" report="true" failWithoutMatch="true">
<attr path="persistence/persistence-unit/properties/property[@name='testprop']" attr="value" value="replaced"/>
</xmltask>

This seems to work with schema definition for persistence tag:

<xmltask source="${persistence-xml-file-path}" dest="${persistence-xml-file-path}_replaced" report="true" failWithoutMatch="true">
<attr path="//*[@name='testprop']" attr="value" value="replaced"/>
</xmltask>

Attributes I need to process are very unique, so this is going to work fine for me without the need to examine entire attribute path.

回答1:

As mentioned by @Rao, your problem is the xpath not dealing properly with namespaces. The syntax that utilizes ":" hasn't worked consistently for me. As many other XmlTask answers have shown on this site, you need to use the //*[local-name()='persistence'] syntax instead. Also, attribute can be referenced with the @name syntax. Last, if you want to replace the value of an attribute, don't use <replace xpath="..., use <attr xpath="...

Please try:

<xmltask source="${persistence-xml-file-path}" dest="${persistence-xml-file-path}_replaced" report="true">
   <attr path="/*[local-name()='persistence']/*[local-name()='persistence-unit']/*[local-name()='properties']/*[local-name()='property'][@name='testprop']" attr="value" value="replaced" />
</xmltask>  


标签: xml ant xmltask