This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Variable number of attributes


At 04:41 AM 09/30/2000 -0500, Aaron Bawcom wrote:
>I'm trying to produce a single style sheet (foo.xsl) that offers the
>following functionality. I'm using IE 5.5. Any help would be greatly
>appreciated.
>
>[snip]
>
>And having a document:
>
><doc>
>         <ele name1="A1" name2="A2" name3="A3"/>
>         <ele name1="B1" name2="B2" name3="B3"/>
></doc>
>
>processed through the same tyle sheet (foo.xsl) produces:
>
><TABLE>
>         <TR>
>                 <TH>name1</TH>
>                 <TH>name2</TH>
>                 <TH>name3</TH>
>         </TR>
>         <TR>
>                 <TD>A1</TD>
>                 <TD>A2</TD>
>                 <TD>A3</TD>
>         </TR>
>         <TR>
>                 <TD>B1</TD>
>                 <TD>B2</TD>
>                 <TD>B3</TD>
>         </TR>
></TABLE>

A couple of observations:

(1) The below works as long as the <ele> elements each has the same number 
of attributes, with the same names. If one <ele> has 4 attributes and one 
has 3, for example, then it will break.

(2) I tested this with both of your examples under IE5.5 (July preview) 
release Also with Saxon; output from Saxon was:

<html xmlns="http://www.w3.org/TR/REC-html40">
   <head><title>Attributes to Elements</title></head>
   <body>
     <table>
       <tr>
         <td>A1</td>
         <td>A2</td>
         <td>A3</td>
       </tr>
       <tr>
         <td>B1</td>
         <td>B2</td>
         <td>B3</td>
       </tr>
     </table>
   </body>
</html>

(3) General idea is to:
(a) Build the root of the result tree to correspond to your root <doc> 
element; this result tree includes a <table> element.
(b) Then there's a template rule which handles the <ele> elements. Within 
this template rule, if this is the first <ele>, put the table headers into 
the rsult tree, values coming from the attribute names for this first 
<ele>. Then all the attributes are processed, values being placed into <td> 
elements.

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns="http://www.w3.org/TR/REC-html40" >

   <xsl:template match="/doc">
     <html>
       <head><title>Attributes to Elements</title></head>
       <body>
         <table>
           <xsl:apply-templates />
         </table>
       </body>
     </html>
   </xsl:template>

   <xsl:template match="ele">
     <xsl:if test="position()=1">
       <tr>
         <xsl:for-each select="@*">
           <th><xsl:value-of select="name()"/></th>
         </xsl:for-each>
       </tr>
     </xsl:if>
     <tr>
       <xsl:for-each select="@*">
         <td><xsl:value-of select="."/></td>
       </xsl:for-each>
     </tr>
   </xsl:template>

</xsl:stylesheet>

==========================================================
John E. Simpson               | "Curiosity killed the cat,
http://www.flixml.org         | but for a while I was a
XML Q&A: http://www.xml.com   | suspect." (Steven Wright) 


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

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]