[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Trouble creating a table with multiple named temp

Subject: Re: Trouble creating a table with multiple named templates
From: JBryant@xxxxxxxxx
Date: Wed, 4 May 2005 09:49:58 -0500
studio peirrot
Hi, Max,

There's no way to exit a for-each statement.

The match-and-apply idiom works like this. Given an XML file of the 
following structure:

<directories>
  <directory>
    <staffPersonal>
      <Person>
        <fName>Sam</fName>
        <lName>Spade</lName>
      </Person>
    </staffPersonal>
    <staffOffice>
      <Office>
        <dptFull>Biology</dptFull>
        <Location>Boone</Location>
      </Office>
    </staffOffice>
    <staffTitle>
      <Position>
        <phone>123-4567</phone>
        <pEmail>sam@xxxxxxxxx</pEmail>
      </Position>
    </staffTitle>
  </directory>
  <directory>
    <staffPersonal>
      <Person>
        <fName>Hercule</fName>
        <lName>Peirrot</lName>
      </Person>
    </staffPersonal>
    <staffOffice>
      <Office>
        <dptFull>Biology</dptFull>
        <Location>Boone</Location>
      </Office>
    </staffOffice>
    <staffTitle>
      <Position>
        <phone>234-5678</phone>
        <pEmail>hercule@xxxxxxxxxxx</pEmail>
      </Position>
    </staffTitle>
  </directory>
</directories>

An XSL file can process the whole thing with templates that match elements 
and apply-template instructions, thus:

<xsl:template match="directories">
  <xsl:apply-templates/>
<xsl:template>

<xsl:template match="directory">
  <xsl:apply-templates select="staffPersonal"/>
  <xsl:apply-templates select="staffOffice"/>
  <xsl:apply-templates select="staffTitle"/>
<xsl:template>

<xsl:template match="staffPersonal">
  <xsl:apply-templates/>
<xsl:template>

<xsl:template match="staffOffice">
  <xsl:apply-templates/>
<xsl:template>

<xsl:template match="staffTitle">
  <xsl:apply-templates/>
<xsl:template>

and so on....

The trouble is that you'll get output in document order. The instant you 
want to rearrange the order of the content, this simple fall-through model 
fails. So, in the case of the data you showed us yesterday, I stopped at 
the directory element and pulled bits from down the tree to get them into 
the positions I wanted. You can do that with apply-templates, too, thus:

<xsl:template match="directory">
  <table>
    <tr>
      <td><xsl:apply-templates select="staffOffice/Office/dptFull"/></td>
    </tr>
    <tr>
      <td><xsl:apply-templates select="staffOffice/Office/Location"/></td>
      <td><xsl:apply-templates select="staffTitle/Position/phone"/></td>
    </tr>
  </table>
<xsl:template>

Then you need matching templates for those elements, thus:

<xsl:template match="dptFull"
  <xsl:apply-templates/>
</xsl:template>

Doing it this way, you don't have to have templates to match the elements 
you skip over (such as Office and Position).

I suspect I'm on the edge of causing massive confusion by showing the 
general model and then cutting directly to how to do something else. To 
clarify, the top part of this message (down to "The trouble is") addresses 
the general fall-through model of match-and-apply processing, while the 
bottom half addresses the actual problem.

Getting a couple good books will help a lot. Also, just fiddling with 
various ways to do things always helps me.

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






Max Bronsema <max.bronsema@xxxxxxxxx> 
05/03/2005 12:27 PM
Please respond to
xsl-list@xxxxxxxxxxxxxxxxxxxxxx


To
xsl-list@xxxxxxxxxxxxxxxxxxxxxx
cc

Subject
Re:  Trouble creating a table with multiple named templates






Hi Jay,

Your project sounds intense. Pretty cool. I really like working the
reccommended way as I have found it to eliminate problems down the
road. The for-each method does work except that it does all those
elements at once. Is there a way to exit the for-each statement? I
like the idea of apply-templates though. That would keep me away from
the for-each and make my code simpler for others to read.

<xsl:template match="/">
  <html>
                   <title>Directory</title>
    <body>
      <xsl:apply-templates/> 
    </body>
  </html>
</xsl:template>

Is that what you are talking about with <xsl:apply-templates/> 

The structure is really no more complex then what I have shown before
and what you have listed below. It was 3 seperate XML files, but I
merged them into 1 using ASP. When I include the
<xsl:apply-templates/> and use the stylesheet you provided earlier,
the XSL still does not walk through the XML file. I am going to be out
the rest of the day so no big hurry in a reply. Thanks again Jay and
to any one else who may contribute while I am away. I am getting a
book tommorrow too, so hopefully not as many silly questions. :)

Max Bronsema
 
> Assuming that you have many <directory> elements within some parent
> element (I'll call it directories for demonstration purposes), you might
> try a for-each kind of solution. So, if your source XML looks like this:
> 
> <directories>
>   <directory>
>     <!-- The structure you showed us earlier here -->
>   </directory>
>   <directory>
>     <!-- The structure you showed us earlier here -->
>   </directory>
>   <!-- and so on many times -->
> </directories>
> 
> Then you could do something like this:
> 
> <xsl:template match="directories">
>   <table>
>     <xsl:for-each select="directory">
>       <!-- Reach down the tree to get the bits you need to insert as 
rows
> -->
>     </xsl:for-each>
>   </table>
> </xsl:template>
> 
> Mind you, <xsl:apply-templates/> would work just fine here and be
> preferred (match and apply is XSLT's natural processing model), but I'm
> guessing you have additional complexity that might make for-each more
> workable.

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.