Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How does this work?

Status
Not open for further replies.

vonehle

Programmer
May 16, 2006
35
US
When viewed in HTML, this shows three different tables. I'm trying to get these all into one table. Can someone explain to me, in layman's terms, what makes this Stylesheet create a new table every time?

Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
 xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform"[/URL]
 version="1.0">
    <xsl:output method="html"/>
    
    <xsl:template match="/">
       <html><head><title>Dinosaurs!</title></head>
       <body><h1>Dinosaurs Redone!</h1>
       <xsl:apply-templates select="DinoList/Dinosaur"/>
       </body></html>
    </xsl:template>

  <xsl:template match="Dinosaur">

    <table border="1" width="400" cellpadding="5">
      <tr>
    <th> <td><xsl:value-of select="Name"/>  </td> </th>
    </tr>
         
      <tr>
        <th>Period</th>
        <td>
          <xsl:value-of select="@period"/>
        </td>
      </tr>
      <tr>
        <th>Group</th>
        <td>
          <xsl:value-of select="Group"/>
        </td>
      </tr>
      <xsl:apply-templates select="Range"/>
      <xsl:apply-templates select="PhysicalAttr"/>
    </table>
  </xsl:template>
    
    <xsl:template match="Range">
       <tr>
       <th>Range</th>
       <td>
       <ul>
       <xsl:for-each select="Region">
          <li><xsl:value-of select="."/></li>
       </xsl:for-each>
       </ul>
       </td>
       </tr>       
    </xsl:template>
    
    <xsl:template match="PhysicalAttr">
       <xsl:if test="Height">
          <tr>
          <th>Height</th>
          <td>
          <xsl:value-of select="Height"/>
          <xsl:text disable-output-escaping="yes">
          &amp;nbsp;
          </xsl:text>
          <xsl:value-of select="Height/@unit"/>
          </td>
          </tr>              
       </xsl:if>
       <xsl:if test="Length">
          <tr>
          <th>Length</th>
          <td>
          <xsl:value-of select="Length"/>
          <xsl:text disable-output-escaping="yes">
          &amp;nbsp;
          </xsl:text>
          <xsl:value-of select="Length/@unit"/>
          </td>
          </tr>              
       </xsl:if>
       <xsl:if test="Weight">
          <tr>
          <th>Weight</th>
          <td>
          <xsl:value-of select="Weight"/>
          <xsl:text disable-output-escaping="yes">
          &amp;nbsp;
          </xsl:text>
          <xsl:value-of select="Weight/@unit"/>
          </td>
          </tr>              
       </xsl:if>
    </xsl:template>
</xsl:stylesheet>
 
Without seeing the input document, I would guess that there are three <Dinosaur> elements in <DinoList>.

Move the <table> element.
Code:
    <xsl:template match="/">
       <html><head><title>Dinosaurs!</title></head>
       <body><h1>Dinosaurs Redone!</h1>
       [COLOR=red]<table border="1" width="400" cellpadding="5">[/color]
       <xsl:apply-templates select="DinoList/Dinosaur"/>
       [COLOR=red]</table>[/color]
       </body></html>
    </xsl:template>

  <xsl:template match="Dinosaur">

      <tr>
    <th> <td><xsl:value-of select="Name"/>  </td> </th>
    </tr>
         
      <tr>
        <th>Period</th>
        <td>
          <xsl:value-of select="@period"/>
        </td>
      </tr>
      <tr>
        <th>Group</th>
        <td>
          <xsl:value-of select="Group"/>
        </td>
      </tr>
      <xsl:apply-templates select="Range"/>
      <xsl:apply-templates select="PhysicalAttr"/>
  </xsl:template>

Tom Morrison
 
Thanks a lot! That isn't how I wanted it to look, but I see that it will work better than what I was trying to do. I'm still unclear on what makes it look for all the dinos, instead of just the first one. Why doesn't it need some sort of loop to find them all?
 
>Why doesn't it need some sort of loop to find them all?
Because xslt is functional programming, 90 degree to the procedural. Templates are passive, sitting there to be called upon when the document is being read top to bottom.
 
because you r telling the parser that "whenever you find a dinosaur element in the root element DinoList, apply the following template".
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top