merging two XML files

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stefan Franke

    merging two XML files

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

    <xsl:variable name="doc1" select="/" />
    <xsl:variable name="doc2" select="documen t('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:otherwis e>
    <xsl:value-of select="TITEL"/><br />
    </xsl:otherwise>
    </xsl:choose>
    </xsl:for-each>
    </xsl:template>

    </xsl:stylesheet>

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

    second XML file:

    <?xml-stylesheet type="text/xsl" href="Bestselle r.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>


  • Joris Gillis

    #2
    Re: merging two XML files

    Hi,
    [color=blue]
    > 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...[/color]

    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)

    Comment

    • Stefan Franke

      #3
      Re: merging two XML files

      > <xsl:when test="($doc1/BESTSELLER/EINTRAG/TITEL) = TITEL">[color=blue]
      > and it will probably work.[/color]

      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


      Comment

      • Joris Gillis

        #4
        Re: merging two XML files

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

        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)

        Comment

        • Stefan Franke

          #5
          Re: merging two XML files

          > Try this one:[color=blue]
          > "$doc2/BUCHBESTAND/BUCH[($doc2/BUCHBESTAND/BUCH/TITEL) = TITEL]/AUTOR"[/color]


          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


          Comment

          • Joris Gillis

            #6
            Re: merging two XML files

            >> Try this one:[color=blue][color=green]
            >> "$doc2/BUCHBESTAND/BUCH[($doc2/BUCHBESTAND/BUCH/TITEL) = TITEL]/AUTOR"[/color]
            >
            > No, doesn't work either[/color]

            Ok, I misread your previous question, try this:
            "$doc2/BUCHBESTAND/BUCH[TITEL = current()/TITEL]/AUTOR"
            [color=blue]
            > 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):[/color]

            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)

            Comment

            Working...