问题
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>
回答1:
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="<\?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>
回答2:
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
来源:https://stackoverflow.com/questions/35707454/how-to-avoid-xml-header-while-concatenating-xml-files-in-ant-script