Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Problem combining two XMLs using XSLT

Reply
Thread Tools

Problem combining two XMLs using XSLT

 
 
Stefan
Guest
Posts: n/a
 
      01-26-2006
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

 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      01-26-2006


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
xmlnssl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xslutput 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">
<xslaram 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/
 
Reply With Quote
 
 
 
 
fischer@sofika.de
Guest
Posts: n/a
 
      01-27-2006
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Comparing two xmls based on keys.... Nick XML 1 06-17-2008 03:39 PM
Receiving XMLs over the Internet suspended@gmail.com XML 1 12-21-2006 08:53 PM
edit xmls like maalej XML 3 08-17-2005 11:59 PM
2 xmls maalej XML 1 04-23-2005 08:51 AM
XML/XSLT Combining queries into XML ? BaKMaN XML 0 01-30-2004 11:01 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57