【问题标题】:Concatenate xml file in ant script在ant脚本中连接xml文件
【发布时间】:2016-06-12 06:43:49
【问题描述】:

我是 ant 脚本的新手。我正在寻找如何将文件夹中的所有 xml 文件连接到 ant 脚本中的单个 xml 文件中。

在我的项目中,将在一个文件夹中动态生成 n 个 xml 文件,例如:server1.xml、manager.xml、server2.xml、server3.xml。我需要将所有文件名中包含 server 的 xml 文件(server1.xml、server2.xml、server3.xml)合并到一个 xml 中,例如:server.xml。并且需要在jboss中部署它。

我发现使用 xmltask 将内容从一个 xml 文件复制到另一个。 我不确定如何将文件夹中的所有 xml 文件内容复制到 ant 脚本中的单个 xml 文件中。

感谢任何帮助。

【问题讨论】:

    标签: xml ant xml-parsing xslt-2.0


    【解决方案1】:

    好吧,假设使用的 XSLT 2.0 处理器是 Saxon 9 那么你可以按照例如

    <xsl:param name="folder-uri" select="'file:/root/users/foo/foldername'"/>
    <xsl:param name="selectPattern" select="'server*.xml'"/>
    <!-- Collection of documents -->
    <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】:

      试试这个

      连接一系列文件,仅在比所有源文件更旧时更新目标文件:

      示例代码:

       <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>
      

      避免根标签:

      <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>
      

      【讨论】:

      猜你喜欢
      • 2016-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-18
      • 1970-01-01
      相关资源
      最近更新 更多