RE: [xsl] data translation => descendants appear side by side in HTML-table

Subject: RE: [xsl] data translation => descendants appear side by side in HTML-table
From: "Stuart Celarier" <stuart@xxxxxxxxxxx>
Date: Fri, 3 May 2002 19:29:09 -0700
Here is a stylesheet that does the trick. The idea I've used here is to
create a pair of variables to hold copies of the description elements
from each base element. Then we can figure out the number of rows in the
table, and generate the rows using recursion. As an optimization, the
variables containing the description elements are top-level (or global)
elements so they don't have to be passed as parameters on each recursive
template call.

I've used the msxsl:node-set extension element from MSXML 4.0, you will
have to replace that with the appropriate extension element for the XSLT
processor you are using.


----- Answer -----
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"

  <xsl:output method="html"/>

    Variables containing description elements in each base element.
    Use top-level variables for efficiency, so they don't have to be
passed into templates
  <xsl:variable name="desc1">
    <xsl:apply-templates select="/root/base[1]"/>
  <xsl:variable name="desc2">
    <xsl:apply-templates select="/root/base[2]"/>

  <!-- Create -->
  <xsl:template match="/">
        <table border="1">
          <!-- find number of rows, i.e., maximum number of description
element in either variable -->
          <xsl:variable name="count1"
          <xsl:variable name="count2"
          <xsl:variable name="count"><xsl:choose>
            <xsl:when test="$count1 &gt;= $count2"><xsl:value-of
          <xsl:call-template name="description-row">
            <xsl:with-param name="count" select="$count"/>

  <!-- make copy of all description elements within base -->
  <xsl:template match="base">
    <xsl:copy-of select=".//description"/>
  <!-- add description row and recurse until index = count -->
  <xsl:template name="description-row">
    <xsl:param name="index" select="1"/>
    <xsl:param name="count" />
    <!-- recurse -->
    <xsl:if test="$index &lt; $count">
      <xsl:call-template name="description-row">
        <xsl:with-param name="index" select="$index + 1"/>
        <xsl:with-param name="count" select="$count"/>


----- End of Answer -----

 XSL-List info and archive:

Current Thread