Re: [xsl] Correlation between two xml trees

Subject: Re: [xsl] Correlation between two xml trees
From: Joerg Heinicke <joerg.heinicke@xxxxxx>
Date: Thu, 06 Jun 2002 06:59:32 +0200
Hello John,

this "magic" should do the job ;-)

<xsl:template match="z:row">
  <data>
    <xsl:variable name="data" select="."/>
    <xsl:for-each select="$outputdef/root/column">
      <xsl:element name="{@tagname}">
        <xsl:value-of select="$data/@*[name()=current()/@dbcolumn]"/>
      </xsl:element>
    </xsl:for-each>
  </data>
</xsl:template>

With your <xsl:for-each/> you are switching the context to another XML tree (outputdef). For accessing again the data, you must switch the context back ... and for this the easiest way is to store the current z:row in a variable.

Regards,

Joerg

John Sands wrote:
I'm sorry I can't come up with a short description of what I want to
do. Instead, here is an example.

I have an exported recordset (henceforth known as tabledef) that looks like this:

  <xml xmlns:rs='urn:schemas-microsoft-com:rowset' xmlns:z='#RowsetSchema'>
    <rs:data>
      <z:row col1='1' col2='a' col3='x' />
      <z:row col1='2' col2='b' col3='y' />
      <z:row col1='3' col2='c' col3='z' />
    </rs:data>
  </xml>

and another xml document (outputdef) that looks like this:

  <root>
    <column tagname="tag1" dbcolumn="col1"/>
    <column tagname="tag2" dbcolumn="col2"/>
  </root>

I want to output this:

  <root>
    <data>
      <tag1>1</tag1>
      <tag2>a</tag2>
    </data>
    <data>
      <tag1>2</tag1>
      <tag2>b</tag2>
    </data>
    <data>
      <tag1>3</tag1>
      <tag2>c</tag2>
    </data>
  </root>

In other words, I want to output a "data" element for each row in
tabledef and I want to use the element values from tabledef, but I
want to output only the elements defined with "dbcolumn" attributes in
outputdef and I want to use the element names from outputdef.

I've got the structure correct, but no values. Like this:

  <root>
    <data>
      <tag1></tag1>
      <tag2></tag2>
    </data>
    <data>
      <tag1></tag1>
      <tag2></tag2>
    </data>
    <data>
      <tag1></tag1>
      <tag2></tag2>
    </data>
  </root>

I'm using MSXML4. Here is my XSLT (I'm transforming the tabledef; the
outputdef is being passed in as a DOMDocument parameter):

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:rs='urn:schemas-microsoft-com:rowset'
xmlns:z='#RowsetSchema'
version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:param name="outputdef" />
<xsl:template match="/">
<root>
<xsl:apply-templates select="/xml/rs:data/z:row"/>
</root>
</xsl:template>
<xsl:template match="z:row">
<data>
<xsl:for-each select="$outputdef/root/column">
<xsl:element name="{@tagname}">
--------> SOME MAGIC HAPPENS HERE <----------
</xsl:element>
</xsl:for-each>
</data>
</xsl:template>
</xsl:stylesheet>


How do I get the value from the outer z:row context that corresponds
to the dbcolumn attribute for the current $outputdef/root/column?

Thanks,
John


XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list


Current Thread