xsl-list
[Top] [All Lists]

Conditioned merge of XML from two files

2005-07-27 13:43:42
Hello Experts,

I have a question resulting from a problem I could not resolve. I have one
XML file which contains information about books (books.xml). I have a second
xml file which contains information about book locations (locations.xml). I
have a location id in books.xml and each location in locations.xml has a
location id of the same sort. I would like to have a stylesheet that looks
up the ids from books.xml in locations.xml and copies the location into a
certain position of the book. After the transformation, each book in
books.xml has a location XML stubstructure.

Here example data from both XML files:

(books.xml - only one book to keep it short)

<bookshelf>
  <book>
    <id>1</id>
    <title>Alice in Wonderland</title>
    <desc>Alice is tumbling down the rabit hole.</desc>
    <location>
      <loc_id>L2</loc_id>
    </location>
  </book>
  ... and many many more ...
</bookshelf>


(locations.xml - only one location to keep it short)

<locations>
  <location>
    <loc_id>L1</loc_id>
    <name>Location 1</name>
    ... many more attributes...
  </location>
  .. many more locations
</locations>


After the transformation I wouild like to have the following:

<bookshelf>
  <book>
    <id>1</id>
    <title>Alice in Wonderland</title>
    <desc>Alice is tumbling down the rabit hole.</desc>
    <location>
      <loc_id>L1</loc_id>
      <name>Location 1</name>
      ... many more attributes...
    </location>
  </book>
  ... and many many more ...
</bookshelf>


I have developed the following XSLT:


<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
  <xsl:output method="xml" indent="yes" encoding="UTF-8" />
  <xsl:strip-space elements="*" />
  <xsl:variable name="locationFile" select="document('locations.xml')" />
  
  <!-- Match with root -->
  <xsl:template match="/">
    <bookshelf>
        <xsl:for-each select="/bookshelf/book">
                <book>
                                <xsl:apply-templates select="." />
                </book>
        </xsl:for-each>
    </bookshelf>    
   </xsl:template>

  <xsl:template match="event">
        <xsl:copy-of select="./id" />
        <xsl:copy-of select="./title" />
        <xsl:copy-of select="./desc" />
        <location>
                <xsl:copy-of
select="$locationFile/locations/location/*[.//location/loc_id=$locationFile/locations//location/id]"/>
        </location>
   </xsl:template>
        
</xsl:stylesheet>



... however, it does not work: What is wrong? I guess it is far from optimal
anyway so I am happy to get completely different solutions as well. As you
can see in the stylesheet, I would like to "hardcode" the lcoations file,
but provide the book data as parameter (for flexiblity reasons).

Any form of help would be highly appreciated.

Kind regards,
Karl





-- 
5 GB Mailbox, 50 FreeSMS http://www.gmx.net/de/go/promail
+++ GMX - die erste Adresse für Mail, Message, More +++

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



<Prev in Thread] Current Thread [Next in Thread>