首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用使用ant或ant扩展的regexp将每个xml数字属性替换为其字符串中的算术积?

用使用ant或ant扩展的regexp将每个xml数字属性替换为其字符串中的算术积?
EN

Stack Overflow用户
提问于 2013-03-08 12:55:18
回答 2查看 635关注 0票数 0

我有一个xml字符串,属性包括x-pos="NN"y-pos="NN",其中NN是正数或负数。我希望读取每个值并将其更改为算术乘积#{NN * 15},即x-pos="3“将改为x_pos="45”。

因此,我需要这样的东西:

代码语言:javascript
复制
<ant-contrib:propertyregex  property="xval"
   input="${xmlfile.contents}"
   regexp="x-pos\s*=\s*&quot;([0-9\-]+)&quot;"
   replace="x-pos=&quot;_TRICKY_EXPR_EVALUATOR_{\1 * 15}&quot;"
   override="true" global="yes"/>

或者我可以以某种方式捕获所有/x-pos*=\s*\“(0-9-+)\”/匹配(就像在PHP preg_match_all函数中那样),并将它们放入一个由';‘分隔的flaka列表或- say - in字符串中?一旦我有了它,我就可以分割它,并遍历它以“手动”替换每个值。

还有其他蚂蚁扩展可以使用类似perl的正则表达式吗?我了解了弗拉卡和蚂蚁的情况,但这些都帮不上忙。

谢谢你的想法!

更新:下面是要解析的xml的一个假设片段:

代码语言:javascript
复制
<sprite name="timer" xref="" pos-x="25" pos-y="4" path="img/folder1/img1.jpg" />
<sprite name="timer1" xref="" pos-x="25" pos-y="4" offset-x="100" offset-y="10" path="img/folder1/img2.jpg" />
<control name="timer2" xref="" pos-x="25" pos-y="4" size="100" offset-y="10" path="img/folder1/img2.jpg" />
EN

回答 2

Stack Overflow用户

发布于 2013-03-11 21:24:06

也许在使用flaka时已经使用了:

代码语言:javascript
复制
 <!-- Activate flaka for all ant tasks -->
 <fl:install-property-handler/>

结合:

代码语言:javascript
复制
#{ x * y}

不知怎么会对你起作用,没有测试它,因为我的机器上没有安装反体肋骨。

属性处理程序允许在所有ant任务中使用EL表达式。

下面是一个具有给定文件foo.xml的小示例,需要xmltask弗拉卡

代码语言:javascript
复制
<whatever>
<sprite name="timer" path="img/folder1/img1.jpg" pos-x="25" pos-y="4" xref=""/>
<sprite name="timer1" offset-x="100" offset-y="10" path="img/folder1/img2.jpg" pos-x="26" pos-y="4" xref=""/>
<control name="timer2" offset-y="10" path="img/folder1/img2.jpg" pos-x="27" pos-y="4" size="100" xref=""/>
</whatever>

编辑foo.xml就绪:

代码语言:javascript
复制
<project xmlns:fl="antlib:it.haefelinger.flaka">
 <!-- Activate flaka for all ant tasks -->
 <fl:install-property-handler/>
 <!-- Import XMLTask -->
 <taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask"/>

 <!-- get a list with all pos-x attribute values -->
 <xmltask source="foo.xml">
   <copy path="//whatever/*/@pos-x" append="true" propertySeparator="," property="posxlist"/>
 </xmltask>
 <echo>$${posxlist} => ${posxlist}</echo>
 <fl:let>counter ::= 1</fl:let>
 <!-- for loop with xmltask editing foo.xml in place => source = dest -->
 <fl:for var="posx" in="split('${posxlist}', ',')">
   <xmltask source="foo.xml" dest="foo.xml" report="true">
     <!-- i.e. multiplicating value * 3 -->
     <attr path="//whatever/*[${counter}]" attr="pos-x" value="#{posx * 3}"/>
   </xmltask>
   <fl:let>counter ::= '${counter}' + 1</fl:let>
 </fl:for>
</project>

产出:

