How to merge XML (with XSLT?)

Discussion in 'XML' started by Victor, Feb 20, 2004.

  1. Victor

    Victor Guest

    Hi, this might sound silly but I cannot figure out how to incorporate
    some XML of employees into some XML of a company to give new XML
    containing them all.

    For example, this is the company now


    <COMPANY>
    <EMPLOYEE currentEmployee="true">
    <NAME>Victor</NAME>
    <AGE>37</AGE>
    </EMPLOYEE>
    </COMPANY>



    And this is other XML that I want to add to the company


    <EMPLOYEE currentEmployee="true">
    <NAME>Thomas</NAME>
    <AGE>39</AGE>
    </EMPLOYEE>

    <EMPLOYEE currentEmployee="false">
    <NAME>Violeta</NAME>
    <AGE>34</AGE>
    </EMPLOYEE>



    And the XML I want to generate is


    <COMPANY>

    <EMPLOYEE currentEmployee="true">
    <NAME>Victor</NAME>
    <AGE>37</AGE>
    </EMPLOYEE>

    <EMPLOYEE currentEmployee="true">
    <NAME>Thomas</NAME>
    <AGE>39</AGE>
    </EMPLOYEE>

    <EMPLOYEE currentEmployee="false">
    <NAME>Violeta</NAME>
    <AGE>34</AGE>
    </EMPLOYEE>

    </COMPANY>

    I think I should be using XSLT but just cannot figure out the syntax.
    Could someone help me out with what the XSLT should look like for this
    example (if it is XSLT I should be using)?

    Thank you again everyone for your help.
    Regards
    Victor
    Victor, Feb 20, 2004
    #1
    1. Advertising

  2. Victor

    Derek Harmon Guest

    "Victor" <> wrote in message news:...
    > Hi, this might sound silly but I cannot figure out how to incorporate
    > some XML of employees into some XML of a company to give new XML
    > containing them all.

    : :
    > And this is other XML that I want to add to the company

    : :
    > <EMPLOYEE currentEmployee="true">
    > <NAME>Thomas</NAME>
    > <AGE>39</AGE>
    > </EMPLOYEE>
    >
    > <EMPLOYEE currentEmployee="false">
    > <NAME>Violeta</NAME>
    > <AGE>34</AGE>
    > </EMPLOYEE>


    Given each of these EMPLOYEE elements are in their own XML instance
    document,

    - - - merger.xsl
    <?xml version="1.0" ?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <!-- Receives the file name of the document containing the employee to add.
    -->
    <xsl:param name="employeeUrl" />

    <!-- Your sample excludes an XML declaration, and is indented, so this tells
    the XSLT processor to emit the same way.
    -->
    <xsl:eek:utput omit-xml-declaration="yes" indent="yes"/>

    <!-- Copies verbatim any elements or attributes not explicitly
    matched by another template rule.
    -->
    <xsl:template match="@*|node()">
    <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
    </xsl:template>

    <!-- Matches the source XML document's root node. This template
    rule re-emits the source XML but appends the document element
    of the XML instance document passed into the employeeUrl before
    emitting the closing COMPANY tag.
    -->
    <xsl:template match="/">
    <COMPANY>
    <xsl:apply-templates select="COMPANY/EMPLOYEE" />
    <xsl:if test="$employeeUrl">
    <xsl:apply-templates select="document($employeeUrl)/EMPLOYEE" />
    </xsl:if>
    </COMPANY>
    </xsl:template>

    </xsl:stylesheet>
    - - -

    On the other hand, if all of the EMPLOYEE elements are in one XML instance
    document, then they must be contained in a document element, i.e.,

    <EMPLOYEES>
    <EMPLOYEE> <!-- . . . --> </EMPLOYEE>
    <EMPLOYEE> <!-- . . . --> </EMPLOYEE>
    </EMPLOYEES>

    because an XML instance document can have only one top-level document
    element. If this is the case, then the above XSLT transform requires a minor
    revision.

    : :
    > I think I should be using XSLT but just cannot figure out the syntax.
    > Could someone help me out with what the XSLT should look like for this
    > example (if it is XSLT I should be using)?


    Sure, see the above. It can be done in XSLT, but as always, there are 33
    ways to skin an armadillo (if for some reason, you want to skin an armadillo).

    If you want to see a solution written in VB.NET to a very similar problem, that
    does not involve any XSLT whatsoever, please see my reply in the following
    newsgroup thread (watch for line-wrap in the hyperlink),

    http://groups.google.com/groups?selm=Ohfss2C#&oe=UTF-8&output=gplain


    Hope this has been of help,


    Derek Harmon
    Derek Harmon, Feb 21, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stylus Studio
    Replies:
    0
    Views:
    651
    Stylus Studio
    Aug 3, 2004
  2. Jacinle Young
    Replies:
    3
    Views:
    445
    Jacinle Young
    Jun 28, 2004
  3. Klaus Baumgart

    XSLT to merge two files

    Klaus Baumgart, Oct 19, 2005, in forum: XML
    Replies:
    2
    Views:
    642
    Tjerk Wolterink
    Oct 19, 2005
  4. Replies:
    4
    Views:
    667
  5. jkflens
    Replies:
    2
    Views:
    1,458
    jkflens
    May 30, 2006
Loading...

Share This Page