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

Re: XSL Newbie: How2 write transforms for variable XML

Subject: Re: XSL Newbie: How2 write transforms for variable XML
From: Joe Weder <jweder@xxxxxxx>
Date: Fri, 28 Jan 2005 14:21:41 -0500
joe weder
Ryan,

You are awesome dude! Thanks for the help. I really appreciate it. This is a big help getting me started.

Ryan Graham wrote:

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 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

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.