문제

I'm new to the ReplaceRegExp function in Ant, and have some questions.

Here is my use case.

I have multiple .xml files, which I need to manipulate in specific ways.

Here is one example of the .xml contents:

<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>29.0</apiVersion>
    <packageVersions>
        <majorNumber>1</majorNumber>
        <minorNumber>1228</minorNumber>
        <namespace>prefix1__FirstNamespace</namespace>
    </packageVersions>
    <packageVersions>
        <majorNumber>2</majorNumber>
        <minorNumber>4</minorNumber>
        <namespace>prefix2__TheNextNamespace</namespace>
    </packageVersions>
    <packageVersions>
        <majorNumber>7</majorNumber>
        <minorNumber>542</minorNumber>
        <namespace>prefix3__AnotherNamespace</namespace>
    </packageVersions>
    <status>Active</status>
</ApexClass>

How can I update the the & attributes of the only where the attribute contains the prefix "prefix3"?

I've tried this function, hoping to change the attribute to the value specified by ${correctMinorNumber}:

<target name="setminorNumber">
    <replaceregexp  flags="gis" 
                    match="&lt;minorNumber&gt;?(.*)&lt;/minorNumber&gt;?(.+?)&lt;namespace&gt;prefix3__&lt;/namespace&gt;"
                    byline="false"
        >
        <substitution expression="&lt;minorNumber&gt;${correctMinorNumber}&lt;/minorNumber&gt;&#10;&#9;&#9;&lt;namespace&gt;prefix3__&lt;/namespace&gt;"/>
        <fileset dir="${sf.retrieveFolder}">
            <include name="**/*.xml"/>
        </fileset>
    </replaceregexp>
</target>

It worked correct on files where there was only one <packageVersion> node (with prefix3__ in the namespace), but on files containing multiple <packageVersion> nodes it replaces all nodes with one prefix3__ node. I'm guessing the problem lies with my use of the wildcard, but I don't know enough about replaceregexp to identify the correct solution.

Any ideas on what I can do to achieve what I need to do?

Any help appreciated.

도움이 되었습니까?

해결책

Parsing XML with regular expressions is fraught with problems and best avoided.

Instead, consider using XMLTask. It's an Ant task dedicated to parsing and modifying XML.

Download xmltask.jar and put it in the Ant lib directory.

Below is part of an Ant script. It identifies <minorNumber> elements that are siblings of <namespace> elements where the <namespace> elements have a text node starting with the string prefix3__. It then replaces the text node under the <minorNumber> element:

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

<xmltask dest="${dest.xml-file}">
    <fileset file="src.xml"/>
    <replace 
        path="//:packageVersions[starts-with(:namespace/text(), 'prefix3__')]/:minorNumber/text()" 
        withText="${correctMinorNumber}"
    />
</xmltask>

This script assumes that the XML is in a file named src.xml and a property named dest.xml-file points to where the modified of XML should go.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top