[xsl] using key to merge data

Subject: [xsl] using key to merge data
From: Terry Ofner <tofner@xxxxxxxxxxx>
Date: Mon, 22 Oct 2007 15:51:37 -0400
Greetings,

I am trying to merge a set of standards into another xml file. I have tried using document() pointing to a separate file as well as incorporating the standards into the stylesheet. With both methods I run into a similar problem. The standards information merges fine. However, it removes the sibling nodes from tree. Here is the basic structure of the xml file I am importing into:

<top>
	<question>
		<info>
			<topic>Vocabulary</topic>
			<keyword>Prefixes</keyword>
			<notes>G4U1S01></notes>
			<foo>blah blah</foo>
		</info>
	</question>
</top>

The standards.xml file looks like this:

<top>
	<standard>
		<g_code>G4U1S01</g_code>
		<state-obj state="AZ">blah blah blah</state-obj>
		<state-obj state="TX">blah blah blah</state-obj>
		. . .
	</standard>
</top>


When I run the stylesheet below, the <notes> node along with all the sibling nodes are removed from the <info> node.


<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
   <xsl:import href="copy.xslt"/>
   <xsl:output method="xml"/>

   <xsl:variable name="standardFile" select="document('standards.xml')"/>
   <xsl:key name="standard_by_g_code" match="standard" use="g_code"/>

 <xsl:template match="/">
    <xsl:copy>
      <xsl:apply-templates/>
    </xsl:copy>
 </xsl:template>

 <xsl:template match="question/info">
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:variable name="question_g_code" select="notes"/>
      <xsl:for-each select="$standardFile">
        <xsl:copy-of select="key('standard_by_g_code', $question_g_code)"/>
      </xsl:for-each>
    </xsl:copy>
 </xsl:template>
</xsl:stylesheet>


The current result looks like this:


<top>
	<question>
		<info>
		<standard>
		<g_code>G4U1S01</g_code>
		<state-obj state="AZ">blah blah blah</state-obj>
		<state-obj state="TX">blah blah blah</state-obj>
		. . .
		</standard>
		</info>
	</question>
</top>

I am looking for this:

<top>
	<question>
		<info>
			<topic>Vocabulary</topic>
			<keyword>Prefixes</keyword>
			<notes>G4U1S01></notes>
			<foo>blah blah</foo>
			<state-obj state="AZ">blah blah blah</state-obj>
			<state-obj state="TX">blah blah blah</state-obj>
			. . .
		</info>
	</question>
</top>


No doubt there something simple that I am doing wrong.


FYI: the <xsl:import href="copy.xslt"/> points to this stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:template match="node( ) | @*">
  <xsl:copy>
    <xsl:apply-templates select="@* | node( )"/>
  </xsl:copy>
</xsl:template>
</xsl:stylesheet>

Thanks in advance.

Terry

Current Thread