Re: [xsl] Trouble creating a table with multiple named templates

Subject: Re: [xsl] Trouble creating a table with multiple named templates
From: JBryant@xxxxxxxxx
Date: Tue, 3 May 2005 11:07:19 -0500
Hi, Max,

This XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

  <xsl:template match="directory">
    <table>
      <tr>
        <td><xsl:value-of select="staffOffice/Office/dptFull"/></td>
      </tr>
      <tr>
        <td><xsl:value-of select="staffOffice/Office/Location"/></td>
        <td><xsl:value-of select="staffTitle/Position/phone"/></td>
        <!-- No fax or box in the sample, so skipped -->
      </tr>
      <tr>
        <td>
          <xsl:value-of 
select="normalize-space(staffPersonal/Person/lName)"/>
          <xsl:text>, </xsl:text>
          <xsl:value-of 
select="normalize-space(staffPersonal/Person/fName)"/>
        </td>
        <td><xsl:value-of select="staffTitle/Position/phone"/></td>
        <td><xsl:value-of select="staffTitle/Position/pEmail"/></td>
        <!-- No title in sample, so skipped -->
        <td><xsl:value-of select="staffOffice/Office/Location"/></td>
      </tr>
    </table>
  </xsl:template>

</xsl:stylesheet>

Produces this HTML fragment:

<table>
  <tr>
    <td>Biology</td>
  </tr>
  <tr>
    <td>Boone</td>
    <td>xxx-xxxx</td>
  </tr>
  <tr>
    <td>last Name, first Name</td>
    <td>xxx-xxxx</td>
    <td>xyxy@xxxxxxxx</td>
    <td>Boone</td>
  </tr>
</table>

I used the normalize-space function to remove the trailing spaces on the 
names. Otherwise, I got a space before the comma.

I'm sure your data is more complex, but I hope this small stylesheet gives 
you one idea for how to approach your problem. Other solutions exist.

If you run into trouble, let us know.

(That wouldn't be Boone, IA, would it?)

Jay Bryant
Bryant Communication Services
(presently consulting at Synergistic Solution Technologies)





Max Bronsema <max.bronsema@xxxxxxxxx> 
05/03/2005 10:28 AM
Please respond to
xsl-list@xxxxxxxxxxxxxxxxxxxxxx


To
xsl-list@xxxxxxxxxxxxxxxxxxxxxx
cc

Subject
Re: [xsl] Trouble creating a table with multiple named templates






Thanks for the nice welcome Jay. I listed below my XML tree and the
desired output as requested. I left out a couple of the inner nodes
that I use in my output just to shorten the e-mail. I have tried
making the table and then pointing to the nodes by using an XPATH
query to them such as //directory/staffPersonal/Person/fName but then
it does not go through all the fName elements, it just lists the first
one repeatedly. Also, I am using XSL 1.0

Thanks again,

Max 


"Anyway, please post a small but complete XML source file and a sample of
your desired output. Then we can help you get to a solution. "

XML tree------

<directory>
  <staffPersonal>
     <Person>
       <fName>first Name </fName>
       <lName>last Name </lName>
     </Person>
  </staffPersonal>
  <staffOffice>
     <Office>
       <dptFull>Biology</dptFull>
       <Location>Boone</Location>
     </Office>
   </staffOffice>
 <staffTitle>
    <Position>
      <phone>xxx-xxxx</phone>
      <pEmail>xyxy@xxxxxxxx</pEmail>
    </Position>
  </staffTitle>
</directory>

------------------------------------------------------------

Desired Output

<table>
   <tr>
       <td>dptFull</td>
   </tr>
   <tr>
       <td>Location</td>
       <td>Phone</td>
       <td>Fax</td>
       <td>Box</td>
    </tr>
    <tr>
        <td>lname
        <xsl:text> ,  </xsl:text>
        fname</td>
    <td>phone</td>
    <td>pEmail</td>
    <td>title</td>
    <td>Location</td>
   </tr>
</table>



> ----- Original Message -----
> From: "Max Bronsema" <max.bronsema@xxxxxxxxx>
> To: <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
> Sent: Monday, May 02, 2005 5:51 PM
> Subject: [xsl] Trouble creating a table with multiple named templates
> 
> Hi all,
> 
> First post. The problem I am having is creating a table using
> templates. Currently my named templates out put the results in a table
> format. I would like to have a master template that outputs all the
> data at once, thus I have control of what the table looks like. I
> currently have three XML documents merged together by ASP into one XML
> document with the root node of directory. From this XML document I
> have been able to pull out all the data I need using templates
> specific to the nodes I am trying to acess. For example:
> 
> <xsl:template name="staffTitle" match="//directory/staffTitle/Position">
>         <table>
>                 <tbody>
>                         <td><xsl:value-of select="Phone"/></td>
>                         <td><xsl:value-of select="pEmail"/></td>
>                         <td><xsl:value-of select="Title"/></td>
>                 </tbody>
>         </table>
> </xsl:template>
> 
> Where I am running into trouble is when I try to create the master
> template to include all of the named templates information. I have
> experimented using a template such as:
> 
> <xsl:template name="master" match="//directory">
> <table>
>         <tbody>
>                 <tr>
>                         <td><xsl:value-of select="dptFull"/></td>
>                 </tr>
>                 <tr>
>                         <td>Location: <xsl:value-of 
select="Location"/></td>
>                         <td>Phone: <xsl:value-of select="Phone" /></td>
>                         <td>Fax: <xsl:value-of select="Fax"/></td>
>                         <td>Box: <xsl:value-of select="oBox"/></td>
>                 </tr>
> etc...
> 
> but this does not return the data in the nodes. The real question is,
> is it possible to combine these named templates into a template that
> can access all of their data? The master template being a table
> structure using <xsl:value-of select=""/> as the way to acess data. It
> works well with my named templates acessing specific parts of the XML
> document. What is the hangup with going to a larger scale? I know
> nesting is not a option and have read some info about using Modes but
> that does not seem like it will work for what I am trying to do. I am
> getting a book on Wednesday but am hoping for some help from this
> wealth of knowledge. Please ask for more details or more code if that
> is what you need.
> 
> Thank you,
> 
> I appreciate the help.
> 
> Max Bronsema

Current Thread