2

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.

user2984213
  • 119
  • 10
  • Show the sample xml that you are working on to see if that is because of any namespaces. – Rao Mar 03 '16 at 04:59
  • your replace path seems not to be a valid XPath, should be more something like : `/persistence/persistence-unit/properties/property[@name='javax.persistence.jdbc.url']/@value` – P.A. Cros Mar 03 '16 at 08:26
  • @Rao I've switched to a simple xml that I've now included in my question. I've also changed the property and value to simple strings to avoid potential conflicts with special characters (periods, colons). I'm still unable to replace the value. – user2984213 Mar 03 '16 at 15:23

1 Answers1

2

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>  
Patrice M.
  • 4,209
  • 2
  • 27
  • 36
  • Running into: javax.xml.transform.TransformerException: Unknown nodetype: local-name – user2984213 Mar 03 '16 at 18:13
  • So without schema this seems to work fine. Can we provide some wildcards as part of the tag name to make the match? – user2984213 Mar 03 '16 at 19:07
  • I'm accepting this answer because Patrice included wildcard example in his response and showed me the `attr` tag. Combination of both works for me, as outlined at the bottom of my question. Thanks. – user2984213 Mar 03 '16 at 20:11
  • Apologies for the dodgy xpath syntax. I have tentatively corrected it. Thank you for accepting the answer, certainly. – Patrice M. Mar 04 '16 at 00:56
  • Also, FWIW, I like to validate my xpath expressions with this simple online tool: http://www.shell-tools.net/index.php?op=xml_xpath , I recommend it. – Patrice M. Mar 04 '16 at 01:20