merging two XML files

S

Stefan Franke

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"
xmlns:xsl="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>
<xsl:eek:therwise>
<xsl:value-of select="TITEL"/><br />
</xsl:eek:therwise>
</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>
 
J

Joris Gillis

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,
 
S

Stefan Franke

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
 
J

Joris Gillis

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"
 
S

Stefan Franke

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
 
J

Joris Gillis

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,
 

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

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top