ANT:根据XML文件内容替换XML文件集中的param

时间:2011-08-20 22:03:14

标签: xml ant

我的目录中包含带有xml文件的子目录,并带有不同的扩展名。


更新部分的开始


目标目录:/some/path

目标文件掩码:*.load

我们用于查找*.load下的/some/path个文件的基本过滤器:

<fileset dir="/some/path">
    <include name="**/*.load" />
</fileset>

/some/path下有多个子目录,多个*.load文件也可以。

但我们需要根据示例中描述的XML内容和参数来增强过滤,下面的用例。

示例:

load_file1.load:

<load 
    id="0a0740d1fc1a33a28f1397b76cae48bc"
    order="9"
    enable="true"
    name="Load name 1"
    type="LoadType.custom">
    <parent />
    <default-property name="Property2"
                      type="java.lang.Integer"
                      value="0" />
    <default-property name="Property1"
                      type="java.lang.Boolean"
                      value="false" />
</load>

load_file2.load:

<load 
      id="ec9ca08d11ca34b42e13c5f21578d82c"
      name="Load name 2"
      order="0"
      enable="true"
      type="LoadType.base">
    <parent />
    <default-property name="Load name 1"
                      type="java.lang.String"
                      value="test3" />
    <default-property name="Property2"
                      type="java.lang.Integer"
                      value="0" />
    <!-- here could be any number of other sub-properties  -->
    <date-range end-date="1/31/1900"
                     start-date="1/31/1900" />
</load>

输入数据:

Ant param:Load_name = "Load name 1" - 用于查找相应的文件

Ant param:Load_param_name_replace = "enable"

Ant param:Load_param_value_replace = "false"

NB!我们总是根据其中//load/@name属性(硬编码)的值搜索文件:

<load 
      id="ec9ca08d11ca34b42e13c5f21578d82c"
      name="Load name 2"
      order="0"
      enable="true"
      type="LoadType.base">

问题(基于上述文件的示例):

  1. 我们应该找到文件,其中//load/@name = $Load_name ANT参数(“加载名称1 ”)

  2. 我们应该将$Load_param_name_replace ANT参数(“ enable ”)中声明的已找到文件(可能是多个)XML属性更改为ANT参数{{1}的值}(“ false ”)

  3. 预期结果

    1. 找到文件: load_file1.load
    2. NB!请注意,在 load_file2.load 中,我们$Load_param_value_replace匹配组合<default-property name="Load name 1" ...,但我们需要区分这个文件 load_file1.load 中的无效案例和有效案例。

      1. 在找到的文件 load_file1.load name="Load name 1"中定义的XML属性(“ enable ”)$Load_param_name_replace节点下的ANT参数应该被更改为ANT参数//load中定义的新值(“ false ”)
      2. 因此,在ANT任务之后,文件 load_file1.load 应如下所示:

        $Load_param_value_replace

        更新后的部分


        正如您所看到的那样,xml文件的内容通过多行传播 - 这对我来说非常棘手。

        我们尝试了使用<load id="0a0740d1fc1a33a28f1397b76cae48bc" order="9" enable="false" name="Load name 1" type="LoadType.custom"> <parent /> <default-property name="Property2" type="java.lang.Integer" value="0" /> <default-property name="Property1" type="java.lang.Boolean" value="false" /> </load> 过滤器的ANT fileset目标,搜索多行正则表达式containsregexp ,但没有成功。我们使用name="load_name1"multiline=true - 也没用。

        之后我们尝试了 XMLtask - 但是没有足够的示例说明如何根据某些singleline=true替换多个.XML文件中的xml属性。

        所以如果你能提供几个例子 - 那就太棒了!

2 个答案:

答案 0 :(得分:1)

使用Ant XSLT任务应用如下所示的转换:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="load[@name='load_name1']">
        <xsl:copy>
            <xsl:attribute name="from">date2</xsl:attribute>
            <xsl:apply-templates select="@*[name()!='from']"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

这会复制您的输入,替换匹配from的代码上的name="load_name1"属性。请注意,这可能会更改缩进,但无论输入的格式如何都可以使用,这对于正则表达式来说可能是不可能的。

答案 1 :(得分:1)

更新后的问题说明完全修订:

<project>
<taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask"/>

<property name="editno" value='&lt;default-property name="Load name 1"'/>
<property name="edityes" value='name="Load name 1"'/>
<property name="editxpath" value="//load/@enable"/>
<property name="editnew" value="false"/>

 <xmltask todir="/some/path" report="true">
  <fileset dir="/some/path">
   <include name="**/*.load"/>
    <not>
     <contains text="${editno}" casesensitive="true"/>
    </not>
    <contains text="${edityes}" casesensitive="true"/>
   </fileset>
   <replace path="${editxpath}" withText="${editnew}"/>
  </xmltask>
</project>

快速检查文件集的内容只需使用内置属性$ {toString:filesetid}

 <fileset dir="/some/path" id="foobar">
  <include name="**/*.load" />
  <not>
   <contains text="${editno}" casesensitive="true" />
  </not>
  <contains text="${edityes}" casesensitive="true" />
 </fileset>

 <echo>${toString:foobar}</echo>

请参阅Ant手册Properties and PropertyHelpers