Concatenate xml file in ant script

五迷三道 提交于 2019-12-02 17:48:54

问题


I am new to ant script. I am looking for how to concatenate all the xml file in a folder into a single xml file in ant script.

In my project n number of xml files will be generated dynamically in a folder eg: server1.xml, manager.xml, server2.xml, server3.xml. I need to merge all the xml files having server in their filename alone (server1.xml, server2.xml, server3.xml) into a single xml eg: server.xml. and need to deploy it in jboss.

I have found that copying content from one xml file to another using xmltask. I am not sure how to copy all the xml files content in a folder into a single xml file in ant script.

Any help is appreciated.


回答1:


Well, assuming the XSLT 2.0 processor used is Saxon 9 then you can do it along the lines of e.g.

<xsl:param name="folder-uri" select="'file:/root/users/foo/foldername'"/>
<xsl:param name="select-pattern" select="'server*.xml'"/>
<xsl:variable name="docs" select="collection(concat($folder-uri, '?select=', $selectPattern))"/>

<xsl:template name="main" match="/">
  <xsl:element name="{name($docs[1]/*)}" namespace="{namespace-uri($docs[1]/*)}">
    <xsl:copy-of select="$docs/*/node()"/>
  </xsl:element>
</xsl:template>



回答2:


try this

Concatenate a series of files, update the destination file only if is older that all the source files:

Sample code:

 <concat destfile="${docbook.dir}/all-sections.xml"
          force="no">
    <filelist dir="${docbook.dir}/sections"
         files="introduction.xml,overview.xml"/>
    <fileset dir="${docbook.dir}"
         includes="sections/*.xml"
         excludes="introduction.xml,overview.xml"/>
  </concat>

avoids root tag:

<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://stackoverflow.com/questions/35695965/concatenate-xml-file-in-ant-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!