如何使用 Ant 替换 XML 字段的值? [英] How to replace value of an XML field using Ant?

查看:21
本文介绍了如何使用 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 字段的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