How to avoid xml header while concatenating xml fi

2019-09-18 01:22发布

This question already has an answer here:

I am concatenating all the xml files in a folder into a single xml file in ant script. While concatenating the xml files, the header

 <?xml version="1.0" encoding="UTF-8" ?> 

in all xml files are getting appended in the output xmlfile.

Is there any way to avoid this header ?

  <concat destfile="${docbook.dir}/all-sections.xml"
      force="no">       
   <fileset dir="${docbook.dir}"
     includes="sections/*.xml"/>
   </concat>

2条回答
别忘想泡老子
2楼-- · 2019-09-18 02:02

You can apply a regex to discard the header:

<concat destfile="${docbook.dir}/all-sections.xml" force="no">       
    <fileset dir="${docbook.dir}" includes="sections/*.xml"/>
    <filterchain>
        <linecontainsregexp negate="true">
            <regexp pattern="&lt;\?xml version"/>
        </linecontainsregexp>
    </filterchain>  
 </concat>

https://ant.apache.org/manual/Types/filterchain.html

EDIT: If you want to keep the first occurrence of the header then this is an option:

<property name="first" value="true"/>

<concat destfile="${docbook.dir}/all-sections.xml">       
    <fileset dir="${docbook.dir}" includes="sections/*.xml"/>
    <filterchain>
        <scriptfilter language="javascript">
        <![CDATA[
            first = project.getProperty("first");
            if(self.getToken().indexOf("<\?xml version") != -1) {
                if(first == "true") {
                    project.setProperty("first", "false");
                } else {
                    self.setToken(null);
                }
            }
        ]]> 
        </scriptfilter>             
    </filterchain>
</concat>
查看更多
太酷不给撩
3楼-- · 2019-09-18 02:20

There is a filter chain.So you can use ' xml version' or 'encoding' to filter.

<filterchain>
         <linecontains>
          <contains value="xml version"/>
         </linecontains>
      </filterchain>

Then there is one Header that can be added as common.There you can give the common header.

<header filtering="no" trimleading="yes">
      ----Put  Header---
</header>

Function Ref: https://googleweblight.com/?lite_url=https://ant.apache.org/manual/Tasks/concat.html&ei=t1jfBWPU&lc=en-IN&s=1&m=717&host=www.google.co.in&ts=1456774849&sig=ALL1Aj6a3WOuua261FfWU1a1B-ULkTgOMw

查看更多
登录 后发表回答