RE: [xsl] XSL Newbie: How2 write transforms for variable XML

Subject: RE: [xsl] XSL Newbie: How2 write transforms for variable XML
From: Ryan Graham <ryan.graham@xxxxxxxxxxxxx>
Date: Fri, 28 Jan 2005 11:42:25 -0700
Using keys and grouping, you can generate some interesting output.  You can
find out more about grouping at
http://www.jenitennison.com/xslt/grouping/muenchian.html.  I'm not sure what
your output requirements are, but the stylesheet below will output an HTML
table, sorted for row comparison, regardless of the element names or order:

This stylesheet:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
 <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
 <xsl:key name="attrName" match="data/row/*" use="name()"/>
 <xsl:variable name="uniqueAttr" select="data/row/*[generate-id() =
generate-id(key('attrName', name())[1])]"/>
 <xsl:template match="/">
  <html>
   <body>
    <table>
     <thead>
      <tr>
       <xsl:for-each select="$uniqueAttr">
        <xsl:sort select="name()"/>
        <td>
         <b>
          <xsl:value-of select="name()"/>
         </b>
        </td>
       </xsl:for-each>
      </tr>
     </thead>
     <tbody>
      <xsl:for-each select="data/row">
       <xsl:variable name="rowChildren" select="*"/>
       <tr>
        <xsl:for-each select="$uniqueAttr">
         <td>
          <xsl:value-of select="$rowChildren[name()=name(current())]"/>
         </td>
        </xsl:for-each>
       </tr>
      </xsl:for-each>
     </tbody>
    </table>
   </body>
  </html>
 </xsl:template>
</xsl:stylesheet>

Applied to this XML:

<data>
 <row>
  <attrA>red</attrA>
  <attrB>128</attrB>
 </row>
 <row>
  <attrC>foo</attrC>
  <attrX>12.00</attrX>
  <attrB>695</attrB>
 </row>
</data>

Will produce the following table:

<html>
 <body>
  <table>
   <thead>
    <tr>
     <td>
      <b>attrA</b>
     </td>
     <td>
      <b>attrB</b>
     </td>
     <td>
      <b>attrC</b>
     </td>
     <td>
      <b>attrX</b>
     </td>
    </tr>
   </thead>
   <tbody>
    <tr>
     <td>red</td>
     <td>128</td>
     <td/>
     <td/>
    </tr>
    <tr>
     <td/>
     <td>695</td>
     <td>foo</td>
     <td>12.00</td>
    </tr>
   </tbody>
  </table>
 </body>
</html>

HTH,
Ryan

-----Original Message-----
From: Joe Weder [mailto:jweder@xxxxxxx] 
Sent: Thursday, January 27, 2005 1:25 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: [xsl] XSL Newbie: How2 write transforms for variable XML


I'm sure there is some way to create XSL that can work for limited 
variability in XML content.

Simple example, consider a report that yields columnar data in which the 
columns in the report will vary - which columns and their order:

Sample1.xml
<data>
<row>
    <attrA>red</attrA>
    <attrB>128</attrB>
</row>
</data>

Sample2.xml
<data>
<row>
    <attrC>foo</attrC>
    <attrX>12.00</attrX>
    <attrB>128</attrB>
</row>
</data>

I want to know how I can develop XSL to handle the data without 
hard-coding selects for specific names (attrA, attrB, attrC, attrD). 
Additionally, I want to be able to define rules for each column. Like 
justification, color, bold, etc. - would this be some kind of meta-data 
or processing instructions?

Any suggestions would be greatly appreciated.

Forgive me if this is well understood, I could not find anything in the 
archive 'cause I was probably not searching for the right keywords.

-- 
Joe Weder
Senior Software Developer/Architect
Intelligent Computer Systems
A Division of Global Beverage Group
717.295.7977 ext 614
www.gbg.com

Current Thread