Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


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

From: "Stuart Celarier" <stuart@----------->
To:
Date: 5/3/2002 6:24:00 PM
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.

Cheers,
Stuart

----- Answer -----
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt">

  <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>
  <xsl:variable name="desc2">
    <xsl:apply-templates select="/root/base[2]"/>
  </xsl:variable>

  <!-- Create -->
  <xsl:template match="/">
    <html>
      <head><title>Descriptions</title></head>
      <body>
        <table border="1">
          <!-- find number of rows, i.e., maximum number of description
element in either variable -->
          <xsl:variable name="count1"
select="count(msxsl:node-set($desc1)/description)"/>
          <xsl:variable name="count2"
select="count(msxsl:node-set($desc2)/description)"/>
          <xsl:variable name="count"><xsl:choose>
            <xsl:when test="$count1 &gt;= $count2"><xsl:value-of
select="$count1"/></xsl:when>
            <xsl:otherwise><xsl:value-of
select="$count2"/></xsl:otherwise>
          </xsl:choose></xsl:variable>
          
          <xsl:call-template name="description-row">
            <xsl:with-param name="count" select="$count"/>
          </xsl:call-template>
          
        </table>
      </body>
    </html>
  </xsl:template>

  <!-- make copy of all description elements within base -->
  <xsl:template match="base">
    <xsl:copy-of select=".//description"/>
  </xsl:template>  
  
  <!-- add description row and recurse until index = count -->
  <xsl:template name="description-row">
    <xsl:param name="index" select="1"/>
    <xsl:param name="count" />
    <tr>
      <td><xsl:value-of
select="msxsl:node-set($desc1)/description[$index]"/></td>
      <td><xsl:value-of
select="msxsl:node-set($desc2)/description[$index]"/></td>
    </tr>
    
    <!-- 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"/>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>

</xsl:stylesheet>

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


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


transparent
Print
Mail
Digg
delicious
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent