RE: [xsl] many-to-many

Subject: RE: [xsl] many-to-many
From: "Brown, William S" <wsbrown@xxxxxxx>
Date: Tue, 30 Jan 2007 09:47:28 -0500
Ronan Klyne wrote:
> It could be done easily with a second key to map from part id to part:
> <xsl:key name="pid-p" match="/doc/parts/part" use="@id"/>
>
> Then instead of using key('i-p', @id) to in the loop in the invoice
> template, get only the part id, you can use key('pid-p', key('i-p',
> @id)/@id) to get the part, which you can then do what you like with,
> including get the child nodes (partLoc).
>
> This is just one solution, and almost certainly not the best; I could
> not figure out a way of having the key 'i-p' give the fuller part
> details. Ideas anyone?

Again, thank you. I had come (after much flailing around) to the realization
that I needed to add another key, which returned 'part'. I might (eventually)
have come to the point of seeing how to make the first key function the
argument of the added key function (well, maybe).
Is your suggestion then that I place something like
<xsl:value-of select="key('pid-p', key('i-p', @id)/@id)????"/>
within the for-each at the point indicated below?
(I'm apparently misunderstanding what's being returned by the key function, or
how to use it - because what I've written doesn't work. Entirely *my* error
I'm sure. Can I not use a value-of here? I would have thought that what is
returned at this point is unique.)
Asking your indulgence...how exactly do I get to the partLoc?

<doc>
  <invoices>
    <invoice id="i1">
	<date>01/01/07</date>
      <part id="1"/>
      <part id="2"/>
    </invoice>
    <invoice id="i2">
	<date>01/02/07</date>
      <part id="5"/>
      <part id="2"/>
    </invoice>
    <invoice id="i3">
	<date>01/03/07</date>
      <part id="5"/>
      <part id="3"/>
    </invoice>
    <invoice id="i4">
	<date>01/04/07</date>
      <part id="5"/>
      <part id="2"/>
    </invoice>
    <invoice id="i5">
	<date>01/05/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>


<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:key name="pid-p" match="/doc/parts/part" use="@id"/>

<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: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"/><BR/>

	<!-- Key function here? -->

  </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)"><BR/>
    <xsl:value-of select="@id"/>
  </xsl:for-each>
</xsl:template>


</xsl:stylesheet>

Current Thread