Problem combining two XMLs using XSLT

S

Stefan

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
 
M

Martin Honnen

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

fischer

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>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,001
Messages
2,570,254
Members
46,851
Latest member
CliftonCor

Latest Threads

Top