RE: [xsl] Conditioned merge of XML from two files

Subject: RE: [xsl] Conditioned merge of XML from two files
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Wed, 27 Jul 2005 23:11:56 +0100
What's happening here:

    		<xsl:copy-of
select="$locationFile/locations/location/*[.//location/loc_id=$locationFile/
locations//location/id]"/>

is that you're (very) muddled about the context node. You want to select a
location that satisfies a condition. Write a path that selects all the
locations, then a predicate containing the condition. Inside the predicate,
"." is the location you are testing, and current() is the book you are
testing it against (you had a typo, match="event"). So you want

$locationFile/locations/location[./loc_id = current()/location/loc_id]

To refine this:

(a) "./" is redundant and can be omitted - but I included it for clarity and
you may want to do the same

(b) if there are many locations, you can make this more efficient using
keys.

Michael Kay
http://www.saxonica.com/

> -----Original Message-----
> From: Karl Koch [mailto:TheRanger@xxxxxxx] 
> Sent: 27 July 2005 21:44
> To: Mulberry list
> Subject: [xsl] Conditioned merge of XML from two files
> 
> 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 +++

Current Thread