代码语言:javascript
复制
  [xmltask] Cannot append values to properties
  [xmltask] Cannot append values to properties
  [xmltask] Cannot append values to properties
     [echo] ${posxlist} => 25,26,27
  [xmltask] Document -->
  [xmltask] <whatever>
  [xmltask] <sprite name="timer" path="img/folder1/img1.jpg" pos-x="75" pos-y="4" xref=""/>
  [xmltask] <sprite name="timer1" offset-x="100" offset-y="10" path="img/folder1/img2.jpg" pos-x="26" pos-y="4" xref=""/>
  [xmltask] <control name="timer2" offset-y="10" path="img/folder1/img2.jpg" pos-x="27" pos-y="4" size="100" xref=""/>
  [xmltask] </whatever>
  [xmltask] Document <--
  [xmltask] Document -->
  [xmltask] <whatever>
  [xmltask] <sprite name="timer" path="img/folder1/img1.jpg" pos-x="75" pos-y="4" xref=""/>
  [xmltask] <sprite name="timer1" offset-x="100" offset-y="10" path="img/folder1/img2.jpg" pos-x="78" pos-y="4" xref=""/>
  [xmltask] <control name="timer2" offset-y="10" path="img/folder1/img2.jpg" pos-x="27" pos-y="4" size="100" xref=""/>
  [xmltask] </whatever>
  [xmltask] Document <--
  [xmltask] Document -->
  [xmltask] <whatever>
  [xmltask] <sprite name="timer" path="img/folder1/img1.jpg" pos-x="75" pos-y="4" xref=""/>
  [xmltask] <sprite name="timer1" offset-x="100" offset-y="10" path="img/folder1/img2.jpg" pos-x="78" pos-y="4" xref=""/>
  [xmltask] <control name="timer2" offset-y="10" path="img/folder1/img2.jpg" pos-x="81" pos-y="4" size="100" xref=""/>
  [xmltask] </whatever>
  [xmltask] Document <--
BUILD SUCCESSFUL
Total time: 826 milliseconds

警告“不能将值附加到属性”起源于com.oopsconsultancy.xmltask.CopyAction第80行,以强调ant中的属性是不可变的,并且可能被安全地忽略,或者更好地从源中删除它并重新构建xmltask.jar。

票数 1
EN

Stack Overflow用户

发布于 2013-03-12 12:26:32

这是我自己的解决方案的一部分。我不会将其标记为接受,因为它不能解决一般的文本解析问题(比如php的preg_match_all)。但也许有人会觉得这很有趣。它有点混乱--我使用ant属性和flaka变量,但它说明了如何将它们结合使用。下面是代码:

代码语言:javascript
复制
filename: process.xml
代码语言:javascript
复制
<!-- these includes are needed so that eclipse can load autocompletion base from plugins,
and they tell to ant where plugins' jars are (in 'ut' folder on the same level)-->
<taskdef uri="antlib:it.haefelinger.flaka" resource="it/haefelinger/flaka/antlib.xml" classpath="ut/ant-flaka.jar" />
<taskdef uri="antlib:net.sf.antcontrib" resource="net/sf/antcontrib/antlib.xml" classpath="ut/ant-contrib-1.0b3.jar" />

<!-- call: >> ant -Dfname="folder/with/xmls" -f process.xml correct-xmls -->
<target name="correct-xmls">
    <fl:install-property-handler />
    <property name="slashn" value="${line.separator}" />

    <!-- get xmls - only with existing root resprops element -->
    <fileset dir="${fname}" includes="**/*.xml" id="xml-classes">
        <contains text="&lt;resprops&gt;" />
    </fileset>

    <fl:for var="xn" in="split('${toString:xml-classes}', ';')">
        <fl:let>curfile = file(concat('${fname}','/',xn))</fl:let>
        <fl:let>tgtfile = file(concat('${fname}','/',xn,'.new'))</fl:let>
        <fl:echo>#{ format('file %s, last modified %tD, size: %d',  
            curfile.path, curfile.mtime, curfile.isdir ? 0 : curfile.size) }</fl:echo>

        <fl:unset>xmlfile.contents</fl:unset>
        <loadfile property="xmlfile.contents" srcFile="#{curfile}" />
        <fl:let>outstr = ''</fl:let>
        <fl:for var="str" in="split('${xmlfile.contents}', '\n')">
            <fl:unset> xval </fl:unset>
            <ac:propertyregex property="xval" input="#{str}" regexp="pos-x\s*=\s*&quot;([0-9\-]+)&quot;" select="\1" override="true" />
            <!-- force set property 'resstr' to value of var 'str'-->
            <fl:let>resstr ::= str</fl:let>

            <!-- process only if pos-x is found and we have its value in 'xval' -->
            <fl:when test="not empty '#{property.xval}'">
                <fl:let>outval = (property.xval * 15.5 + 0.5) </fl:let>
                <!-- kinda int-from-float -->
                <ac:propertyregex property="gotv" input="#{outval}" regexp="([0-9\-]+)\." select="\1" override="true" />
                <ac:propertyregex property="resstr"
                                  input="${resstr}"
                                  regexp="pos-x\s*=\s*&quot;([0-9\-]+)&quot;"
                                  replace="pos-x = &quot;#{gotv}&quot;"
                                  override="true"
                />
            </fl:when>

            <!-- add to output string by string -->
            <fl:let> outstr = format('%s%s%s', outstr , resstr, slashn) </fl:let>

        </fl:for>

        <!--save processed file -->
        <echo file="#{tgtfile}" encoding="utf-8">#{outstr}</echo>

    </fl:for>
</target>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15294489

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档