RE: [xsl] Need help with xsl:for-each and xsl:value-of

Subject: RE: [xsl] Need help with xsl:for-each and xsl:value-of
From: "Michael Kay" <michael.h.kay@xxxxxxxxxxxx>
Date: Thu, 21 Mar 2002 09:12:34 -0000
This code is just horrible. XSLT is designed to transform a source tree to
create a result tree. It is not designed to write tags directly into a
serial output stream. The disable-output-escaping attribute is there for
applications that need to write output formats other than pure XML or HTML
(for example, ASP or JSP pages), it is not there to allow you to construct
XML start and end tags by hand. At present, this XSLT code is completely
"against the grain", it is not the way the language was designed to be used,
and trying to use it in this way will always cause problems. If some tool is
generating this code for you, then I don't think much of the tool.

As far as I can see, the transformation you need to do is very simple:

<xsl:template match="test">
  <table>
    <thead>
      <tr>
        <td>att1</td>
        <td>att2</td>
      </tr>
    </thead>
    <tbody>
      <xsl:apply-templates/>
    </tbody>
  </table>
</xsl:template>

<xsl:template match="item">
<tr>
  <td>
    <xsl:choose>
    <xsl:when test="@att1"><xsl:value-of select="@att1"/></xsl:when>
    <xsl:otherwise>&#xa0;</xsl:otherwise>
    </xsl:choose>
  </td>
  <td>
    <xsl:choose>
    <xsl:when test="@att2"><xsl:value-of select="@att2"/></xsl:when>
    <xsl:otherwise>&#xa0;</xsl:otherwise>
    </xsl:choose>
  </td>
</tr>
</xsl:template>

Michael Kay
Software AG
home: Michael.H.Kay@xxxxxxxxxxxx
work: Michael.Kay@xxxxxxxxxxxxxx

> -----Original Message-----
> From: owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> [mailto:owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx]On Behalf Of Williams,
> Chris D.
> Sent: 20 March 2002 21:02
> To: 'XSL-List@xxxxxxxxxxxxxxxxxxxxxx'
> Subject: [xsl] Need help with xsl:for-each and xsl:value-of
>
>
> I have a simple table I am trying to create.  Here is the
> sample XML file.
>
> <?xml version="1.0" encoding="UTF-8"?>
> <test>
>   <item att1="foo1" att2="bar1"/>
>   <item att2="bar2"/>
>   <item att1="foo3"/>
>   <item/>
>   <item att1="foo5" att2="bar5"/>
> </test>
>
> I am currently using XMLSpy to create a XSL file to create my
> HTML table.
> It does create a basic table but not exaclty what I want.
> Right now, if
> either att1 or att2 if not set, nothing gets placed in the
> table cell.  What
> I would like to do it do a test of that value to see if it is
> defined and if
> not, place some other fixed value there.  I have tried adding
> various if
> conditions without any success.  Can this be done??  Should I
> use some other
> command other that xsl:for-each?  Here is the stylesheet it
> is generating.
>
> Thanks
> Chris
>
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
> xmlns:xs="http://www.w3.org/2001/XMLSchema";>
>   <xsl:template match="/">
>   <html>
>     <head />
>       <body>
>         <xsl:for-each select="test">
>           <xsl:for-each select="item">
>             <xsl:if test="position()=1">
>               <xsl:text disable-output-escaping="yes">&lt;table
> border="1"&gt;</xsl:text>
>             </xsl:if>
>             <xsl:if test="position()=1">
>               <thead>
>                 <tr>
>                   <td>att1</td>
>                   <td>att2</td>
>                 </tr>
>               </thead>
>             </xsl:if>
>             <xsl:if test="position()=1">
>               <xsl:text
> disable-output-escaping="yes">&lt;tbody&gt;</xsl:text>
>             </xsl:if>
>             <tr>
>               <td>
>                 <xsl:for-each select="@att1">
>                   <xsl:value-of select="." />
>                 </xsl:for-each>
>               </td>
>               <td>
>                 <xsl:for-each select="@att2">
>                   <xsl:value-of select="." />
>                 </xsl:for-each>
>               </td>
>             </tr>
>             <xsl:if test="position()=last()">
>               <xsl:text
> disable-output-escaping="yes">&lt;/tbody&gt;</xsl:text>
>             </xsl:if>
>             <xsl:if test="position()=last()">
>               <xsl:text
> disable-output-escaping="yes">&lt;/table&gt;</xsl:text>
>             </xsl:if>
>           </xsl:for-each>
>         </xsl:for-each>
>       </body>
>     </html>
>   </xsl:template>
> </xsl:stylesheet>
>
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
>
>


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


Current Thread