本文介绍了如何使用Ant替换XML字段的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Ant脚本中,我需要替换以下persistence.xml文件中的javax.persistence.jdbc.url属性的值.

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>

我已经下载了XMLTask并尝试了以下操作:

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>

不幸的是,这不起作用.我没有任何错误.源和目标xml文件的内容都显示在控制台中,并且它们是相同的.就像上面引用的替换指令永远不会运行(或永远不会标识要更新的属性).

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).

===以下来自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.

推荐答案

如@Rao所述,您的问题是xpath无法正确处理名称空间.使用:"的语法对我而言并非始终如一.正如此站点上显示的许多其他XmlTask​​答案一样,您需要使用//*[local-name()='persistence']语法.同样,可以使用@name语法引用属性.最后,如果要替换属性的值,请不要使用<replace xpath="...,请使用<attr xpath="...

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="...

请尝试:

<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>

这篇关于如何使用Ant替换XML字段的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 05:33
查看更多