ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "J.Pietschmann" <j3322...@yahoo.de>
Subject Re: Merging XML files
Date Fri, 05 Apr 2002 22:19:18 GMT
Drew Davidson wrote:
> I want to merge two or more XML files into one,

Use XSLT.
There is a lot of advice on how to write an XSL
transformation to merge XML files available in the
XSL list archives at http://www.mulberrytech.com/xsl/xsl-list/
One approach is to have a "master XML" with file names:
  <files>
    <file>foo.ejb</file>
    <file>bar.ejb</file>
  </files>
and some modified EJB descriptor XML
  foo.ejb
    <entity>
      <ejb-name>Foo</ejb-name>
    </entity>
and a simple style sheet:
  <?xml version="1.0"?>
  <xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="files">
      <ejb-jar>
        <enterprise-beans>
          <xsl:for-each select="document(file)/*">
            <xsl:copy-of select="."/>
          </xsl:for-each>
        </enterprise-beans>
      </ejb-jar>
    </xsl:template>
  </xsl:stylesheet>
(Untested, apply standard disclaimer)

If the files to be merged are more complicated, either
extend the the example above accordingly (may quickly
become unmaintainable) or switch to a template approach
with a master template
  <ejb-jar>
    <enterprise-beans>
      <ejb-files>
        <file>foo.ejb</file>
        <file>bar.ejb</file>
      </ejb-files>
    </enterprise-beans>
    <other-stuff/>
  </ejb-jar>
Style sheet:
  <?xml version="1.0"?>
  <xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <!-- copy template -->
    <xsl:template match="node()|@*">
      <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
    </xsl:template>
    <xsl:template match="ejb-files">
      <!-- insert ejb files -->
      <xsl:for-each select="document(file)/*">
         <xsl:copy-of select="."/>
      </xsl:for-each>
    </xsl:template>
    <xsl:template match="other-stuff">
      <!-- do whatever you need -->
    </xsl:template>
  </xsl:stylesheet>

It would be an advantage to have the elements names
"ejb-files" and "other-stuff" in another namespace,
but this is an advanced topic.
For more information on XSLT, ask on the XSL list.

I believe ant has or will have a built-in task to
run XSL transformations.

J.Pietschmann


--
To unsubscribe, e-mail:   <mailto:ant-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:ant-user-help@jakarta.apache.org>


Mime
View raw message