Re: [xsl] retrive data from #1 xml via data from #2 xml

Subject: Re: [xsl] retrive data from #1 xml via data from #2 xml
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Wed, 14 Mar 2001 18:51:47 +0000
Hi Walter,

> I have a piece of XSLT that I think gets close to a solution (thanks
> again Jeni), but it seems that it does not return any data.
>
> <tr>
>    <xsl:for-each select="$data">
>       <xsl:variable name="datum" select="." />
>       <xsl:for-each select="$columns">
>          <xsl:variable name="column" select="." />
>          <td>
>             <xsl:value-of select="$datum/*[name() = $column]" />
>          </td>
>       </xsl:for-each>
>    </xsl:for-each>
> </tr>
>
> [my sample data has 3 records]
>
> This creates a table with 3 rows, 12 columns, but no data.
>
> It should be 3 rows 5 columns with data.

You don't say what you've set $data and $columns to be.  You need
$data to hold the elements that you want to appear as rows.  So it
needs to be:

<xsl:variable name="data"
   select="/callEvent/response/interaction_list/interaction/events
              /call_event" />

Of course you can crop the path there depending on the context when
you're setting the variable.

You need the $columns variable to hold nodes, each of which has a
value that is the name of the element underneath the call_event that
you want to get at.  So it needs to look something like:

<xsl:variable name="columns"
   select="document('DisplayData.xml')/titles/display/@id"/>

(assuming that your display data is held in a file called
DisplayData.xml)

That should then work, aside from the fact that one of the display
element's id attributes has a value of 'source/timestamp'. That's not
the name of any of the subelements of call_event (and it can't be
because it has a '/' in it). So I guess that you want the value of the
timestamp element of the source element in the same interaction
element as the call_event element is in.  You'll have to test
separately for that:

<tr>
   <xsl:for-each select="$data">
      <xsl:variable name="datum" select="." />
      <xsl:for-each select="$columns">
         <xsl:variable name="column" select="." />
         <td>
            <xsl:choose>
               <xsl:when test="$column = 'source/timestamp'">
                  <xsl:value-of select="ancestor::interaction/source
                                          /timestamp" />
               </xsl:when>
               <xsl:otherwise>
                  <xsl:value-of select="$datum/*[name() = $column]" />
               </xsl:otherwise>
            </xsl:choose>
         </td>
      </xsl:for-each>
   </xsl:for-each>
</tr>

If you're going to be using strange paths a lot in your table
definitions, rather than simply using the names of the elements under
the call_event element, then you'll have to start using something like
saxon:evaluate() to evaluate the strings as XPaths.  That does mean
that you need to redefine some of it, for example rather than using:

  'source/timestamp'

you'd have to use the proper XPath to get from the call_event element
to the data you wanted, i.e.:

  'ancestor::interaction/source/timestamp'

I hope that helps,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/



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


Current Thread