merging two XML files

Discussion in 'XML' started by Stefan Franke, Jan 4, 2005.

  1. 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>
     
    Stefan Franke, Jan 4, 2005
    #1
    1. Advertising

  2. Stefan Franke

    Joris Gillis Guest

    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-view.cgi?userid=38041)
    Spread the wiki (http://www.wikipedia.org)
     
    Joris Gillis, Jan 4, 2005
    #2
    1. Advertising

  3. > <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
     
    Stefan Franke, Jan 4, 2005
    #3
  4. Stefan Franke

    Joris Gillis Guest

    > 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-view.cgi?userid=38041)
    Spread the wiki (http://www.wikipedia.org)
     
    Joris Gillis, Jan 4, 2005
    #4
  5. > 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
     
    Stefan Franke, Jan 4, 2005
    #5
  6. Stefan Franke

    Joris Gillis Guest

    >> 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-view.cgi?userid=38041)
    Spread the wiki (http://www.wikipedia.org)
     
    Joris Gillis, Jan 4, 2005
    #6
    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. GenxLogic
    Replies:
    3
    Views:
    1,379
    andrewmcdonagh
    Dec 6, 2006
  2. Replies:
    5
    Views:
    591
    Jerry Coffin
    Sep 24, 2006
  3. n o s p a m p l e a s e

    Merging two DLL files

    n o s p a m p l e a s e, Jun 18, 2007, in forum: C Programming
    Replies:
    10
    Views:
    1,290
    CBFalconer
    Jun 19, 2007
  4. triangle
    Replies:
    1
    Views:
    120
    Gunnar Hjalmarsson
    Jan 30, 2004
  5. Replies:
    8
    Views:
    204
Loading...

Share This Page