Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > merging two XML files

Reply
Thread Tools

merging two XML files

 
 
Stefan Franke
Guest
Posts: n/a
 
      01-04-2005
Hi,
I would like to merge two XML files. The first XML contains a list of books,
the second XML file contains a bestseller list of books. So some of the
books in the first file will appear in the second file.

I want to merge the two files, so that the ones, that also exist in the
second XML file are printed in bold. I am trying to compare the titles of
the books, but this doesn't seem to work.

Could anybody give me a hint how to solve this? I guess this XPath statement
ind the <xsl:when...> is not quite right...

Thanks,
Stefan



<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0"
xmlnssl="http://www.w3.org/1999/XSL/Transform">

<xsl:variable name="doc1" select="/" />
<xsl:variable name="doc2" select="document('Buchbestand.xml')" />

<xsl:template match="/">
<xsl:for-each select="$doc2/BUCHBESTAND/BUCH">
<xsl:choose>
<xsl:when test="($doc1/BESTSELLER/EINTRAG/TITEL) =
($doc2/BUCHBESTAND/BUCH/TITEL)">
<b><xsl:value-of select="TITEL"/></b><br />
</xsl:when>
<xsltherwise>
<xsl:value-of select="TITEL"/><br />
</xsltherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>

.................................................. ..................

second XML file:

<?xml-stylesheet type="text/xsl" href="Bestseller.xsl"?>
<BESTSELLER>
<EINTRAG>
<TITEL>...</TITEL>
<VERLAG>...</VERLAG>
<JAHR>...</JAHR>
</EINTRAG>
<EINTRAG>
....
</BESTSELLER>

.................................................. ..................

first XML file:

<BUCHBESTAND>
<BUCH Einband="..." Lagernd="...">
<TITEL>...</TITEL>
<AUTOR>...</AUTOR>
<SEITEN>...</SEITEN>
<PREIS>...</PREIS>
</BUCH>
....
</BUCHBESTAND>


 
Reply With Quote
 
 
 
 
Joris Gillis
Guest
Posts: n/a
 
      01-04-2005
Hi,

> I want to merge the two files, so that the ones, that also exist in the
> second XML file are printed in bold. I am trying to compare the titles of
> the books, but this doesn't seem to work.
>
> Could anybody give me a hint how to solve this? I guess this XPath statement
> ind the <xsl:when...> is not quite right...


It is indeed not correct. Change it to:
<xsl:when test="($doc1/BESTSELLER/EINTRAG/TITEL) = TITEL">
and it will probably work.

regards,
--
Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)
Spread the wiki (http://www.wikipedia.org)
 
Reply With Quote
 
 
 
 
Stefan Franke
Guest
Posts: n/a
 
      01-04-2005
> <xsl:when test="($doc1/BESTSELLER/EINTRAG/TITEL) = TITEL">
> and it will probably work.


Thanks very much, that was it. However I ran into another problem when
merging those two files. When I'm iterating through the elements with a
for-each statement I always get the current element from the XML file in the
for-each statement. When I'm trying to access information that's from the
other XML file, it fails.

<xsl:for-each select="$doc1/BESTSELLER/EINTRAG">
<xsl:if test="($doc2/BUCHBESTAND/BUCH/TITEL) = TITEL">
<b><xsl:value-of select="TITEL"/></b><br />
<xsl:value-of
select="$doc2/BUCHBESTAND/BUCH/AUTOR[($doc2/BUCHBESTAND/BUCH/TITEL) =
TITEL]"/><br />
</xsl:if>
</xsl:for-each>

I'm trying to work around this problem with an XPath statement
("$doc2/BUCHBESTAND/BUCH/AUTOR[($doc2/BUCHBESTAND/BUCH/TITEL) = TITEL]"),
but so far there has been no success.

Does anyone have any ideas on that problem?

Thanks,
Stefan


 
Reply With Quote
 
Joris Gillis
Guest
Posts: n/a
 
      01-04-2005
> I'm trying to work around this problem with an XPath statement
> "$doc2/BUCHBESTAND/BUCH/AUTOR[($doc2/BUCHBESTAND/BUCH/TITEL) = TITEL]"
> but so far there has been no success.


This cannot work since the 'AUTOR' element has no 'TITLE' child to perform an equality test.

Try this one:
"$doc2/BUCHBESTAND/BUCH[($doc2/BUCHBESTAND/BUCH/TITEL) = TITEL]/AUTOR"
--
Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)
Spread the wiki (http://www.wikipedia.org)
 
Reply With Quote
 
Stefan Franke
Guest
Posts: n/a
 
      01-04-2005
> Try this one:
> "$doc2/BUCHBESTAND/BUCH[($doc2/BUCHBESTAND/BUCH/TITEL) = TITEL]/AUTOR"



No, doesn't work either, but it certainly brought me nearer to the solution,
that I found just a few minutes ago - one has to define a variable that
holds the current TITEL):

<xsl:for-each select="$doc1/BESTSELLER/EINTRAG">

<xsl:variable name="titel" select="TITEL" />

<xsl:if test="($doc2/BUCHBESTAND/BUCH/TITEL) = TITEL">
<b><xsl:value-of select="TITEL"/></b><br />
<xsl:value-of select="$doc2/BUCHBESTAND/BUCH[TITEL =
$titel]/AUTOR"/><br />
</xsl:if>
</xsl:for-each>

Thanks very much for your help,
Stefan


 
Reply With Quote
 
Joris Gillis
Guest
Posts: n/a
 
      01-04-2005
>> Try this one:
>> "$doc2/BUCHBESTAND/BUCH[($doc2/BUCHBESTAND/BUCH/TITEL) = TITEL]/AUTOR"

>
> No, doesn't work either


Ok, I misread your previous question, try this:
"$doc2/BUCHBESTAND/BUCH[TITEL = current()/TITEL]/AUTOR"

> it certainly brought me nearer to the solution,that I found just a few minutes ago - one has to define a variable thatholds the current TITEL):


That is indeed a working solution, but I (personally speaking) prefer to use 'current()' in my Xpath expressions rather than declaring dummy variables for reaching the same goal.

regards,
--
Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)
Spread the wiki (http://www.wikipedia.org)
 
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
Merging two DLL files n o s p a m p l e a s e C Programming 10 06-19-2007 08:57 PM
How to compare two SOAP Envelope or two Document or two XML files GenxLogic Java 3 12-06-2006 08:41 PM
Merging Two Files using C++ ckoniecny@gmail.com C++ 5 09-24-2006 07:37 PM
Merging two XML tree: xslt or dom ? Yannick Patois XML 1 07-23-2004 07:17 PM
Merging two text files based on some kind of text anchors triangle Perl Misc 1 01-30-2004 09:00 PM



Advertisments