Problem combining two XMLs using XSLT

Discussion in 'XML' started by Stefan, Jan 26, 2006.

  1. Stefan

    Stefan Guest

    Hi there!

    I'm very new to XML and XSLT, so while working i encountered the
    following, probably very easy to solve, problem.

    i've got 2 XML-files, which look like this:

    LinesOfCode.xml:
    <?xml version="1.0"?>
    <component name="ORCA_Common">
    <LOC-netto>2100</LOC-netto>
    <LOC-blank>200</LOC-blank>
    <LOC-comment>400</LOC-comment>
    <LOC>2700</LOC>
    <Comment-netto>20%</Comment-netto>
    </component>

    CodeCoverage.xml:
    <?xml version="1.0"?>
    <component name="ORCA_Common">
    <Coverage>50%</Coverage>
    </component>

    Both should be combined to a "component.xml", lookinf like this:
    <?xml version="1.0"?>
    <component name="ORCA_Common">
    <LOC-netto>2100</LOC-netto>
    <LOC-blank>200</LOC-blank>
    <LOC-comment>400</LOC-comment>
    <LOC>2700</LOC>
    <Comment-netto>20%</Comment-netto>
    <Coverage>50%</Coverage>
    </component>

    so just adding the data of the one XML to the data of the other.

    I wanted to use a primary source, referring to both files, looking like
    this:
    <?xml version="1.0"?>
    <index>
    <entry>LinesOfCode</entry>
    <entry>CodeCoverage</entry>
    </index>

    and logically the document() funcition in the xsl-file.

    Could you please tell me how to do this? i tried several times with
    lots of different XSL-files, but nothing seems to work.

    Thanks in advance!

    Greets, Stefan
    Stefan, Jan 26, 2006
    #1
    1. Advertising

  2. Stefan wrote:


    > I wanted to use a primary source, referring to both files, looking like
    > this:
    > <?xml version="1.0"?>
    > <index>
    > <entry>LinesOfCode</entry>
    > <entry>CodeCoverage</entry>


    What are these entries, the names of the files without the suffix (e.g.
    file name would be LinesOfCode.xml for the first entry)?

    > and logically the document() funcition in the xsl-file.


    Well you could simply e.g.
    <xsl:template match="entry">
    <xsl:apply-templates select="document(concat(., '.xml'))/component" />
    to process the component elements in the file the entry element points
    to. Is that what you want to know, how to use the document function?

    Here is an example stylesheet

    <xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

    <xsl:eek:utput method="xml" indent="yes" />

    <xsl:template match="/index">
    <components>
    <xsl:apply-templates select="entry[position() mod 2 = 1]" />
    </components>
    </xsl:template>

    <xsl:template match="entry">
    <xsl:apply-templates select="document(concat(., '.xml'), /)/component">
    <xsl:with-param name="secondFile"
    select="concat(following-sibling::entry[1], '.xml')" />
    </xsl:apply-templates>
    </xsl:template>

    <xsl:template match="component">
    <xsl:param name="secondFile" />
    <xsl:copy>
    <xsl:apply-templates select="@* | node()" />
    <xsl:apply-templates select="document($secondFile,
    /)/component[@name = current()/@name]/node()" />
    </xsl:copy>
    </xsl:template>

    <xsl:template match="@* | node()">
    <xsl:copy>
    <xsl:apply-templates select="@* | node()" />
    </xsl:copy>
    </xsl:template>

    </xsl:stylesheet>

    I assumed your index file would have several entry elements where each
    pair has to be processed together. Not sure if that is what you need.


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Jan 26, 2006
    #2
    1. Advertising

  3. Stefan

    Guest

    Hi Stefan,

    this is a typical merging-task.

    I have not an XSLT-solution,
    but with the product <xml>cmp (http://www.xmlcmp.com) you can solve
    such tasks
    and even much more complicated merging tasks.

    For merging with <xml>cmp you need a basic-control-file and a
    merge-control-file.

    The basic-control-file (cmp1.xml) for your example would be:

    <?xml version="1.0" encoding="UTF-8"?>
    <component ident_att_name="true" >
    <LOC-netto cmp_text="true" />
    <LOC-comment cmp_text="true" />
    <LOC cmp_text="true" />
    <Coverage cmp_text="true" />
    <Comment-netto cmp_text="true" />
    <LOC-blank cmp_text="true" />
    </component>

    The merge-control-file (merge1.xml) would be:

    <?xml version="1.0" encoding="UTF-8"?>
    <merge>
    <identity path='/component' merge='333' >
    <value path='/LOC-netto' merge='3332' />
    <value path='/LOC-comment' merge='3332' />
    <value path='/LOC' merge='3332' />
    <value path='/Coverage' merge='3332' />
    <value path='/Comment-netto' merge='3332' />
    <value path='/LOC-blank' merge='3332' />
    </identity>
    </merge>

    The merge-attributes contain the merging-rules.
    You can define very detailed merging-rules.
    For further informations about these merging rules look at
    http://www.xmlcmp.com.

    Merging the two files:

    $ xmlmerge.sh cmp1.xml merge1.xml example1a.xml example1b.xml >
    result1.xml

    Content of result-file (result1.xml):

    <component name='ORCA_Common'>
    <LOC-netto>2100</LOC-netto>
    <LOC-blank>200</LOC-blank>
    <LOC-comment>400</LOC-comment>
    <LOC>2700</LOC>
    <Comment-netto>20%</Comment-netto>
    <Coverage>50%</Coverage>
    </component>











    Stefan wrote:
    > Hi there!
    >
    > I'm very new to XML and XSLT, so while working i encountered the
    > following, probably very easy to solve, problem.
    >
    > i've got 2 XML-files, which look like this:
    >
    > LinesOfCode.xml:
    > <?xml version="1.0"?>
    > <component name="ORCA_Common">
    > <LOC-netto>2100</LOC-netto>
    > <LOC-blank>200</LOC-blank>
    > <LOC-comment>400</LOC-comment>
    > <LOC>2700</LOC>
    > <Comment-netto>20%</Comment-netto>
    > </component>
    >
    > CodeCoverage.xml:
    > <?xml version="1.0"?>
    > <component name="ORCA_Common">
    > <Coverage>50%</Coverage>
    > </component>
    >
    > Both should be combined to a "component.xml", lookinf like this:
    > <?xml version="1.0"?>
    > <component name="ORCA_Common">
    > <LOC-netto>2100</LOC-netto>
    > <LOC-blank>200</LOC-blank>
    > <LOC-comment>400</LOC-comment>
    > <LOC>2700</LOC>
    > <Comment-netto>20%</Comment-netto>
    > <Coverage>50%</Coverage>
    > </component>
    >
    > so just adding the data of the one XML to the data of the other.
    >
    > I wanted to use a primary source, referring to both files, looking like
    > this:
    > <?xml version="1.0"?>
    > <index>
    > <entry>LinesOfCode</entry>
    > <entry>CodeCoverage</entry>
    > </index>
    >
    > and logically the document() funcition in the xsl-file.
    >
    > Could you please tell me how to do this? i tried several times with
    > lots of different XSL-files, but nothing seems to work.
    >
    > Thanks in advance!
    >
    > Greets, Stefan
    , Jan 27, 2006
    #3
    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. BaKMaN
    Replies:
    0
    Views:
    376
    BaKMaN
    Jan 30, 2004
  2. maalej

    2 xmls

    maalej, Apr 23, 2005, in forum: XML
    Replies:
    1
    Views:
    518
    Dimitre Novatchev
    Apr 23, 2005
  3. maalej

    edit xmls like

    maalej, Aug 16, 2005, in forum: XML
    Replies:
    3
    Views:
    481
    Peter Flynn
    Aug 18, 2005
  4. Replies:
    1
    Views:
    411
    Andy Dingley
    Dec 21, 2006
  5. Nick
    Replies:
    1
    Views:
    391
    Joseph J. Kesselman
    Jun 17, 2008
Loading...

Share This Page