RE: [xsl] many-to-many

Subject: RE: [xsl] many-to-many
From: "Brown, William S" <wsbrown@xxxxxxx>
Date: Mon, 29 Jan 2007 14:39:57 -0500
What I meant to say was...
That is exactly what I had in mind. Thank you.
Quite a bit cleaner than any example I was able to find on the web
(but maybe I was looking in the wrong places).
However, I've been away from XSL for a while - and wasn't ever real good at
it.
I haven't figured out yet how the 'part' template works yet and
I don't see how I would output elements (e.g., partLoc below) along with the
part id.
It's simple, right? (ducking qand running)

 <doc>
   <invoices>
     <invoice id="i1">
	 <date>01/01/07</date>
       <part id="1"/>
       <part id="2"/>
     </invoice>
     <invoice id="i2">
	 <date>01/01/07</date>
       <part id="5"/>
       <part id="2"/>
     </invoice>
     <invoice id="i3">
	 <date>01/01/07</date>
       <part id="5"/>
       <part id="3"/>
     </invoice>
     <invoice id="i4">
	 <date>01/02/07</date>
       <part id="5"/>
       <part id="2"/>
     </invoice>
     <invoice id="i5">
	 <date>01/01/07</date>
       <part id="3"/>
       <part id="4"/>
     </invoice>
   </invoices>
   <parts>
	<part name="part1" id="1">
		<partLoc>A</partLoc>
	</part>
	<part name="part2" id="2">
		<partLoc>B</partLoc>
	</part>
	<part name="part3" id="3">
		<partLoc>C</partLoc>
	</part>
	<part name="part4" id="4">
		<partLoc>D</partLoc>
	</part>
   </parts>
 </doc>

Ronan Klyne wrote:
> I read the many-to-many problem as two one-to-many problems,
> one in each
> direction, for which keys are a good solution.
> As far as I know, it is not possible to use a key in reverse, so two
> keys must be used; one for each direction.
>
> I include an example involving parts and invoices. Is this the kind of
> thing you had in mind?
>
> 	# r
>
> ----- Input data:
> <doc>
>   <invoices>
>     <invoice id="i1">
>       <part id="1"/>
>       <part id="2"/>
>     </invoice>
>     <invoice id="i2">
>       <part id="5"/>
>       <part id="2"/>
>     </invoice>
>     <invoice id="i3">
>       <part id="5"/>
>       <part id="3"/>
>     </invoice>
>     <invoice id="i4">
>       <part id="5"/>
>       <part id="2"/>
>     </invoice>
>     <invoice id="i5">
>       <part id="3"/>
>       <part id="4"/>
>     </invoice>
>   </invoices>
>   <parts>
>     <part name="part1" id="1"/>
>     <part name="part2" id="2"/>
>     <part name="part3" id="3"/>
>     <part name="part4" id="4"/>
>   </parts>
> </doc>
>
> ----- Stylesheet:
> <?xml version="1.0" encoding="ISO-8859-1"?>
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
> version="1.0">
> <xsl:output method="html" indent="no" encoding="ISO-8859-1"/>
>
> <xsl:key name="p-i" match="/doc/invoices/invoice" use="part/@id"/>
> <xsl:key name="i-p" match="/doc/invoices/invoice/part" use="../@id"/>
>
> <xsl:template match="invoice">
>   <h3>invoice <xsl:value-of select="@id"/> contains:</h3>
>   <xsl:for-each select="key('i-p', @id)">
>     part <xsl:value-of select="@id"/>
>   </xsl:for-each>
> </xsl:template>
>
> <xsl:template match="part">
>   <h3>part <xsl:value-of select="@id"/> was part of invoices:</h3>
>
>   <xsl:for-each select="key('p-i', @id)">
>     <xsl:value-of select="@id"/>
>   </xsl:for-each>
> </xsl:template>
>
> <xsl:template match="doc">
>   <h2>invoice to parts</h2>
>   <xsl:apply-templates select="invoices/*"/>
>   <h2>part to invoices</h2>
>   <xsl:apply-templates select="parts/*"/>
> </xsl:template>
>
> </xsl:stylesheet>

Current Thread